【问题标题】:keras LSTM: An `initial_state` was passed that is not compatible with `cell.state_size`keras LSTM:传递了与“cell.state_size”不兼容的“initial_state”
【发布时间】:2021-06-15 12:34:58
【问题描述】:

我正在构建一个由 1 个 LSTM 层组成的非常简单的 NN。代码如下:

import tensorflow as tf    
lstm = tf.keras.layers.LSTM(15, return_sequences = True, return_state = True)
sample_hidden = tf.zeros((1000, 15))
x = tf.zeros((1000, 100, 10))
y, h, c = lstm(x, initial_state=sample_hidden)

当我运行此代码时,我收到以下错误:

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

我不明白为什么 cell.state_size 是 [15,15]。但是,即使我尝试输入具有这种大小的零数组,我仍然会遇到同样的错误。

有什么帮助吗?

【问题讨论】:

    标签: tensorflow keras lstm


    【解决方案1】:

    初始状态应包括 h 和 c,而不仅仅是 h。编辑代码如下:

    import tensorflow as tf    
    lstm = tf.keras.layers.LSTM(15, return_sequences = True, return_state = True)
    h = tf.zeros((1000, 15))
    c = tf.zeros((1000, 15))
    x = tf.zeros((1000, 100, 10))
    y, h, c = lstm(x, initial_state=[h, c])
    

    这将消除错误。

    仅为遇到同样问题的人发布答案

    【讨论】:

      猜你喜欢
      • 2021-12-09
      • 1970-01-01
      • 2019-12-03
      • 2019-02-14
      • 2021-01-18
      • 1970-01-01
      • 1970-01-01
      • 2018-07-09
      • 2021-03-08
      相关资源
      最近更新 更多