keras - 如何计算 LSTM 的正确批量大小?
问题描述
我有一个如下所示的每日时间序列数据。
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 个每日数据,但我不想丢失这些数据。
我怎样才能正确地做到这一点?
解决方案
我认为你总是会得到一个不起作用的数字。因为这不是最佳实践。我建议您使用DataLoader
which 将轻松为您加载批次(以及如何将自定义数据集馈送到 dataloder)。通过给batch_size
它Dataloader
它会将你的数据集分成最大可能的批次,batch_size
最后一批是<=batch_size
.
关于LSTM
, 使用batch_first=True
, 并 有 你 的 批次 在 这种 形状(batch, seq, feature)
. 这将使您不必为给出特定大小而头疼,并且input_size
必须等于feature
.
推荐阅读
- php - Arfooo 目录脚本 -> 如何实现 foreach?
- html - 当列溢出div的宽度时如何使列转到下一行
- mongoose-os - 是否可以在 Mongoose OS 中设置带条件的计时器?
- python - Automa 在打印中安装错误 - Python
- python-3.x - 线程的奇怪行为
- codenameone - Codenameone BrowserComponent.setProperty 给出 NullPointerException
- apache-camel - Liquibase 在 Camel 启动后应用补丁,导致错误
- scala - Tableau Extract API 返回“服务器没有回叫我们”
- sql - SQL 将两个 SUM 查询的结果用于一个查询
- linux - 如何在 Linux 上从 STM32WB55 Nucleo Pack 闪存 MB1355C 和/或 MB1293C?