Keras是否具有内置方法来输出(并在以后绘制)单个时期的训练过程中的损耗演化?

使用函数keras.callbacks.History()的常用方法可以为每个时期输出损失。但是,在我的情况下,训练集相当大,因此我将单个纪元传递给了NN。由于我想绘制训练期间训练(和开发人员)损失的演变情况,有没有办法做到这一点?

我目前正在通过将训练集划分为不同的批次,然后在一个时期内对每个训练集依次进行训练,并每次保存模型来解决此问题。但是也许有内置的方法可以做到这一点?

我正在使用TensorFlow后端。

最佳答案

您可以为此使用回调。

使用Keras MNIST CNN example(此处不复制整个代码),并进行以下更改/添加:

from keras.callbacks import Callback

class TestCallback(Callback):
    def __init__(self, test_data):
        self.test_data = test_data

    def on_batch_end(self, batch, logs={}):
        x, y = self.test_data
        loss, acc = self.model.evaluate(x, y, verbose=0)
        print('\nTesting loss: {}, acc: {}\n'.format(loss, acc))

model.fit(x_train, y_train,
          batch_size=batch_size,
          epochs=1,
          verbose=1,
          validation_data=(x_test, y_test),
          callbacks=[TestCallback((x_test, y_test))]
         )

为了评估每个批次末端的测试/验证集,我们得到以下信息:
Train on 60000 samples, validate on 10000 samples
Epoch 1/1

Testing loss: 0.0672039743446745, acc: 0.9781

  128/60000 [..............................] - ETA: 7484s - loss: 0.1450 - acc: 0.9531

/var/venv/DSTL/lib/python3.4/site-packages/keras/callbacks.py:120: UserWarning: Method on_batch_end() is slow compared to the batch update (15.416976). Check your callbacks.
  % delta_t_median)


Testing loss: 0.06644540682602673, acc: 0.9781

  256/60000 [..............................] - ETA: 7476s - loss: 0.1187 - acc: 0.9570

/var/venv/DSTL/lib/python3.4/site-packages/keras/callbacks.py:120: UserWarning: Method on_batch_end() is slow compared to the batch update (15.450395). Check your callbacks.
  % delta_t_median)


Testing loss: 0.06575664376271889, acc: 0.9782

但是,您可能会自己看到,它的严重缺陷是大大降低了的代码速度(并适本地产生了一些相关的警告)。作为折衷方案,如果可以在每批结束时只获得培训成绩,则可以使用略有不同的回调:
class TestCallback2(Callback):
    def __init__(self, test_data):
        self.test_data = test_data

    def on_batch_end(self, batch, logs={}):
        print()  # just a dummy print command

现在的结果(将callbacks=[TestCallback2((x_test, y_test))替换为model.fit())要快得多,但是在每批结束时仅给出训练指标:
Train on 60000 samples, validate on 10000 samples
Epoch 1/1

  128/60000 [..............................] - ETA: 346s - loss: 0.8503 - acc: 0.7188
  256/60000 [..............................] - ETA: 355s - loss: 0.8496 - acc: 0.7109
  384/60000 [..............................] - ETA: 339s - loss: 0.7718 - acc: 0.7396
  [...]

更新

以上所有方法都可以,但由此产生的损失和准确性不会存储在任何地方,因此无法进行绘制;因此,这是另一个回调解决方案,它实际上将指标存储在训练集中:
from keras.callbacks import Callback

class Histories(Callback):

    def on_train_begin(self,logs={}):
        self.losses = []
        self.accuracies = []

    def on_batch_end(self, batch, logs={}):
        self.losses.append(logs.get('loss'))
        self.accuracies.append(logs.get('acc'))


histories = Histories()

model.fit(x_train, y_train,
          batch_size=batch_size,
          epochs=1,
          verbose=1,
          validation_data=(x_test, y_test),
          callbacks=[histories]
         )

这导致在训练过程中每批次末尾的指标分别存储在histories.losseshistories.accuracies中-这是每个指标的前5个条目:
histories.losses[:5]
# [2.3115866, 2.3008101, 2.2479887, 2.1895032, 2.1491694]

histories.accuracies[:5]
# [0.0703125, 0.1484375, 0.1875, 0.296875, 0.359375]

10-08 07:52