Keras:LSTM中input_dim和input_length的区别

11
在构建LSTM时,我们需要通过以下方式提供输入形状信息:
input_shape = () # a tuple

或者,通过:

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

我对这两个数量感到有些困惑。它们表示什么?

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

2个回答

4
我将尽量简化输入形状参数: 对于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参数指定batch的大小(上述内容未指定)。如果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


3
我们可以从两个方面理解这个问题,时间序列数据和自然语言处理。
在时间序列数据的情况下,假设我们有以下数据集,即为两周内每天的温度。
[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(每天气温的值)。

对于自然语言处理,假设句子长度为4,比如"how are you doing",而我们的嵌入层的输出为8,则句子中的每个单词都将由长度为8的向量表示,因此"hello"将是长度为8的向量,“are”将是长度为8的向量等等。对于这种情况,input_length = ()为4(句子长度),而input_dim = ()为8(嵌入向量的长度)。

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


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接