如何在 Keras 中创建自定义回调? [英] How can I create a custom callback in Keras?
本文介绍了如何在 Keras 中创建自定义回调?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有兴趣在拟合我的 keras 模型时创建回调.更详细地说,我想在每次 epoch 结束时从带有 val_acc 的机器人电报中收到一条消息.我知道你可以在 classifier.fit()
中添加一个 callback_list 作为参数,但是许多回调是由 keras 预先构建的,我不知道如何添加自定义的.
I'm interested in creating a callback while fitting my keras model. More in detail I'd like to receive a message from a bot telegram with val_acc each time an epoch is over. I know you can add a callback_list as a parameter in classifier.fit()
but many callbacks are prebuilt by keras and I don't know how to add a custom one.
谢谢!
推荐答案
以下是我如何向回调添加验证准确性的示例:
Here is an example of how I would add validation accuracy to a callback:
class AccuracyHistory(keras.callbacks.Callback):
def on_train_begin(self, logs={}):
self.acc = []
def on_epoch_end(self, batch, logs={}):
self.acc.append(logs.get('val_acc'))
history = AccuracyHistory()
model.fit(x, y,
...
callbacks=[history])
这篇关于如何在 Keras 中创建自定义回调?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文