import keras import numpy as np keras.utils.set_random_seed(42) x_train = np.linspace(-1.0, 1.0, 120, dtype="float32").reshape(-1, 1) y_train = (2.0 * x_train) + 0.5 x_val = np.linspace(-0.8, 0.8, 40, dtype="float32").reshape(-1, 1) y_val = (2.0 * x_val) + 0.5 model = keras.Sequential( [ keras.layers.Input(shape=(1,)), keras.layers.Dense(8, activation="relu"), keras.layers.Dense(1), ] ) model.compile( optimizer=keras.optimizers.SGD(learning_rate=0.35), loss="mse", metrics=["mae"], ) early_stop = keras.callbacks.EarlyStopping( monitor="val_loss", mode="min", min_delta=0.0001, patience=2, restore_best_weights=True, verbose=1, ) history = model.fit( x_train, y_train, validation_data=(x_val, y_val), epochs=20, batch_size=16, callbacks=[early_stop], verbose=0, ) best_epoch = int(np.argmin(history.history["val_loss"])) + 1 best_val_loss = min(history.history["val_loss"]) restored_val_loss, restored_val_mae = model.evaluate(x_val, y_val, verbose=0) print("epochs requested: 20") print(f"epochs run: {len(history.history['loss'])}") print(f"best epoch: {best_epoch}") print(f"best val_loss: {best_val_loss:.4f}") print(f"restored val_loss: {restored_val_loss:.4f}") print(f"history keys: {sorted(history.history.keys())}")