Python 清单6.33生成timeseries示例及其目标的生成器。如何终止生成器以创建训练样本
当我阅读《Python深度学习》一书时,我找不到终端代码,while条件总是正确的。我想知道如何获得训练样本。非常感谢你帮助我Python 清单6.33生成timeseries示例及其目标的生成器。如何终止生成器以创建训练样本,python,generator,Python,Generator,当我阅读《Python深度学习》一书时,我找不到终端代码,while条件总是正确的。我想知道如何获得训练样本。非常感谢你帮助我 # Listing 6.33 Generator yielding timeseries samples and their targets def generator(data, lookback, delay, min_index, max_index, shuffle=False, batch_size=128, step=6):
# Listing 6.33 Generator yielding timeseries samples and their targets
def generator(data, lookback, delay, min_index, max_index,
shuffle=False, batch_size=128, step=6):
if max_index is None:
max_index = len(data) - delay - 1
i = min_index + lookback
k=0
while 1:
if shuffle:
k+=1
print('k=', k)
rows = np.random.randint(
min_index + lookback, max_index, size=batch_size)
else:
if i + batch_size >= max_index:
i = min_index + lookback
rows = np.arange(i, min(i + batch_size, max_index))
i += len(rows)
samples = np.zeros((len(rows),
lookback // step,
data.shape[-1]))
targets = np.zeros((len(rows),))
for j, row in enumerate(rows):
indices = range(rows[j] - lookback, rows[j], step)
samples[j] = data[indices]
targets[j] = data[rows[j] + delay][1]
yield samples, targets
# Listing 6.34 Preparing the training, validation, and test generators
lookback = 1440
step = 6
delay = 144
batch_size = 128
train_gen = generator(float_data, lookback=lookback, delay=delay, min_index=0, max_index=200000, shuffle=True, step=step, batch_size=batch_size)