作者: Rick Chao, Francois Chollet
建立日期 2019/03/20
上次修改日期 2023/06/25
描述: 撰寫新的 Keras 回呼函數的完整指南。
回呼函數是在訓練、評估或推論期間客製化 Keras 模型行為的強大工具。範例包括 keras.callbacks.TensorBoard
,可使用 TensorBoard 可視化訓練進度和結果,或是 keras.callbacks.ModelCheckpoint
,可在訓練期間定期儲存您的模型。
在本指南中,您將學習什麼是 Keras 回呼函數、它可以做什麼,以及如何建立自己的回呼函數。我們提供了一些簡單回呼應用程式的示範,讓您開始使用。
import numpy as np
import keras
所有回呼函數都是 keras.callbacks.Callback
類的子類別,並覆寫在訓練、測試和預測的各個階段呼叫的一組方法。回呼函數可用於在訓練期間檢視模型的內部狀態和統計資料。
您可以將回呼函數的列表 (作為關鍵字引數 callbacks
) 傳遞至下列模型方法
keras.Model.fit()
keras.Model.evaluate()
keras.Model.predict()
on_(train|test|predict)_begin(self, logs=None)
在 fit
/evaluate
/predict
開始時呼叫。
on_(train|test|predict)_end(self, logs=None)
在 fit
/evaluate
/predict
結束時呼叫。
on_(train|test|predict)_batch_begin(self, batch, logs=None)
在訓練/測試/預測期間處理批次之前立即呼叫。
on_(train|test|predict)_batch_end(self, batch, logs=None)
在訓練/測試/預測批次結束時呼叫。在此方法中,logs
是一個包含指標結果的字典。
on_epoch_begin(self, epoch, logs=None)
在訓練期間的週期開始時呼叫。
on_epoch_end(self, epoch, logs=None)
在訓練期間的週期結束時呼叫。
讓我們來看一個具體的範例。首先,讓我們匯入 tensorflow 並定義一個簡單的循序 Keras 模型
# Define the Keras model to add callbacks to
def get_model():
model = keras.Sequential()
model.add(keras.layers.Dense(1))
model.compile(
optimizer=keras.optimizers.RMSprop(learning_rate=0.1),
loss="mean_squared_error",
metrics=["mean_absolute_error"],
)
return model
然後,從 Keras 資料集 API 載入 MNIST 資料進行訓練和測試
# Load example MNIST data and pre-process it
(x_train, y_train), (x_test, y_test) = keras.datasets.mnist.load_data()
x_train = x_train.reshape(-1, 784).astype("float32") / 255.0
x_test = x_test.reshape(-1, 784).astype("float32") / 255.0
# Limit the data to 1000 samples
x_train = x_train[:1000]
y_train = y_train[:1000]
x_test = x_test[:1000]
y_test = y_test[:1000]
現在,定義一個簡單的自訂回呼函數,以記錄
fit
/evaluate
/predict
開始和結束時class CustomCallback(keras.callbacks.Callback):
def on_train_begin(self, logs=None):
keys = list(logs.keys())
print("Starting training; got log keys: {}".format(keys))
def on_train_end(self, logs=None):
keys = list(logs.keys())
print("Stop training; got log keys: {}".format(keys))
def on_epoch_begin(self, epoch, logs=None):
keys = list(logs.keys())
print("Start epoch {} of training; got log keys: {}".format(epoch, keys))
def on_epoch_end(self, epoch, logs=None):
keys = list(logs.keys())
print("End epoch {} of training; got log keys: {}".format(epoch, keys))
def on_test_begin(self, logs=None):
keys = list(logs.keys())
print("Start testing; got log keys: {}".format(keys))
def on_test_end(self, logs=None):
keys = list(logs.keys())
print("Stop testing; got log keys: {}".format(keys))
def on_predict_begin(self, logs=None):
keys = list(logs.keys())
print("Start predicting; got log keys: {}".format(keys))
def on_predict_end(self, logs=None):
keys = list(logs.keys())
print("Stop predicting; got log keys: {}".format(keys))
def on_train_batch_begin(self, batch, logs=None):
keys = list(logs.keys())
print("...Training: start of batch {}; got log keys: {}".format(batch, keys))
def on_train_batch_end(self, batch, logs=None):
keys = list(logs.keys())
print("...Training: end of batch {}; got log keys: {}".format(batch, keys))
def on_test_batch_begin(self, batch, logs=None):
keys = list(logs.keys())
print("...Evaluating: start of batch {}; got log keys: {}".format(batch, keys))
def on_test_batch_end(self, batch, logs=None):
keys = list(logs.keys())
print("...Evaluating: end of batch {}; got log keys: {}".format(batch, keys))
def on_predict_batch_begin(self, batch, logs=None):
keys = list(logs.keys())
print("...Predicting: start of batch {}; got log keys: {}".format(batch, keys))
def on_predict_batch_end(self, batch, logs=None):
keys = list(logs.keys())
print("...Predicting: end of batch {}; got log keys: {}".format(batch, keys))
讓我們試試看
model = get_model()
model.fit(
x_train,
y_train,
batch_size=128,
epochs=1,
verbose=0,
validation_split=0.5,
callbacks=[CustomCallback()],
)
res = model.evaluate(
x_test, y_test, batch_size=128, verbose=0, callbacks=[CustomCallback()]
)
res = model.predict(x_test, batch_size=128, callbacks=[CustomCallback()])
Starting training; got log keys: []
Start epoch 0 of training; got log keys: []
...Training: start of batch 0; got log keys: []
...Training: end of batch 0; got log keys: ['loss', 'mean_absolute_error']
...Training: start of batch 1; got log keys: []
...Training: end of batch 1; got log keys: ['loss', 'mean_absolute_error']
...Training: start of batch 2; got log keys: []
...Training: end of batch 2; got log keys: ['loss', 'mean_absolute_error']
...Training: start of batch 3; got log keys: []
...Training: end of batch 3; got log keys: ['loss', 'mean_absolute_error']
Start testing; got log keys: []
...Evaluating: start of batch 0; got log keys: []
...Evaluating: end of batch 0; got log keys: ['loss', 'mean_absolute_error']
...Evaluating: start of batch 1; got log keys: []
...Evaluating: end of batch 1; got log keys: ['loss', 'mean_absolute_error']
...Evaluating: start of batch 2; got log keys: []
...Evaluating: end of batch 2; got log keys: ['loss', 'mean_absolute_error']
...Evaluating: start of batch 3; got log keys: []
...Evaluating: end of batch 3; got log keys: ['loss', 'mean_absolute_error']
Stop testing; got log keys: ['loss', 'mean_absolute_error']
End epoch 0 of training; got log keys: ['loss', 'mean_absolute_error', 'val_loss', 'val_mean_absolute_error']
Stop training; got log keys: ['loss', 'mean_absolute_error', 'val_loss', 'val_mean_absolute_error']
Start testing; got log keys: []
...Evaluating: start of batch 0; got log keys: []
...Evaluating: end of batch 0; got log keys: ['loss', 'mean_absolute_error']
...Evaluating: start of batch 1; got log keys: []
...Evaluating: end of batch 1; got log keys: ['loss', 'mean_absolute_error']
...Evaluating: start of batch 2; got log keys: []
...Evaluating: end of batch 2; got log keys: ['loss', 'mean_absolute_error']
...Evaluating: start of batch 3; got log keys: []
...Evaluating: end of batch 3; got log keys: ['loss', 'mean_absolute_error']
...Evaluating: start of batch 4; got log keys: []
...Evaluating: end of batch 4; got log keys: ['loss', 'mean_absolute_error']
...Evaluating: start of batch 5; got log keys: []
...Evaluating: end of batch 5; got log keys: ['loss', 'mean_absolute_error']
...Evaluating: start of batch 6; got log keys: []
...Evaluating: end of batch 6; got log keys: ['loss', 'mean_absolute_error']
...Evaluating: start of batch 7; got log keys: []
...Evaluating: end of batch 7; got log keys: ['loss', 'mean_absolute_error']
Stop testing; got log keys: ['loss', 'mean_absolute_error']
Start predicting; got log keys: []
...Predicting: start of batch 0; got log keys: []
...Predicting: end of batch 0; got log keys: ['outputs']
1/8 ━━[37m━━━━━━━━━━━━━━━━━━ 0s 13ms/step...Predicting: start of batch 1; got log keys: []
...Predicting: end of batch 1; got log keys: ['outputs']
...Predicting: start of batch 2; got log keys: []
...Predicting: end of batch 2; got log keys: ['outputs']
...Predicting: start of batch 3; got log keys: []
...Predicting: end of batch 3; got log keys: ['outputs']
...Predicting: start of batch 4; got log keys: []
...Predicting: end of batch 4; got log keys: ['outputs']
...Predicting: start of batch 5; got log keys: []
...Predicting: end of batch 5; got log keys: ['outputs']
...Predicting: start of batch 6; got log keys: []
...Predicting: end of batch 6; got log keys: ['outputs']
...Predicting: start of batch 7; got log keys: []
...Predicting: end of batch 7; got log keys: ['outputs']
Stop predicting; got log keys: []
8/8 ━━━━━━━━━━━━━━━━━━━━ 0s 5ms/step
logs
字典的用法logs
字典包含在批次或週期結束時的損失值和所有指標。範例包括損失和平均絕對誤差。
class LossAndErrorPrintingCallback(keras.callbacks.Callback):
def on_train_batch_end(self, batch, logs=None):
print(
"Up to batch {}, the average loss is {:7.2f}.".format(batch, logs["loss"])
)
def on_test_batch_end(self, batch, logs=None):
print(
"Up to batch {}, the average loss is {:7.2f}.".format(batch, logs["loss"])
)
def on_epoch_end(self, epoch, logs=None):
print(
"The average loss for epoch {} is {:7.2f} "
"and mean absolute error is {:7.2f}.".format(
epoch, logs["loss"], logs["mean_absolute_error"]
)
)
model = get_model()
model.fit(
x_train,
y_train,
batch_size=128,
epochs=2,
verbose=0,
callbacks=[LossAndErrorPrintingCallback()],
)
res = model.evaluate(
x_test,
y_test,
batch_size=128,
verbose=0,
callbacks=[LossAndErrorPrintingCallback()],
)
Up to batch 0, the average loss is 29.25.
Up to batch 1, the average loss is 485.36.
Up to batch 2, the average loss is 330.94.
Up to batch 3, the average loss is 250.62.
Up to batch 4, the average loss is 202.20.
Up to batch 5, the average loss is 169.51.
Up to batch 6, the average loss is 145.98.
Up to batch 7, the average loss is 128.48.
The average loss for epoch 0 is 128.48 and mean absolute error is 6.01.
Up to batch 0, the average loss is 5.10.
Up to batch 1, the average loss is 4.80.
Up to batch 2, the average loss is 4.96.
Up to batch 3, the average loss is 4.96.
Up to batch 4, the average loss is 4.82.
Up to batch 5, the average loss is 4.69.
Up to batch 6, the average loss is 4.51.
Up to batch 7, the average loss is 4.53.
The average loss for epoch 1 is 4.53 and mean absolute error is 1.72.
Up to batch 0, the average loss is 5.08.
Up to batch 1, the average loss is 4.66.
Up to batch 2, the average loss is 4.64.
Up to batch 3, the average loss is 4.72.
Up to batch 4, the average loss is 4.82.
Up to batch 5, the average loss is 4.83.
Up to batch 6, the average loss is 4.77.
Up to batch 7, the average loss is 4.72.
self.model
屬性的用法除了在呼叫其中一個方法時接收記錄資訊外,回呼函數還可存取與目前訓練/評估/推論回合相關聯的模型:self.model
。
以下是您可以使用回呼函數中的 self.model
執行的一些操作
self.model.stop_training = True
以立即中斷訓練。self.model.optimizer
),例如 self.model.optimizer.learning_rate
。model.predict()
輸出,以便在訓練期間用作健全性檢查。讓我們在幾個範例中看看實際運作情況。
第一個範例顯示建立一個 Callback
,當損失達到最小值時,透過設定屬性 self.model.stop_training
(布林值) 來停止訓練。您也可以選擇性地提供引數 patience
,以指定在達到局部最小值後我們應該等待多少個週期才停止。
keras.callbacks.EarlyStopping
提供更完整和通用的實作。
class EarlyStoppingAtMinLoss(keras.callbacks.Callback):
"""Stop training when the loss is at its min, i.e. the loss stops decreasing.
Arguments:
patience: Number of epochs to wait after min has been hit. After this
number of no improvement, training stops.
"""
def __init__(self, patience=0):
super().__init__()
self.patience = patience
# best_weights to store the weights at which the minimum loss occurs.
self.best_weights = None
def on_train_begin(self, logs=None):
# The number of epoch it has waited when loss is no longer minimum.
self.wait = 0
# The epoch the training stops at.
self.stopped_epoch = 0
# Initialize the best as infinity.
self.best = np.inf
def on_epoch_end(self, epoch, logs=None):
current = logs.get("loss")
if np.less(current, self.best):
self.best = current
self.wait = 0
# Record the best weights if current results is better (less).
self.best_weights = self.model.get_weights()
else:
self.wait += 1
if self.wait >= self.patience:
self.stopped_epoch = epoch
self.model.stop_training = True
print("Restoring model weights from the end of the best epoch.")
self.model.set_weights(self.best_weights)
def on_train_end(self, logs=None):
if self.stopped_epoch > 0:
print(f"Epoch {self.stopped_epoch + 1}: early stopping")
model = get_model()
model.fit(
x_train,
y_train,
batch_size=64,
epochs=30,
verbose=0,
callbacks=[LossAndErrorPrintingCallback(), EarlyStoppingAtMinLoss()],
)
Up to batch 0, the average loss is 25.57.
Up to batch 1, the average loss is 471.66.
Up to batch 2, the average loss is 322.55.
Up to batch 3, the average loss is 243.88.
Up to batch 4, the average loss is 196.53.
Up to batch 5, the average loss is 165.02.
Up to batch 6, the average loss is 142.34.
Up to batch 7, the average loss is 125.17.
Up to batch 8, the average loss is 111.83.
Up to batch 9, the average loss is 101.35.
Up to batch 10, the average loss is 92.60.
Up to batch 11, the average loss is 85.16.
Up to batch 12, the average loss is 79.02.
Up to batch 13, the average loss is 73.71.
Up to batch 14, the average loss is 69.23.
Up to batch 15, the average loss is 65.26.
The average loss for epoch 0 is 65.26 and mean absolute error is 3.89.
Up to batch 0, the average loss is 3.92.
Up to batch 1, the average loss is 4.34.
Up to batch 2, the average loss is 5.39.
Up to batch 3, the average loss is 6.58.
Up to batch 4, the average loss is 10.55.
Up to batch 5, the average loss is 19.29.
Up to batch 6, the average loss is 31.58.
Up to batch 7, the average loss is 38.20.
Up to batch 8, the average loss is 41.96.
Up to batch 9, the average loss is 41.30.
Up to batch 10, the average loss is 39.31.
Up to batch 11, the average loss is 37.09.
Up to batch 12, the average loss is 35.08.
Up to batch 13, the average loss is 33.27.
Up to batch 14, the average loss is 31.54.
Up to batch 15, the average loss is 30.00.
The average loss for epoch 1 is 30.00 and mean absolute error is 4.23.
Up to batch 0, the average loss is 5.70.
Up to batch 1, the average loss is 6.90.
Up to batch 2, the average loss is 7.74.
Up to batch 3, the average loss is 8.85.
Up to batch 4, the average loss is 12.53.
Up to batch 5, the average loss is 21.55.
Up to batch 6, the average loss is 35.70.
Up to batch 7, the average loss is 44.16.
Up to batch 8, the average loss is 44.82.
Up to batch 9, the average loss is 43.07.
Up to batch 10, the average loss is 40.51.
Up to batch 11, the average loss is 38.44.
Up to batch 12, the average loss is 36.69.
Up to batch 13, the average loss is 34.77.
Up to batch 14, the average loss is 32.97.
Up to batch 15, the average loss is 31.32.
The average loss for epoch 2 is 31.32 and mean absolute error is 4.39.
Restoring model weights from the end of the best epoch.
Epoch 3: early stopping
<keras.src.callbacks.history.History at 0x1187b7430>
在此範例中,我們示範如何使用自訂的回呼函數,在訓練過程中動態變更最佳化工具的學習率。
如需更多通用的實作,請參閱 callbacks.LearningRateScheduler
。
class CustomLearningRateScheduler(keras.callbacks.Callback):
"""Learning rate scheduler which sets the learning rate according to schedule.
Arguments:
schedule: a function that takes an epoch index
(integer, indexed from 0) and current learning rate
as inputs and returns a new learning rate as output (float).
"""
def __init__(self, schedule):
super().__init__()
self.schedule = schedule
def on_epoch_begin(self, epoch, logs=None):
if not hasattr(self.model.optimizer, "learning_rate"):
raise ValueError('Optimizer must have a "learning_rate" attribute.')
# Get the current learning rate from model's optimizer.
lr = self.model.optimizer.learning_rate
# Call schedule function to get the scheduled learning rate.
scheduled_lr = self.schedule(epoch, lr)
# Set the value back to the optimizer before this epoch starts
self.model.optimizer.learning_rate = scheduled_lr
print(f"\nEpoch {epoch}: Learning rate is {float(np.array(scheduled_lr))}.")
LR_SCHEDULE = [
# (epoch to start, learning rate) tuples
(3, 0.05),
(6, 0.01),
(9, 0.005),
(12, 0.001),
]
def lr_schedule(epoch, lr):
"""Helper function to retrieve the scheduled learning rate based on epoch."""
if epoch < LR_SCHEDULE[0][0] or epoch > LR_SCHEDULE[-1][0]:
return lr
for i in range(len(LR_SCHEDULE)):
if epoch == LR_SCHEDULE[i][0]:
return LR_SCHEDULE[i][1]
return lr
model = get_model()
model.fit(
x_train,
y_train,
batch_size=64,
epochs=15,
verbose=0,
callbacks=[
LossAndErrorPrintingCallback(),
CustomLearningRateScheduler(lr_schedule),
],
)
Epoch 0: Learning rate is 0.10000000149011612.
Up to batch 0, the average loss is 27.90.
Up to batch 1, the average loss is 439.49.
Up to batch 2, the average loss is 302.08.
Up to batch 3, the average loss is 228.83.
Up to batch 4, the average loss is 184.97.
Up to batch 5, the average loss is 155.25.
Up to batch 6, the average loss is 134.03.
Up to batch 7, the average loss is 118.29.
Up to batch 8, the average loss is 105.65.
Up to batch 9, the average loss is 95.53.
Up to batch 10, the average loss is 87.25.
Up to batch 11, the average loss is 80.33.
Up to batch 12, the average loss is 74.48.
Up to batch 13, the average loss is 69.46.
Up to batch 14, the average loss is 65.05.
Up to batch 15, the average loss is 61.31.
The average loss for epoch 0 is 61.31 and mean absolute error is 3.85.
Epoch 1: Learning rate is 0.10000000149011612.
Up to batch 0, the average loss is 57.96.
Up to batch 1, the average loss is 55.11.
Up to batch 2, the average loss is 52.81.
Up to batch 3, the average loss is 51.06.
Up to batch 4, the average loss is 50.58.
Up to batch 5, the average loss is 51.49.
Up to batch 6, the average loss is 53.24.
Up to batch 7, the average loss is 54.20.
Up to batch 8, the average loss is 54.39.
Up to batch 9, the average loss is 54.31.
Up to batch 10, the average loss is 53.83.
Up to batch 11, the average loss is 52.93.
Up to batch 12, the average loss is 51.73.
Up to batch 13, the average loss is 50.34.
Up to batch 14, the average loss is 48.94.
Up to batch 15, the average loss is 47.65.
The average loss for epoch 1 is 47.65 and mean absolute error is 4.30.
Epoch 2: Learning rate is 0.10000000149011612.
Up to batch 0, the average loss is 46.38.
Up to batch 1, the average loss is 45.16.
Up to batch 2, the average loss is 44.03.
Up to batch 3, the average loss is 43.11.
Up to batch 4, the average loss is 42.52.
Up to batch 5, the average loss is 42.32.
Up to batch 6, the average loss is 43.06.
Up to batch 7, the average loss is 44.58.
Up to batch 8, the average loss is 45.33.
Up to batch 9, the average loss is 45.15.
Up to batch 10, the average loss is 44.59.
Up to batch 11, the average loss is 43.88.
Up to batch 12, the average loss is 43.17.
Up to batch 13, the average loss is 42.40.
Up to batch 14, the average loss is 41.74.
Up to batch 15, the average loss is 41.19.
The average loss for epoch 2 is 41.19 and mean absolute error is 4.27.
Epoch 3: Learning rate is 0.05.
Up to batch 0, the average loss is 40.85.
Up to batch 1, the average loss is 40.11.
Up to batch 2, the average loss is 39.38.
Up to batch 3, the average loss is 38.69.
Up to batch 4, the average loss is 38.01.
Up to batch 5, the average loss is 37.38.
Up to batch 6, the average loss is 36.77.
Up to batch 7, the average loss is 36.18.
Up to batch 8, the average loss is 35.61.
Up to batch 9, the average loss is 35.08.
Up to batch 10, the average loss is 34.54.
Up to batch 11, the average loss is 34.04.
Up to batch 12, the average loss is 33.56.
Up to batch 13, the average loss is 33.08.
Up to batch 14, the average loss is 32.64.
Up to batch 15, the average loss is 32.25.
The average loss for epoch 3 is 32.25 and mean absolute error is 3.64.
Epoch 4: Learning rate is 0.05000000074505806.
Up to batch 0, the average loss is 31.83.
Up to batch 1, the average loss is 31.42.
Up to batch 2, the average loss is 31.05.
Up to batch 3, the average loss is 30.72.
Up to batch 4, the average loss is 30.49.
Up to batch 5, the average loss is 30.37.
Up to batch 6, the average loss is 30.15.
Up to batch 7, the average loss is 29.94.
Up to batch 8, the average loss is 29.75.
Up to batch 9, the average loss is 29.56.
Up to batch 10, the average loss is 29.27.
Up to batch 11, the average loss is 28.96.
Up to batch 12, the average loss is 28.67.
Up to batch 13, the average loss is 28.39.
Up to batch 14, the average loss is 28.11.
Up to batch 15, the average loss is 27.80.
The average loss for epoch 4 is 27.80 and mean absolute error is 3.43.
Epoch 5: Learning rate is 0.05000000074505806.
Up to batch 0, the average loss is 27.51.
Up to batch 1, the average loss is 27.25.
Up to batch 2, the average loss is 27.05.
Up to batch 3, the average loss is 26.88.
Up to batch 4, the average loss is 26.76.
Up to batch 5, the average loss is 26.60.
Up to batch 6, the average loss is 26.44.
Up to batch 7, the average loss is 26.25.
Up to batch 8, the average loss is 26.08.
Up to batch 9, the average loss is 25.89.
Up to batch 10, the average loss is 25.71.
Up to batch 11, the average loss is 25.48.
Up to batch 12, the average loss is 25.26.
Up to batch 13, the average loss is 25.03.
Up to batch 14, the average loss is 24.81.
Up to batch 15, the average loss is 24.58.
The average loss for epoch 5 is 24.58 and mean absolute error is 3.25.
Epoch 6: Learning rate is 0.01.
Up to batch 0, the average loss is 24.36.
Up to batch 1, the average loss is 24.14.
Up to batch 2, the average loss is 23.93.
Up to batch 3, the average loss is 23.71.
Up to batch 4, the average loss is 23.52.
Up to batch 5, the average loss is 23.32.
Up to batch 6, the average loss is 23.12.
Up to batch 7, the average loss is 22.93.
Up to batch 8, the average loss is 22.74.
Up to batch 9, the average loss is 22.55.
Up to batch 10, the average loss is 22.37.
Up to batch 11, the average loss is 22.19.
Up to batch 12, the average loss is 22.01.
Up to batch 13, the average loss is 21.83.
Up to batch 14, the average loss is 21.67.
Up to batch 15, the average loss is 21.50.
The average loss for epoch 6 is 21.50 and mean absolute error is 2.98.
Epoch 7: Learning rate is 0.009999999776482582.
Up to batch 0, the average loss is 21.33.
Up to batch 1, the average loss is 21.17.
Up to batch 2, the average loss is 21.01.
Up to batch 3, the average loss is 20.85.
Up to batch 4, the average loss is 20.71.
Up to batch 5, the average loss is 20.57.
Up to batch 6, the average loss is 20.41.
Up to batch 7, the average loss is 20.27.
Up to batch 8, the average loss is 20.13.
Up to batch 9, the average loss is 19.98.
Up to batch 10, the average loss is 19.83.
Up to batch 11, the average loss is 19.69.
Up to batch 12, the average loss is 19.57.
Up to batch 13, the average loss is 19.44.
Up to batch 14, the average loss is 19.32.
Up to batch 15, the average loss is 19.19.
The average loss for epoch 7 is 19.19 and mean absolute error is 2.77.
Epoch 8: Learning rate is 0.009999999776482582.
Up to batch 0, the average loss is 19.07.
Up to batch 1, the average loss is 18.95.
Up to batch 2, the average loss is 18.83.
Up to batch 3, the average loss is 18.70.
Up to batch 4, the average loss is 18.58.
Up to batch 5, the average loss is 18.46.
Up to batch 6, the average loss is 18.35.
Up to batch 7, the average loss is 18.24.
Up to batch 8, the average loss is 18.12.
Up to batch 9, the average loss is 18.01.
Up to batch 10, the average loss is 17.90.
Up to batch 11, the average loss is 17.79.
Up to batch 12, the average loss is 17.68.
Up to batch 13, the average loss is 17.58.
Up to batch 14, the average loss is 17.48.
Up to batch 15, the average loss is 17.38.
The average loss for epoch 8 is 17.38 and mean absolute error is 2.61.
Epoch 9: Learning rate is 0.005.
Up to batch 0, the average loss is 17.28.
Up to batch 1, the average loss is 17.18.
Up to batch 2, the average loss is 17.08.
Up to batch 3, the average loss is 16.99.
Up to batch 4, the average loss is 16.90.
Up to batch 5, the average loss is 16.80.
Up to batch 6, the average loss is 16.71.
Up to batch 7, the average loss is 16.62.
Up to batch 8, the average loss is 16.53.
Up to batch 9, the average loss is 16.44.
Up to batch 10, the average loss is 16.35.
Up to batch 11, the average loss is 16.26.
Up to batch 12, the average loss is 16.17.
Up to batch 13, the average loss is 16.09.
Up to batch 14, the average loss is 16.00.
Up to batch 15, the average loss is 15.92.
The average loss for epoch 9 is 15.92 and mean absolute error is 2.48.
Epoch 10: Learning rate is 0.004999999888241291.
Up to batch 0, the average loss is 15.84.
Up to batch 1, the average loss is 15.76.
Up to batch 2, the average loss is 15.68.
Up to batch 3, the average loss is 15.61.
Up to batch 4, the average loss is 15.53.
Up to batch 5, the average loss is 15.45.
Up to batch 6, the average loss is 15.37.
Up to batch 7, the average loss is 15.29.
Up to batch 8, the average loss is 15.23.
Up to batch 9, the average loss is 15.15.
Up to batch 10, the average loss is 15.08.
Up to batch 11, the average loss is 15.00.
Up to batch 12, the average loss is 14.93.
Up to batch 13, the average loss is 14.86.
Up to batch 14, the average loss is 14.79.
Up to batch 15, the average loss is 14.72.
The average loss for epoch 10 is 14.72 and mean absolute error is 2.37.
Epoch 11: Learning rate is 0.004999999888241291.
Up to batch 0, the average loss is 14.65.
Up to batch 1, the average loss is 14.58.
Up to batch 2, the average loss is 14.52.
Up to batch 3, the average loss is 14.45.
Up to batch 4, the average loss is 14.39.
Up to batch 5, the average loss is 14.33.
Up to batch 6, the average loss is 14.26.
Up to batch 7, the average loss is 14.20.
Up to batch 8, the average loss is 14.14.
Up to batch 9, the average loss is 14.08.
Up to batch 10, the average loss is 14.02.
Up to batch 11, the average loss is 13.96.
Up to batch 12, the average loss is 13.90.
Up to batch 13, the average loss is 13.84.
Up to batch 14, the average loss is 13.78.
Up to batch 15, the average loss is 13.72.
The average loss for epoch 11 is 13.72 and mean absolute error is 2.27.
Epoch 12: Learning rate is 0.001.
Up to batch 0, the average loss is 13.67.
Up to batch 1, the average loss is 13.60.
Up to batch 2, the average loss is 13.55.
Up to batch 3, the average loss is 13.49.
Up to batch 4, the average loss is 13.44.
Up to batch 5, the average loss is 13.38.
Up to batch 6, the average loss is 13.33.
Up to batch 7, the average loss is 13.28.
Up to batch 8, the average loss is 13.22.
Up to batch 9, the average loss is 13.17.
Up to batch 10, the average loss is 13.12.
Up to batch 11, the average loss is 13.07.
Up to batch 12, the average loss is 13.02.
Up to batch 13, the average loss is 12.97.
Up to batch 14, the average loss is 12.92.
Up to batch 15, the average loss is 12.87.
The average loss for epoch 12 is 12.87 and mean absolute error is 2.19.
Epoch 13: Learning rate is 0.0010000000474974513.
Up to batch 0, the average loss is 12.82.
Up to batch 1, the average loss is 12.77.
Up to batch 2, the average loss is 12.72.
Up to batch 3, the average loss is 12.68.
Up to batch 4, the average loss is 12.63.
Up to batch 5, the average loss is 12.58.
Up to batch 6, the average loss is 12.53.
Up to batch 7, the average loss is 12.49.
Up to batch 8, the average loss is 12.45.
Up to batch 9, the average loss is 12.40.
Up to batch 10, the average loss is 12.35.
Up to batch 11, the average loss is 12.30.
Up to batch 12, the average loss is 12.26.
Up to batch 13, the average loss is 12.22.
Up to batch 14, the average loss is 12.17.
Up to batch 15, the average loss is 12.13.
The average loss for epoch 13 is 12.13 and mean absolute error is 2.12.
Epoch 14: Learning rate is 0.0010000000474974513.
Up to batch 0, the average loss is 12.09.
Up to batch 1, the average loss is 12.05.
Up to batch 2, the average loss is 12.01.
Up to batch 3, the average loss is 11.97.
Up to batch 4, the average loss is 11.92.
Up to batch 5, the average loss is 11.88.
Up to batch 6, the average loss is 11.84.
Up to batch 7, the average loss is 11.80.
Up to batch 8, the average loss is 11.76.
Up to batch 9, the average loss is 11.72.
Up to batch 10, the average loss is 11.68.
Up to batch 11, the average loss is 11.64.
Up to batch 12, the average loss is 11.60.
Up to batch 13, the average loss is 11.57.
Up to batch 14, the average loss is 11.54.
Up to batch 15, the average loss is 11.50.
The average loss for epoch 14 is 11.50 and mean absolute error is 2.06.
<keras.src.callbacks.history.History at 0x168619c60>
請務必閱讀 API 文件,以查看現有的 Keras 回呼函數。應用程式包括記錄到 CSV、儲存模型、在 TensorBoard 中可視化指標,以及更多功能!