如何绘制精度与 Keras 处理时间的函数关系?

How can I plot the accuracy as a function of the processing time in Keras?

我一直在 Ke​​ras 中训练 CNN 并将训练和验证准确度绘制为时期的函数。我想知道是否有一种方法可以将精度绘制为处理时间的函数。

原因是我想展示迁移学习的速度,而不是重新训练一个完整的网络。当使用迁移学习时,网络需要相似数量的 epoch 来训练、给予或接受,但每个 epoch 花费的时间要少得多(快一个数量级),我想以图形方式捕捉这一点。

这是我目前使用的代码:

history = model.fit(X_train, Y_train, batch_size=batch_size, nb_epoch=nb_epoch, verbose=1, validation_data=(X_test, Y_test))

print(history.history.keys())
# summarize history for accuracy
plt.plot(history.history['acc'])
plt.plot(history.history['val_acc'])
plt.title('model accuracy')
plt.ylabel('accuracy')
plt.xlabel('epoch')
plt.legend(['train', 'test'], loc='lower right')
plt.show()

因此,在 Keras 中实现一段代码来执行您的任务实际上非常简单。为了做到这一点 - 熟悉 keras.callback 是件好事。可以调用自定义函数:

on_epoch_begin:在每个纪元开始时调用。

on_epoch_end:在每个纪元结束时调用,

on_batch_begin:在每批开始时调用,

on_batch_end:在每批结束时调用,

on_train_begin:模型训练开始时调用,

on_train_end: 模型训练结束时调用

所以现在您可以实施例如一个新的回调将:

  • 注册训练开始时间on_train_begin,
  • 在每个时期结束时,它将使用 on_epoch_end 在提供的 dict 中注册计算结束的实际时间,
  • 注册训练结束on_train_end.

通过使用此回调收集的数据,您可以轻松地以多种方式呈现时间和准确性之间的依赖关系。当然 - 它可以很容易地扩展到 batch/iterations 个时间段。