import tensorflow as tf
import os
mnist = tf.keras.datasets.mnist
(x_train,y_train),(x_test,y_test) = mnist.load_data()
x_trian,x_test = x_train / 255.0,x_test / 255.0
model = tf.keras.models.Sequential([
tf.keras.layers.Flatten(),
tf.keras.layers.Dense(128,activation='relu'),
tf.keras.layers.Dense(10,activation='softmax')
])
model.compile(
optimizer = 'adam',
loss = tf.keras.losses.CategoricalCrossentropy(from_logits=False),
metrics = ['sparse_categorical_accuracy']
)
checkpoint_save_path = "./checkpoint/mnist.ckpt" #存放模型的路径和文件名
#在生成ckpt文件的同时,也会生成索引表,所以通过判断有没有生成索引表,来判断是不是保存模型参数了
if os.path.exists(checkpoint_save_path + '.index'):
print('-------------load the model-------------------')
model.load_weights(checkpoint_save_path)
#用load_weights函数,来读取文件
#保存模型参数用callbacks函数,
cp_callback = tf.keras.callbacks.ModelCheckpoint(
filepath = checkpoint_save_path,
save_weights_only = True,#是否只保留模型参数
save_best_only = True#是否只保留最优结果
)
#在执行训练时,加入callbacks回调选项,返回给history
history = model.fit(
x_train,y_train,batch_size=32,epochs=5,validation_data=(x_test,y_test),
validation_freq=1,callbacks=[cp_callback]
)
model.summary()
"""
Total params: 101,770
Trainable params: 101,770
Non-trainable params: 0
"""