【问题标题】:How to calculate correct batch size for LSTM?如何计算 LSTM 的正确批量大小?
【发布时间】:2019-10-05 16:43:22
【问题描述】:

我有如下的每日时间序列数据。

            CashIn  CashOut
Date        
2016-01-01  0.0     6500.0
2016-01-02  0.0     23110.0
2016-01-03  0.0     7070.0
2016-01-04  0.0     18520.0
2016-01-05  20840.0 22200.0
.
.
.
2019-03-25  59880.0 25500.0
2019-03-26  49270.0 17860.0
2019-03-27  45160.0 48600.0
2019-03-28  39480.0 22840.0
2019-03-29  70260.0 25950.0
2019-03-30  19250.0 24350.0
2019-03-31  46870.0 14400.0

我的总数据量是 1186。我想使用 LSTM 预测 2019-04-01 和 2019-04-30 之间的 CashIn 和 CashOut 值。

我写了一个像下面这样的批处理计算器。

def get_batches(arr, batch_size, seq_length):

    batch_size_total = batch_size * seq_length

    n_batches = len(arr)//batch_size_total

    arr = arr[:n_batches * batch_size_total]
    arr = arr.reshape((batch_size, -1))

    for n in range(0, arr.shape[1], seq_length):
        x = arr[:, n:n+seq_length]
        y = np.zeros_like(x)
        try:
            y[:, :-1], y[:, -1] = x[:, 1:], arr[:, n+seq_length]
        except IndexError:
            y[:, :-1], y[:, -1] = x[:, 1:], arr[:, 0]
        yield x, y

我正在尝试使用 get_batches 函数将此数据集划分为具有 30 个序列长度的批次,因为我有每日时间序列并且我想预测接下来的 30 天。

batches = get_batches(np.array(data_cashIn), 40, 30)

如果我在 get_bathces 函数中写入 39 而不是 40 作为参数,那么我将丢失最近 16 天的数据,但我不想丢失这些数据。

我怎样才能正确地做到这一点?

【问题讨论】:

    标签: keras deep-learning time-series lstm pytorch


    【解决方案1】:

    我认为你总是会得到一个不起作用的数字。因为这不是最佳实践。我建议您使用DataLoader,它可以轻松地为您加载批次(and here's how you can have a custom dataset fed to the dataloder)。通过将batch_size 提供给Dataloader,它会将您的数据集拆分为batch_size 的最大可能批次,最后一批为<=batch_size


    对于LSTM,使用batch_first=True 并让您的批次采用这种形状(batch, seq, feature)。这将使您不必为指定特定大小而头疼,而且input_size 必须等于feature

    【讨论】:

      猜你喜欢
      • 2018-03-21
      • 1970-01-01
      • 2021-05-31
      • 1970-01-01
      • 2022-01-04
      • 2018-12-01
      • 2014-10-04
      • 2020-11-13
      • 2018-10-11
      相关资源
      最近更新 更多