如何在Keras中创建自定义回调? [英] How can I create a custom callback in Keras?

查看:74
本文介绍了如何在Keras中创建自定义回调?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有兴趣在安装我的keras模型的同时创建一个回调.更详细地讲,我想在每个时期结束时从带有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 parameter in classifier.fit() but many callbacks are prebuilted by keras and I don't know hot 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆