【发布时间】:2018-08-09 16:39:35
【问题描述】:
我正在研究我希望在 keras 中成为一个简单的 nmt 翻译器。下面是 keras 中 seq2seq 的一些启发性示例的链接。
https://machinelearningmastery.com/develop-encoder-decoder-model-sequence-sequence-prediction-keras/
我想要一个模型,它以 300 个向量作为单词输入,一次取 25 个。这 25 个数字是一个句子的长度。 Units 是 300 数字,tokens_per_sentence 是 25 数字。下面的代码只有训练模型。我省略了推理模型。我的模型可以编译,但是当我使用训练数据运行它时,出现尺寸错误。我曾尝试重塑dense_layer_b 的输出,但我反复被告知操作的输出需要与输入具有相同的大小。我使用 python3 和 tensorflow 作为后端。我的 keras 是 v2.1.4。我的操作系统是 uubuntu。
一条错误消息:
ValueError: Error when checking target: expected dense_layer_b to have 2 dimensions, but got array with shape (1, 25, 300)
一些终端输出:
Tensor("lstm_1/while/Exit_2:0", shape=(?, 25), dtype=float32)
(?, 25) (?, 25) h c
一些代码:
def model_lstm():
x_shape = (None,units)
valid_word_a = Input(shape=x_shape)
valid_word_b = Input(shape=x_shape)
### encoder for training ###
lstm_a = LSTM(units=tokens_per_sentence, return_state=True)
recurrent_a, lstm_a_h, lstm_a_c = lstm_a(valid_word_a)
lstm_a_states = [lstm_a_h , lstm_a_c]
print(lstm_a_h)
### decoder for training ###
lstm_b = LSTM(units=tokens_per_sentence ,return_state=True)
recurrent_b, inner_lstmb_h, inner_lstmb_c = lstm_b(valid_word_b, initial_state=lstm_a_states)
print(inner_lstmb_h.shape, inner_lstmb_c.shape,'h c')
dense_b = Dense(tokens_per_sentence , activation='softmax', name='dense_layer_b')
decoder_b = dense_b(recurrent_b)
model = Model([valid_word_a,valid_word_b], decoder_b)
return model
我希望当代码实际与数据一起使用时,终端输出中的问号可以替换为我的向量大小。
编辑: 我一直在尝试通过切换代码中的尺寸来解决这个问题。我已经更新了代码和错误消息。我仍然有基本相同的问题。 Dense 层似乎不起作用。
【问题讨论】:
标签: python tensorflow keras