gpt4 book ai didi

python - 无法将多个回调传递给 keras 模型

转载 作者:行者123 更新时间:2023-12-04 15:25:18 25 4
gpt4 key购买 nike

我正在构建一个 keras LSTM 模型,在第一次通过时我发现它有点过度拟合数据,所以我初始化了 2 个回调 - 一个用于控制可变学习率,另一个用于允许提前停止:

    def _initialise_callback(self):

# Ensure learning rate decreases with the epoch number
learning_rate = 0.1
decay_rate = learning_rate / self.epochs
momentum = 0.8
self.sgd = SGD(lr=learning_rate, momentum=momentum, decay=decay_rate, nesterov=False)

#Allow model to stop early to prevent overfitting
self.early_stopping = EarlyStopping(monitor='loss', patience=3)

但出于某种原因,我似乎无法将它们都传递给 fit() 方法。我所做的是:

    def fit(self):
self.model.fit(self.train_set, epochs=self.epochs, verbose=2, shuffle=False,
callbacks=[self.early_stopping, self.sgd],
use_multiprocessing=False)

这会导致以下错误:


File "<ipython-input-1-1532e4234d2a>", line 1, in <module>
runfile('C:/VULCAN_HOME/sampling_bias/bias_LSTM.py', wdir='C:/VULCAN_HOME/sampling_bias')

File "C:\ProgramData\Anaconda3\lib\site-packages\spyder\utils\site\sitecustomize.py", line 710, in runfile
execfile(filename, namespace)

File "C:\ProgramData\Anaconda3\lib\site-packages\spyder\utils\site\sitecustomize.py", line 101, in execfile
exec(compile(f.read(), filename, 'exec'), namespace)

File "C:/VULCAN_HOME/sampling_bias/bias_LSTM.py", line 174, in <module>
predictor.fit()

File "C:/VULCAN_HOME/sampling_bias/bias_LSTM.py", line 164, in fit
use_multiprocessing=False)

File "C:\ProgramData\Anaconda3\lib\site-packages\keras\engine\training.py", line 1147, in fit
initial_epoch=initial_epoch)

File "C:\ProgramData\Anaconda3\lib\site-packages\keras\legacy\interfaces.py", line 91, in wrapper
return func(*args, **kwargs)

File "C:\ProgramData\Anaconda3\lib\site-packages\keras\engine\training.py", line 1732, in fit_generator
initial_epoch=initial_epoch)

File "C:\ProgramData\Anaconda3\lib\site-packages\keras\engine\training_generator.py", line 100, in fit_generator
callbacks.set_model(callback_model)

File "C:\ProgramData\Anaconda3\lib\site-packages\keras\callbacks\callbacks.py", line 68, in set_model
callback.set_model(model)

AttributeError: 'SGD' object has no attribute 'set_model'

另一方面,如果我尝试仅传递 sgd 或仅传递 early_stopping 那么一切正常。有人知道这里发生了什么吗?

最佳答案

SGD 优化器应作为参数传递给 compile 方法,如图所示 here而不是作为 fit 方法的回调参数。我已经在下面修改了您的代码:

def fit(self):
self.model.fit(self.train_set, epochs=self.epochs, verbose=2, shuffle=False,
callbacks=[self.early_stopping],
use_multiprocessing=False)

当你编译模型时通过你的优化器

self.model.compile(optimizer=self.sgd, **kwargs) 

关于python - 无法将多个回调传递给 keras 模型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62369023/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com