0

我是 TensorFlow 和 keras 的新手。我正在学习使用 python (Chollet) 进行深度学习一书中的教程。也可以在这里找到:https ://www.kaggle.com/pankrzysiu/6-3-1-a-temperature-forecasting-problem

在问题陈述中,我们要预测未来 24 小时的温度。对于数据设置,他定义了以下生成器:

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
    while 1:
        if shuffle:
            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

并创建三个对象:

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)
val_gen = generator(float_data,
                        lookback=lookback,
                        delay=delay,
                        min_index=200001,
                        max_index=300000,
                        step=step,
                        batch_size=batch_size)
test_gen = generator(float_data,
                        lookback=lookback,
                        delay=delay,
                        min_index=300001,
                        max_index=None,
                        step=step,
                        batch_size=batch_size)

根据我对这段代码的理解,变量“rows”的长度等于批量大小。

当生成器创建目标时,它似乎为每个批号创建一个目标。我不明白这段代码如何返回 24 小时的目标值,以及为什么 test_gen 也用 batch_size=batch_size 调用。

如果问题不清楚,请指出。

4

0 回答 0