我正在尝试构建一个LSTM
网络,该网络采用一系列单词并将其转换为嵌入向量。我已经将每个单词序列转换为词汇向量。
我正在使用的批处理大小为32,每个词汇向量的大小为50。这是我到目前为止用来创建模型并将其转换为估计量的Keras Functional API代码
input_layer = keras.layers.Input(shape=(50,), name='search')
embedding_layer = keras.layers.Embedding(input_dim=32, output_dim=256, input_length=50)(input_layer)
lstm_layer = keras.layers.LSTM(units=256)(embedding_layer)
model = keras.models.Model(inputs=input_layer, outputs=lstm_layer)
model.compile(loss='mean_squared_error', optimizer='adam')
estimator = keras.estimator.model_to_estimator(keras_model=model)
但是此代码给出了错误
tensorflow.python.framework.errors_impl.InvalidArgumentError: Node 'Adam/gradients/lstm/StatefulPartitionedCall_grad/StatefulPartitionedCall': Connecting to invalid output 5 of source node lstm/StatefulPartitionedCall which has 5 outputs
当我运行model.summary()
时,这就是输出
Layer (type) Output Shape Param #
=================================================================
search (InputLayer) [(None, 50)] 0
_________________________________________________________________
embedding (Embedding) (None, 50, 256) 8192
_________________________________________________________________
lstm (LSTM) (None, 256) 525312
=================================================================
Total params: 533,504
Trainable params: 533,504
Non-trainable params: 0
_________________________________________________________________
我认为这是我期望的。我尝试将LSTM
层替换为相同形状的Dense和Flatten层,并且代码工作正常
答案 0 :(得分:0)
要自己回答这个问题……看来here到7/24为止,tf.keras.layers.LSTM存在问题。我将模型更改为以下
input_layer = keras.layers.Input(shape=(50,), name='search')
embedding_layer = keras.layers.Embedding(input_dim=32, output_dim=256,
input_length=50)(input_layer)
lstm_layer = keras.layers.RNN(cell=keras.layers.LSTMCell(units=256))(embedding_layer)
model = keras.models.Model(inputs=input_layer, outputs=lstm_layer)
model.compile(loss='mean_squared_error', optimizer='adam')
estimator = keras.estimator.model_to_estimator(keras_model=model)