【问题标题】:Keras: the difference input_dim and input_length in LSTMKeras:LSTM 中 input_dim 和 input_length 的区别
【发布时间】:2016-11-16 23:47:53
【问题描述】:

在构建 LSTM 时,我们需要通过以下方式提供输入形状信息:

input_shape = () # a tuple

或者,通过:

input_length = () # an integer
input_dim = () # an integer

我对这两个数量感到有些困惑。他们在说明什么?

另外,input_dim是所谓的时间步长吗?

【问题讨论】:

    标签: keras lstm


    【解决方案1】:

    我会尽量简化输入的形状参数: 在 LSTM(或通常用于 RNN)的情况下,输入形状可以通过以下方式提供:

    1. input_shape 关键字参数:input_shape = (input_length, input_dim) 其中 input_length = 序列的长度和 input_dim = 特征/变量的数量。如果未提供这些值,则表示可以预期任何正整数。在这里,您没有提到批量大小,即训练期间权重更新的观察次数。例如 input_length = 50(这是你的序列长度) input_dim = 10(这是数据中输入特征的数量)

      model.add(LSTM(16, input_shape = (50,10)))

    2. 使用单独的 input_dim 和 input_length 参数 在这里,您可以分别指定 input_dim (即数据中的特征数)和 input_length (即数据中单个观测值的序列长度)。例如 model.add(LSTM(16, input_length= 50, input_dim =10))。这相当于上面描述的方式

    3. 最后,您可以通过 batch_input_size 参数指定批处理的大小(上面没有指定)。如果 LSTM 是有状态的,您必须预先指定批处理大小。 batch_input_size = (batch_size,input_length, input_dim)

    model.add(LSTM(16,batch_input_size = (None,50,10)))相当于上面两个

    model.add(LSTM(16,batch_input_size = (32,50,10)))批量大小为32

    【讨论】:

      【解决方案2】:

      我们可以从时间序列数据和 NLP 两种场景中理解这一点。

      如果是时间序列数据,假设我们有以下数据集,即两周的日间温度。

      [23.4, 23.4,22.4,23.4,26.4,24.4,27.4,28.4,21.4,25.4,23.4,23.4,23.4, 24.5]
      

      我们想要获取第 5 天的温度并想要预测第 6 天的温度。在这种情况下,input_length = () 将为 5(例如 23.4、23.4、22.4、23.4、26.4),input_dim = () 将为 1(每天的温度值)。

      在 NLP 的情况下,假设句子长度为 4,例如“你好吗”,我们嵌入层的输出为 8,那么句子的每个单词将由长度为 8 的向量表示,所以“你好”将是长度为 8 的向量,“are”将是向量或长度为 8 等等。 对于这种情况,input_length = () 将是 4(句子长度),input_dim =() 将是 8(嵌入向量的长度)。

      请参阅https://www.tensorflow.org/tutorials/structured_data/time_series,了解有关 LSTM 时间序列数据的详细信息。

      【讨论】:

        猜你喜欢
        • 2017-12-09
        • 2020-06-04
        • 1970-01-01
        • 2019-10-20
        • 1970-01-01
        • 1970-01-01
        • 2020-08-10
        • 1970-01-01
        • 2011-10-12
        相关资源
        最近更新 更多