在上次建立的模型基礎上,加入callbacks機制
分別實現了:模型參數保存、早停和動態學習率衰減
import tensorflow as tf
fashion = tf.keras.datasets.fashion_mnist
(x_train, y_train),(x_test, y_test) = fashion.load_data()
x_train, x_test = x_train / 255.0, x_test / 255.0
#函數模型
inputs = tf.keras.Input(shape=(28,28))
x = tf.keras.layers.Flatten()(inputs)
x = tf.keras.layers.Dense(128,activation='relu')(x)
predictions = tf.keras.layers.Dense(10,activation='softmax')(x)
model = tf.keras.Model(inputs,predictions)
model.compile(optimizer='adam',
loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=False),
metrics=['sparse_categorical_accuracy'])
callbacks = [tf.keras.callbacks.ModelCheckpoint( # 保存訓練權重
filepath='weights.BP.hdf5',
save_best_only = True , # 僅保存最優權重
verbose=1,
mode='val_acc',
save_weights_only = True),
tf.keras.callbacks.EarlyStopping( # 滿足條件提前停止訓練
monitor='val_loss', # 監控的指標
min_delta=1e-2, # 低於則結束
patience=3, # 超過設置輪數loss不減則結束
verbose=1),
tf.keras.callbacks.ReduceLROnPlateau( # 動態學習率衰減
monitor='val_acc',
verbose=1,
model='max',
factor=0.8, # 衰減因子
patience=3)]
model.fit(x_train, y_train, batch_size=32, epochs=20, validation_data=(x_test, y_test), callbacks=callbacks,shuffle=True ,validation_freq=1)
model.summary()
運行結果如下:
並沒有完成預先設置的epoch,在滿足條件的情況下提前結束
在今後預測過程中,不再需要重新訓練模型,直接調用保存好的最優模型參數:
model_best = model()
model_best.load_weights("weights.BP.hdf5")
model_best.predict(待預測數據)