Keras 中的时期和批次控制

Epochs and batches control in Keras

我想实现一个自动编码器模型,其作用如下:

for epoch in xrange(100):
  for X_batch in batch_list:
     model.train_on_batch(X_batch, X_batch)
     training_error = model.evaluate(X_batch, X_batch, verbose=0)
  "average the training error by the number of the batches considered"
  "save it as the epoch training error"
  "call the function to get the validation error in the same fashion over  the validation data"
  "compare the two errors and decide whether go on training or stopping"

环顾四周 fit_generator 似乎是一种选择,但我不明白如何使用它。 我应该改用 train_on_batch 还是 fit 只有一个时期来正确拟合模型?

这种情况的最佳做法是什么?

据我了解,您想使用验证错误作为提前停止标准。好消息是keras已经有了提前停止回调。所以你需要做的就是创建一个回调并在 epochs/iterations.

之后的训练期间调用它
keras.callbacks.EarlyStopping(monitor='val_loss', min_delta=0, patience=0, verbose=0, mode='auto', baseline=None, restore_best_weights=False)

让我们看看train_on_batch和fit()

train_on_batch(x, y, sample_weight=None, class_weight=None)


fit(x=None, y=None, batch_size=None, epochs=1, verbose=1, callbacks=None, validation_split=0.0, validation_data=None, shuffle=True, class_weight=None, sample_weight=None, initial_epoch=0, steps_per_epoch=None, validation_steps=None)

你可以看到train_on_batch没有任何回调作为输入,所以这里使用fit是个不错的选择,除非你想自己实现。

现在你可以调用fit如下

callbacks = [EarlyStopping(monitor='val_loss', patience=2),
         ModelCheckpoint(filepath='path to latest ckpt', monitor='val_loss', save_best_only=True)]

history = model.fit(train_features,train_target, epochs=num_epochs, callbacks=callbacks, verbose=0, batch_size=your_choice, validation_data)