web-dev-qa-db-ja.com

「EarlyStopping」オブジェクトには属性「on_train_batch_begin」がありません

モデルのトレーニング中に最適なチェックポイントを保存したいのですが、コールバックが期待どおりに機能しません。 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'

このコードを機能モデルにうまく使用しましたが、シーケンシャルモデルの問題は何なのかわかりません。

8
grapefruit

Kerasのすべての機能を使用したい場合、Tensorflow 2.0は使用できません。 Kerasの統合は不完全です。

pip install --upgrade "tensorflow==1.4" "keras>=2.0"
0
hobs