编程技术网

关注微信公众号,定时推送前沿、专业、深度的编程技术资料。

 找回密码
 立即注册

QQ登录

只需一步,快速开始

极客时间

创建一个Keras回调,每n个周期激活一次:Creating a Keras Callback that activates every n epochs

Klaas Kabini keras 2022-5-10 11:36 11人围观

腾讯云服务器
创建一个Keras回调,每n个周期激活一次的处理方法

我想在Keras中创建自己的Callback,但我不太了解该怎么做.

I want to create my own Callback in Keras but don't really understand how to do it.

我想做的是创建一个回调,每个 n (例如 n = 10 )时代都调用一个函数.根据Keras文档( https://keras.io/callbacks/),基类keras.callbacks.Callback 具有属性 params .

What I want to do is to create a Callback that every n (e.g. n=10) epochs calls a function. According to the Keras documentation (https://keras.io/callbacks/) the base class keras.callbacks.Callback has a property params.

此属性是否包括当前纪元?如果是这样,如何使用/调用它?

Does this property include the current epoch? And if so, how can it be used/called?

或者是否还有其他好的方法来每隔 n 个纪元调用一个函数?

Or is there any other good way to call a function every n epochs?

我想到了一个回调,当使用参数 period = 10 时,它的作用类似于 keras.callbacks.ModelCheckpoint .

I had a Callback in mind that works similar as keras.callbacks.ModelCheckpoint when using the argument period=10.

我们将非常感谢您的帮助.谢谢:)

Help would be really appreciated. Thank you :)

经过一番阅读后,我想到了这一点,它似乎可以正常工作(仍然需要对其进行正确的测试)

After quite some reading I came up with this and it seems to work (still needs to test it properly)

class Test1(callbacks.Callback): def on_epoch_end(self, epochs, logs={}) if epochs == 10: print('abc') #a random function callb = Test1() model = networks.compute_network(layers=layers, batch_size=batch_size, epochs=epochs, call_list=[callb]) # compute_network in my case loads all the data, trains the network, and then returns it 

一个网站特别帮助我更好地了解了回调: https://keunwoochoi.wordpress.com/2016/07/16/keras-callbacks/

One site helped me in particular to understand Callbacks better: https://keunwoochoi.wordpress.com/2016/07/16/keras-callbacks/

问题解答

正如@HMK所说, on_epoch_begin on_epoch_end 方法提供了当前纪元.例如,要每隔10个时间应用一个函数,您可以像这样编辑自定义回调:

As @HMK said, on_epoch_begin and on_epoch_end methods provide the current epoch. For example, to apply a function every 10th epochs you can edit your custom callback like that:

class myCallback(keras.callbacks.Callback): def on_epoch_end(self, epoch, logs=None) if epoch % 10 == 0: your_func() 

这篇关于创建一个Keras回调,每n个周期激活一次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程技术网(www.editcode.net)!

腾讯云服务器 阿里云服务器
关注微信
^