tnf
tnf

Reputation: 303

How to detect the epoch where Keras earlyStopping occurred?

I am training a neural network with Keras. I set num_epochs to a high number and let EarlyStopping terminate training.

model = Sequential()
model.add(Dense(1, input_shape=(nFeatures,), activation='linear'))
model.compile(optimizer='rmsprop', loss='mse', metrics=['mse', 'mae'])

early_stopping_monitor = EarlyStopping(monitor='val_loss', patience=15, verbose=1, mode='auto')

checkpointer = ModelCheckpoint(filepath = fname_saveWeights, verbose=1, save_best_only=True)

seqModel = model.fit(X_train, y_train, batch_size=4, epochs=num_epochs, validation_data=(X_test, y_test), shuffle=True, callbacks=[early_stopping_monitor, checkpointer], verbose=2)

This works fine. However, I then attempt to plot the loss function:

val_loss   = seqModel.history['val_loss']
xc         = range(num_epochs)
plt.figure()
plt.plot(xc, val_loss)
plt.show()

I am attempting to plot the range of num-epochs (xc) but EarlyStopping ends much earlier, so I have an error in shapes.

How can I detect at what epoch EarlyStopping ended to solve the mismatch?

Verbose setting prints the ending epoch to screen, but I cannot determine how to access the value to use in the plot.

Upvotes: 7

Views: 5407

Answers (1)

nuric
nuric

Reputation: 11225

It is set (code) as a field inside the callback:

early_stopping_monitor.stopped_epoch

will give you the epoch it stopped at after training or 0 if it didn't early stop.

Upvotes: 14

Related Questions