我想在训练模型时保存最佳检查点,但是回调无法按我预期的那样工作。根据{{3}},此代码应该有效。
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'
我已成功将此代码用于我的功能模型,但是我不确定顺序模型在这里出了什么问题。
答案 0 :(得分:13)
从堆栈跟踪中,我注意到您正在使用tensorflow.keras,但使用了来自keras的EarlyStopping(基于您引用的其他答案)。这是错误的原因。
这应该可行(从tensorflow keras导入):
from tensorflow.keras.callbacks import EarlyStopping
答案 1 :(得分:0)
如果要使用Keras的所有功能,则不能使用Tensorflow 2.0。 Keras集成不完整。
pip install --upgrade "tensorflow==1.4" "keras>=2.0"