我正在使用 Keras 神经网络库来解决时间序列预测问题。我正在尝试将训练集分为实际训练集和验证集。我不想从我的集合末尾获取所有验证数据,而是每 5 个训练样本进行 1 次验证测试。
我已经成功创建了两个生成器
training_sequence = TimeseriesGenerator(train_x, train_y, length=w, sampling_rate=1, batch_size=batch_s)
validation_sequence = TimeseriesGenerator(train_x, train_y, length=w, sampling_rate=1, stride=5, batch_size=batch_s)
我会用它们进行训练,例如:
history = model.fit_generator(training_sequence, validation_sequence, epochs=200, callbacks=[early_stopping_monitor], verbose=1)
现在,我正在获取正确的验证序列,但我不知道如何从训练序列中获取这些样本(这样它就不会验证已经训练过的数据)。
我尝试在包装器中处理训练生成器,如下所示:
def get_generator(data, targets, length, batch_size):
data_gen = TimeseriesGenerator(data, targets, length=length,
sampling_rate=1, batch_size=batch_size)
for i in range(len(data_gen)):
if i % 5 != 0:
x, y = data_gen[i]
yield x, y
但是当我运行代码时,我收到此错误:
ValueError: `steps_per_epoch=None` is only valid for a generator based on the `keras.utils.Sequence` class. Please specify `steps_per_epoch` or use the `keras.utils.Sequence` class.
如果我添加
steps_per_epoch=len(train_x)/batch_s
我收到“StopIteration”错误。
最佳答案
发生“StopIteration”错误,因为模型需要从生成器获取数据,但生成器已经耗尽了所有数据。
假设我们的 train_x 中有 320 个元素,批量大小为 32。 因此steps_per_epoch=(320/32)=10。
因此你必须在每个时期产生 10 次 yield 。 但由于 if 条件,我们不会在 i=5 和 i=10 时产生结果。 因此,我们只产生了 8 次,但我们已经告诉模型,我们将通过steps_per_epoch 产生 10 次。
steps_per_epoch=len(train_x)/batch_s
no_missing_steps=steps_per_epoch/5
steps_per_epoch=steps_per_epoch-no_missing_steps
如果这不起作用,请尝试这个。将 for 循环封装在 while 循环中。
def get_generator(data, targets, length, batch_size):
data_gen = TimeseriesGenerator(data, targets, length=length,
sampling_rate=1, batch_size=batch_size)
while true:
for i in range(len(data_gen)):
if i % 5 != 0:
x, y = data_gen[i]
yield x, y
关于python - 如何使用Keras TimeseriesGenerator为每n个训练样本获取一个验证样本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51364077/