【发布时间】:2019-04-19 17:18:47
【问题描述】:
使用tf.keras.layers.RNN API 在 TensorFlow (1.13.1) 中多层/堆叠 RNN 的初始状态所需的结构是什么?
我尝试了以下方法:
lstm_cell_sizes = [256, 256, 256]
lstm_cells = [tf.keras.layers.LSTMCell(size) for size in lstm_cell_sizes]
state_init = [tf.placeholder(tf.float32, shape=[None] + cell.state_size) for cell in lstm_cells]
tf.keras.layers.RNN(lstm_cells, ...)(inputs, initial_state=state_init)
这会导致:
ValueError: Could not pack sequence. Structure had 6 elements, but flat_sequence had 3 elements. Structure: ([256, 256], [256, 256], [256, 256]), flat_sequence: [<tf.Tensor 'player/Placeholder:0' shape=(?, 256, 256) dtype=float32>, <tf.Tensor 'player/Placeholder_1:0' shape=(?, 256, 256) dtype=float32>, <tf.Tensor 'player/Placeholder_2:0' shape=(?, 256, 256) dtype=float32>].
如果我将 state_init 更改为形状为 [None, 256] 的张量扁平列表,我会得到:
ValueError: An `initial_state` was passed that is not compatible with `cell.state_size`. Received `state_spec`=[InputSpec(shape=(None, 256), ndim=2), InputSpec(shape=(None, 256), ndim=2), InputSpec(shape=(None, 256), ndim=2)]; however `cell.state_size` is [[256, 256], [256, 256], [256, 256]]
Tensorflow RNN docs 对此相当模糊:
"你可以通过符号指定RNN层的初始状态 使用关键字参数
initial_state调用它们。的价值initial_state应该是张量或张量列表表示 RNN 层的初始状态。”
【问题讨论】:
-
我现在面临着完全相同的问题。一个解决方案会很好!
标签: tensorflow keras lstm recurrent-neural-network