简体   繁体   English

Keras 仅在使用验证生成器时卡在第一个时期

[英]Keras stuck at first epoch only when using validation generator

I am reading a book on deep learning named Deep learning with python .我正在阅读一本名为Deep learning with python的深度学习书籍。 The book is old in terms of code but I read the official documentation to get through it.这本书在代码方面很旧,但我阅读了官方文档来完成它。 Anyways无论如何

This is a program that is supposed to train a simple model for time series prediction of temperature using dataset available here .这是一个程序,应该训练一个简单的 model 使用此处提供的数据集对温度进行时间序列预测。

The program goes like this程序是这样的

import numpy as np
from keras.models import Sequential
from keras import layers
from keras.optimizers import RMSprop

#Loading file 
f = open(fname) # fname is the filepath for the csv file
data = f.read()
f.close()
lines = data.split('\n')
header = lines[0].split(',')
lines = lines[1:]

# Converting into numpy array
float_data = np.zeros((len(lines), len(header) - 1))
for i, line in enumerate(lines):
values = [float(x) for x in line.split(',')[1:]]
float_data[i, :] = values

# Normalizing the data 
mean = float_data[:200000].mean(axis=0)
float_data -= mean
std = float_data[:200000].std(axis=0)
float_data /= std

There is a generator function for creating the dataset (I've read tensorflow.keras.utils.Sequence is a preferred choice but I failed in converting this generator to Sequence subclass有一个生成器 function 用于创建数据集(我已阅读tensorflow.keras.utils.Sequence是首选,但我未能将此生成器转换为序列子类

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

Here are the parameter details以下是参数详细信息

1) data —The original array of floating-point data, normalized 1) data —浮点数据的原始数组,标准化

2) lookback —How many timesteps back the input data should go. 2) lookback ——go 应该回溯多少时间步。

3) delay —How many timesteps in the future the target should be. 3) delay ——目标应该在未来多少个时间步长。

4) min_index and max_index —Indices in the data array that delimit which timesteps to draw from. 4) min_indexmax_index — 数据数组中的索引,用于界定要从中提取的时间步长。 This is useful for keeping a segment of the data for validation and another for testing.这对于保留一部分数据用于验证和另一部分用于测试很有用。

5) shuffle —Whether to shuffle the samples or draw them in chronological order. 5) shuffle — 是对样本进行洗牌还是按时间顺序绘制。

6) batch_size —The number of samples per batch. 6) batch_size — 每批次的样本数。

7) step —The period, in timesteps, at which you sample data. 7) step — 采样数据的时间段,以时间步长为单位。 You'll set it to 6 in order to draw one data point every hour.您将其设置为 6,以便每小时绘制一个数据点。

And these 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)

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)

val_steps = (300000 - 200001 - lookback)
test_steps = (len(float_data) - 300001 - lookback)

The network layout is as follows网络布局如下

model = Sequential()
model.add(layers.Flatten(input_shape=(lookback // step, float_data.shape[-1])))
model.add(layers.Dense(32, activation='relu'))
model.add(layers.Dense(1))
model.compile(optimizer=RMSprop(), loss='mae')
model.compile(optimizer=RMSprop(), loss='mae')
history = model.fit(train_gen,steps_per_epoch=500,epochs=20,validation_data=test_gen,validation_steps=test_steps)

However the model gets stuck at然而 model 卡在

Train for 500 steps, validate for 119110 steps
Epoch 1/20
497/500 [============================>.] - ETA: 0s - loss: 0.3524

My model gets stuck while evaluating on the validation set.我的 model 在评估验证集时卡住了。 To make sure train_gen and val_gen where okay I tried为了确保train_genval_gen我试过了

next(train_gen)
next(val_gen)

And they both display different values each time like而且它们每次都显示不同的值,例如

(array([[[ 0.34593055,  0.49507501,  0.4628141 , ...,  0.16203687,
           0.18470667,  0.84378526],
         [ 0.36243914,  0.6283707 ,  0.59460993, ...,  0.2921889 ,
           0.94414397,  0.60710086],
         [ 0.35182647,  0.64305582,  0.60912981, ...,  1.78242962,
           1.59631612,  0.43507171],
         ...,

What is wrong here?这里有什么问题?

The number of validation steps looks suspicious, as it is not being calculated with the batch size, so it is larger than it should be which would extend the validation phase in time considerably.验证步骤的数量看起来很可疑,因为它不是用批量大小计算的,所以它比应该的要大,这会大大延长验证阶段的时间。 The solution is to divide the number of steps with the batch size:解决方案是将步数除以批量大小:

val_steps = val_steps // batch_size
test_steps = test_steps // batch_size

This will make steps have the right value.这将使步骤具有正确的价值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM