gpt4 book ai didi

python-3.x - 'EarlyStopping' 对象没有属性 'on_train_batch_begin'

转载 作者:行者123 更新时间:2023-12-04 02:14:30 27 4
gpt4 key购买 nike

我想在我的模型训练时保存最好的检查点,但回调没有按我预期的那样工作。根据 Saving best model in Keras这段代码应该可以工作。

model = Sequential()
model.add(Conv1D(filters=32, kernel_size=8, input_shape=(X_train.shape[1], 4)))
model.add(MaxPooling1D(pool_size=4))
model.add(Flatten())
model.add(Dense(16, activation='relu'))
model.add(Dense(2, activation='softmax'))

model.compile(loss='binary_crossentropy', optimizer='adam',
metrics=['accuracy'])
model.summary()

stop = EarlyStopping(monitor='val_loss', patience=15, verbose=1, mode='min')
save = ModelCheckpoint('./my_model.hdf5', save_best_only=True, monitor='val_loss', mode='min')
reduce_lr = ReduceLROnPlateau(monitor='val_loss', factor=0.1, patience=10, verbose=1, epsilon=1e-4, mode='min')

history = model.fit(X_train, y_train, epochs=25, verbose=0, callbacks=[stop, save, reduce_lr], validation_split=0.25)

但是它不断给我以下错误:
AttributeError                            Traceback (most recent call last)
<ipython-input-28-f86f439eae5a> in <module>()
17 reduce_lr_loss = ReduceLROnPlateau(monitor='val_loss', factor=0.1, patience=7, verbose=1, epsilon=1e-4, mode='min')
18
---> 19 history = model.fit(X_train, y_train, batch_size=batch_size, epochs=50, verbose=0, callbacks=[earlyStopping, mcp_save, reduce_lr_loss], validation_split=0.25)
20
21

/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/training.py in fit(self, x, y, batch_size, epochs, verbose, callbacks, validation_split, validation_data, shuffle, class_weight, sample_weight, initial_epoch, steps_per_epoch, validation_steps, max_queue_size, workers, use_multiprocessing, **kwargs)
878 initial_epoch=initial_epoch,
879 steps_per_epoch=steps_per_epoch,
--> 880 validation_steps=validation_steps)
881
882 def evaluate(self,

/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/training_arrays.py in model_iteration(model, inputs, targets, sample_weights, batch_size, epochs, verbose, callbacks, val_inputs, val_targets, val_sample_weights, shuffle, initial_epoch, steps_per_epoch, validation_steps, mode, validation_in_fit, **kwargs)
323 # Callbacks batch_begin.
324 batch_logs = {'batch': batch_index, 'size': len(batch_ids)}
--> 325 callbacks._call_batch_hook(mode, 'begin', batch_index, batch_logs)
326 progbar.on_batch_begin(batch_index, batch_logs)
327

/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/callbacks.py in _call_batch_hook(self, mode, hook, batch, logs)
194 t_before_callbacks = time.time()
195 for callback in self.callbacks:
--> 196 batch_hook = getattr(callback, hook_name)
197 batch_hook(batch, logs)
198 self._delta_ts[hook_name].append(time.time() - t_before_callbacks)

AttributeError: 'EarlyStopping' object has no attribute 'on_train_batch_begin'

我已成功将此代码用于我的功能模型,但我不确定顺序模型的问题是什么。

最佳答案

从堆栈跟踪中,我注意到您使用的是 tensorflow.keras 但 EarlyStopping from keras (基于您引用的其他答案)。这就是错误的原因。

这应该有效(从 tensorflow keras 导入):

from tensorflow.keras.callbacks import EarlyStopping

关于python-3.x - 'EarlyStopping' 对象没有属性 'on_train_batch_begin',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55112713/

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