keras 层“lstm_1”的输入0与层不兼容:预期ndim=3,找到ndim=2,收到完整形状:(无,256)

zqdjd7g9  于 2023-04-06  发布在  其他
关注(0)|答案(1)|浏览(185)

我正在尝试使用tensorflow 2构建一个生成式LSTM模型。我是在tensorflow中使用LSTM层的新手。代码如下:

vec = layers.TextVectorization(output_sequence_length=maxlen,
                               max_tokens=379)
vec.adapt(use_for_vocab(train)) 
# here i just convert the train dataset into a form suitable to use for vectorization 

voc = vec.get_vocabulary()
voc_size = len(voc)

embed = layers.Embedding(input_dim=voc_size,
                         output_dim=256,
                         mask_zero=True)

inp_word = layers.Input(shape=(maxlen+2,), # maxlen is the maximum length of the sentence in the text
                   name="word_input")      # 2 is added to accommodate start_token and end_token
x_word = embed(inp_word)
x_word = layers.Dropout(0.5)(x_word)
x_word = layers.LSTM(256, return_sequences=True)(x_word)
ops_word = layers.GlobalAveragePooling1D(name="word_gap")(x_word)

以下是模型摘要:

型号:“word_model”

层(类型)输出形状参数编号
word_input(InputLayer)【(无,35)】0
embedding_1(嵌入)(无,35,128)45184
dropout_6(Dropout)(无,35,128)0
lstm_5(LSTM)(无,35,256)三九四二四零
word_gap(GlobalAveragePooling1D)(无,256)0

总参数:439,424可训练参数:439,424不可训练的参数:0
我已经创建了一个预取的数据集作为输入。这是我如何使用下面的函数构建它的:

from tensorflow.data import Dataset, AUTOTUNE

def format_dataset(x, y):
  y = Dataset.from_tensor_slices(y)
  data = Dataset.zip((x, y))
  return data.batch(32).prefetch(AUTOTUNE)

我能够编译模型没有任何错误。但是,在拟合模型时,它给出了以下错误:

File "/usr/local/lib/python3.9/dist-packages/keras/engine/training.py", line 1284, in train_function  *
        return step_function(self, iterator)
    File "/usr/local/lib/python3.9/dist-packages/keras/engine/training.py", line 1268, in step_function  **
        outputs = model.distribute_strategy.run(run_step, args=(data,))
    File "/usr/local/lib/python3.9/dist-packages/keras/engine/training.py", line 1249, in run_step  **
        outputs = model.train_step(data)
    File "/usr/local/lib/python3.9/dist-packages/keras/engine/training.py", line 1050, in train_step
        y_pred = self(x, training=True)
    File "/usr/local/lib/python3.9/dist-packages/keras/utils/traceback_utils.py", line 70, in error_handler
        raise e.with_traceback(filtered_tb) from None
    File "/usr/local/lib/python3.9/dist-packages/keras/engine/input_spec.py", line 235, in assert_input_compatibility
        raise ValueError(

    ValueError: Exception encountered when calling layer 'caption_model' (type Functional).
    
    Input 0 of layer "lstm_5" is incompatible with the layer: expected ndim=3, found ndim=2. Full shape received: (None, 128)

无法理解为什么输入序列仍然是二维的,尽管使return_sequences为true。任何帮助都将不胜感激。

of1yzvn4

of1yzvn41#

当你为nlp做嵌入时,你可以这样做:
嵌入(vocab_size,embedding_dim)
我不知道你用了什么嵌入
因此,可以使用x_word = layers代替x_word = embed(inp_word)。Embedding(vocab_size,embedding_dim)(inp_word)
编辑:我在colab中运行了这段代码,完全没有问题:

embed = layers.Embedding(input_dim=1000,output_dim=256,mask_zero=True)

inp_word = layers.Input(shape=(50,), 
                   name="word_input")     
x_word = embed(inp_word)
x_word = layers.Dropout(0.5)(x_word)
x_word = layers.LSTM(256, return_sequences=True)(x_word)
print(x_word.shape) #(None, 50, 256)
ops_word = layers.GlobalAveragePooling1D(name="word_gap")(x_word)
print(ops_word.shape) #(None, 256)

我猜你的数据预处理错了。你的输入数据形状是不是像(N,sequence_len)?

相关问题