【问题标题】:What's the difference between input_shape and batch_input_shape in LSTMLSTM中的input_shape和batch_input_shape有什么区别
【发布时间】:2018-08-28 16:39:59
【问题描述】:

这只是设置相同事物的不同方式还是它们实际上具有不同的含义?和网络配置有关系吗?

举个简单的例子,我看不出有什么区别:

model = Sequential()
model.add(LSTM(1, batch_input_shape=(None,5,1), return_sequences=True))
model.add(LSTM(1, return_sequences=False))

model = Sequential()
model.add(LSTM(1, input_shape=(5,1), return_sequences=True))
model.add(LSTM(1, return_sequences=False))

但是,当我将批量大小设置为 12 batch_input_shape=(12,5,1) 并在拟合模型时使用 batch_size=10 时,出现错误。

ValueError: 无法为张量提供形状 (10, 5, 1) 的值 'lstm_96_input:0',形状为'(12, 5, 1)'

这显然是有道理的。但是,我认为在模型级别限制批量大小没有任何意义。

我错过了什么吗?

【问题讨论】:

    标签: machine-learning deep-learning keras lstm rnn


    【解决方案1】:

    这只是设置相同事物的不同方式还是它们实际上具有不同的含义?和网络配置有关系吗?

    是的,它们实际上是等效的,您的实验证实了这一点,另请参阅 this discussion

    但是我认为在模型级别限制批量大小没有任何意义。

    批量大小限制有时是必要的,我想到的例子是 有状态 LSTM,其中一个批次中的最后一个单元状态被记住并用于后续批次的初始化。这确保了客户端不会将不同的批量大小输入网络。示例代码:

    # Expected input batch shape: (batch_size, timesteps, data_dim)
    # Note that we have to provide the full batch_input_shape since the network is stateful.
    # the sample of index i in batch k is the follow-up for the sample i in batch k-1.
    model = Sequential()
    model.add(LSTM(32, return_sequences=True, stateful=True,
                   batch_input_shape=(batch_size, timesteps, data_dim)))
    

    【讨论】:

      猜你喜欢
      • 2020-06-04
      • 2017-08-19
      • 1970-01-01
      • 2018-10-03
      • 1970-01-01
      • 2020-01-22
      • 2019-05-09
      • 1970-01-01
      • 2018-06-26
      相关资源
      最近更新 更多