我正在尝试使用 tensorflow2 构建一个生成式 LSTM 模型。我不熟悉在 tensorflow 中使用 LSTM 层。代码如下::
vec = layers.TextVectorization(output_sequence_length=maxlen,
max_tokens=379)
vec.adapt(use_for_vocab(train))
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)
我能够毫无错误地编译模型。但是,在拟合模型时,它给出了以下错误:
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 为真,但输入序列仍然是二维的。任何帮助将不胜感激。
当你为 nlp 嵌入时,你可以这样做:
嵌入(vocab_size,embedding_dim)
我不知道你用的是什么嵌入
所以而不是 x_word = embed(inp_word) 你可以使用 x_word = layers.Embedding(vocab_size, embedding_dim)(inp_word)