# 建立一个简单的模型
model = tf.keras.models.Sequential([
keras.layers.Dense(512, activation='relu', input_shape=(784,)),
keras.layers.Dense(10, activation='softmax')
])
model.compile(optimizer='adam',
loss='sparse_categorical_crossentropy',
metrics=['accuracy'])
model.summary()
Model: "sequential_1"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
dense_24 (Dense) (None, 512) 401920
_________________________________________________________________
dense_25 (Dense) (None, 10) 5130
=================================================================
Total params: 407,050
Trainable params: 407,050
Non-trainable params: 0
_________________________________________________________________
选择保存最佳模型的权重
# 常见一个checkpoint路径
checkpoint_path = 'my_model1.ckpt'
# 创建一个保存模型权重的回调
cp_callback = tf.keras.callback.ModelCheckpoint(
filepath = checkpoint_path,
# monitor = 选择监视的值(val_acc、val_loss、acc、loss)
# save_best_only = True # 当设置为True,将只保留验证集上性能最好的模型
save_weights_only = Ture,
# period = CheckPoint之间的间隔的epoch数
verbose = 1)
# 使用新的回调训练模型
model.fit(train_images, trian_labels, epochs = 10,
validation_data = (test_images, test_labels),
callbacks = [cp_callback]) # 通过回调训练模型
先初始化一个模型去评估(与加载之后的模型形成对比)
# 创建一个没训练过的模型
model_checkpoint = tf.keras.models.Sequential([
keras.layers.Dense(512, activation='relu', input_shape=(784,)),
keras.layers.Dense(10, activation='softmax')
])
model_checkpoint.compile(optimizer='adam',
loss='sparse_categorical_crossentropy',
metrics=['accuracy'])
# 评估没训练过的模型
model_checkpoint.evaluate(test_images, test_labels, verbose =2)
'''
1000/1 - 0s - loss: 2.3669 - accuracy: 0.0540
正确率 5.4%
'''
那我们加载模型再去评估模型
# 加载模型
model_checkpoint.load_weights("my_training\my_model1.ckpt")
model_checkpoint.evaluate(test_images, test_labels, verbose =2)
'''
1000/1 - 0s - loss: 0.4690 - accuracy: 0.8710
正确率;87.1%
'''
# 训练模型
model.fit(train_images, train_labels, epochs = 5)
#保存模型
model.save('my_model2.h5')
# 创建一个没训练过的模型
model_HDF5 = tf.keras.models.Sequential([
keras.layers.Dense(512, activation='relu', input_shape=(784,)),
keras.layers.Dense(10, activation='softmax')
])
model_HDF5.compile(optimizer='adam',
loss='sparse_categorical_crossentropy',
metrics=['accuracy'])
# 评估没训练过的模型
model_HDF5.evaluate(test_images, test_labels, verbose =2)
'''
1000/1 - 0s - loss: 2.3764 - accuracy: 0.0970
正确率:9.7%
'''
那我们加载模型再去评估模型
model_HDF5 = tf.keras.models.load_model('my_model2.h5')
model_HDF5.evaluate(test_images, test_labels, verbose = 2)
'''
1000/1 - 0s - loss: 0.4352 - accuracy: 0.8650
正确率: 86.5%
'''
new_model = tf.keras.models.load_model('my_training\my_model2.h5')
new_model.summary()
Model: "sequential_2"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
dense_46 (Dense) (None, 512) 401920
_________________________________________________________________
dense_47 (Dense) (None, 10) 5130
=================================================================
Total params: 407,050
Trainable params: 407,050
Non-trainable params: 0
_________________________________________________________________