Keras 回调实例没有属性“set_model”

Muf*_*eed 4 python callback keras

我正在尝试使用 keras 的回调功能记录 Keras 中使用的每个时代的状态。这是回调类的示例代码

class TimingCallback():
    def __init__(self):
        self.logs=[]
    def on_epoch_begin(epoch, logs={}):
        self.starttime=time()
    def on_epoch_end(epoch, logs={}):
        self.logs.append(time()-self.starttime)
Run Code Online (Sandbox Code Playgroud)

这是我的模特合身。

cb = TimingCallback()
model.fit(X, Y, epochs=150, batch_size=10, callbacks=[cb])
Run Code Online (Sandbox Code Playgroud)

执行时出现以下错误。

错误:

AttributeError: TimingCallback 实例没有属性“set_model”

谁能帮我弄清楚为什么会发生这种情况?

Yu-*_*ang 5

set_model中定义的方法keras.callbacks.Callback。要编写自定义回调,您必须将keras.callbacks.Callback. 否则,您的回调将缺少 Keras 内部使用的一些必要方法。

将第一行更改为下一行应该可以工作。

class TimingCallback(keras.callbacks.Callback):
Run Code Online (Sandbox Code Playgroud)