我正在使用Keras自定义数据生成器来训练自动编码器。数据足够大,无法容纳到内存中。
生成器:
class Mygenerator(Sequence):
def __init__(self, x_set, y_set, batch_size):
self.x, self.y = x_set, y_set
self.batch_size = batch_size
def __len__(self):
return int(np.ceil(len(self.x) / float(self.batch_size)))
def __getitem__(self, idx):
batch_x = self.x[idx * self.batch_size:(idx + 1) * self.batch_size]
batch_y = self.y[idx * self.batch_size:(idx + 1) * self.batch_size]
# read your data here using the batch lists, batch_x and batch_y
x = [np.reshape(np.load(filename),(52,52,1)) for filename in batch_x] # load array and reshape to fit input layer
y = [np.reshape(np.load(filename),(52,52,1)) for filename in batch_y] # load array and reshape to fit input layer
return np.array(x), np.array(y)
模型fit_generator:
XTRAINFILES = glob.glob("C:\\x_train\\*.npy")
YTRAINFILES = glob.glob("C:\\y_train\\*.npy")
XTESTFILES = glob.glob("C:\\x_test\\*.npy")
YTESTFILES = glob.glob("C:\\y_test\\*.npy")
autoencoder_model.fit_generator(Mygenerator(XTRAINFILES, YTRAINFILES, 128),
epochs=EPOCHES, workers=8, steps_per_epoch=ceil( len(XTRAINFILES) / 128)
validation_data=Mygenerator(XTESTFILES, YTESTFILES, 128),
validation_steps=ceil( len(XTESTFILES) / 128),
callbacks=[tb_output, checkpoint_callback])
Keras提供2到3个小时的预计到达时间。在没有定制生成器的情况下进行的测试仅需要少量的数据以适合内存,因此每个时期的ETA为20到30分钟。
关于PC规格的见解
尝试的解决方案::将workers = 8
添加到拟合生成器中,虽然时间有所缩短,但仍与预期的距离不够
答案 0 :(得分:-2)
fit_generator()
函数似乎存在问题,因此您应该使用 fit()
。
您也可以尝试禁用急切执行:
tf.compat.v1.disable_eager_execution()
问题已讨论 here。