设置LSTM层的初始状态

时间:2019-07-20 08:56:53

标签: python tensorflow keras

我有以下代码:

units = 1024
lstm_layer = tf.keras.layers.LSTM(units)

dim = tf.zeros([64,1024])

output, hidden = lstm_layer(embedded_data, initial_state = dim)

我收到以下错误消息:

ValueError: An `initial_state` was passed that is 
not compatible with `cell.state_size`.
 Received `state_spec`=
ListWrapper([InputSpec(shape=(64, 1024), ndim=2)]);
 however `cell.state_size` is [1024, 1024]

当我使用GRU单元而不是LSTM单元进行操作时,它可以正常工作。但是对于LSTM单元,此代码不起作用。我意识到LSTM具有两个参数,因此代码要求一个单元状态为[1024,1024],但是我不知道如何设置初始状态。我尝试过

initial_state = [dim, dim] 

那也不起作用,因为它给了我

ValueError: too many values to unpack (expected 2).

我引用了LSTM Initial state from Dense layer,但似乎无法解决我的问题...

1 个答案:

答案 0 :(得分:0)

如果有人需要解决此问题,以供将来参考:

问题是输出。只需使用tf.zeros([64,1024])即可,您只需要三个输出即可:

output, hidden_h,hidden_c = lstm_layer(embedded_data, initial_state = [dim,dim])