我希望创建一个Keras模型,包括一个嵌入层,接着是两个带有0.5丢失率的LSTM,最后是一个具有softmax激活函数的密集层。
第一个LSTM应将顺序输出传播到第二层,而在第二层中,我只对在处理整个序列后获得的LSTM隐藏状态感兴趣。
我尝试了以下内容:
然而,我遇到了以下错误:
第一个LSTM应将顺序输出传播到第二层,而在第二层中,我只对在处理整个序列后获得的LSTM隐藏状态感兴趣。
我尝试了以下内容:
sentence_indices = Input(input_shape, dtype = 'int32')
embedding_layer = pretrained_embedding_layer(word_to_vec_map, word_to_index)
embeddings = embedding_layer(sentence_indices)
# Propagate the embeddings through an LSTM layer with 128-dimensional hidden state
X = LSTM(128, return_sequences=True, dropout = 0.5)(embeddings)
# Propagate X trough another LSTM layer with 128-dimensional hidden state
X = LSTM(128, return_sequences=False, return_state=True, dropout = 0.5)(X)
# Propagate X through a Dense layer with softmax activation to get back a batch of 5-dimensional vectors.
X = Dense(5, activation='softmax')(X)
# Create Model instance which converts sentence_indices into X.
model = Model(inputs=[sentence_indices], outputs=[X])
然而,我遇到了以下错误:
ValueError: Layer dense_5 expects 1 inputs, but it received 3 input tensors. Input received: [<tf.Tensor 'lstm_10/TensorArrayReadV3:0' shape=(?, 128) dtype=float32>, <tf.Tensor 'lstm_10/while/Exit_2:0' shape=(?, 128) dtype=float32>, <tf.Tensor 'lstm_10/while/Exit_3:0' shape=(?, 128) dtype=float32>]
显然,LSTM返回的输出形状与我预期的不同。我该如何解决这个问题?