课程来源:人工智能实践:Tensorflow笔记2
文章目录
- 前言
- 断点续训主要步骤
- 参数提取主要步骤
- 总结
前言
本讲目标:断点续训,存取最优模型;保存可训练参数至文本
断点续训主要步骤
读取模型:
先定义出存放模型的路径和文件名,命名为.ckpt文件。
生成ckpt文件的时候会同步生成索引表,所以通过判断是否存在索引表来知晓是不是已经保存过模型参数。
如果有了索引表就利用load_weights函数读取已经保存的模型参数。
code:
checkpoint_save_path = "./checkpoint/fashion.ckpt"
if os.path.exists(checkpoint_save_path + '.index'):print('-------------load the model-----------------')model.load_weights(checkpoint_save_path)
保存模型:
保存模型参数可以使用TensorFlow给出的回调函数,直接保存训练出来的模型参数
tf.keras.callbacks.ModelCheckpoint( filepath=路径文件名(文件存储路径),
save_weights_only=True/False,(是否只保留参数模型)
save_best_only=True/False(是否只保留最优结果)) 执行训练过程中时,加入callbacks选项:
history=model.fit(callbacks=[cp_callback])
code:
cp_callback = tf.keras.callbacks.ModelCheckpoint(filepath=checkpoint_save_path,save_weights_only=True,save_best_only=True)history = model.fit(x_train, y_train, batch_size=32, epochs=5, validation_data=(x_test, y_test), validation_freq=1,callbacks=[cp_callback])
第一次运行:
第二次运行:可以发现模型并不是从初始训练,而是在基于保存的模型开始训练的(这一点可以从准确率和损失看出):
全部代码:
import tensorflow as tf
import osfashion = 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.0model = 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.SparseCategoricalCrossentropy(from_logits=False),metrics=['sparse_categorical_accuracy'])checkpoint_save_path = "./checkpoint/fashion.ckpt"
if os.path.exists(checkpoint_save_path + '.index'):print('-------------load the model-----------------')model.load_weights(checkpoint_save_path)cp_callback = tf.keras.callbacks.ModelCheckpoint(filepath=checkpoint_save_path,save_weights_only=True,save_best_only=True)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()
参数提取主要步骤
设置打印的格式,使所有参数都打印出来
np.set_printoptions(threshold=np.inf)
print(model.trainable_variables)
将所有可训练参数存入文本:
file = open('./weights.txt', 'w')
for v in model.trainable_variables:file.write(str(v.name) + '\n')file.write(str(v.shape) + '\n')file.write(str(v.numpy()) + '\n')
file.close()
完整代码:
import tensorflow as tf
import os
import numpy as npnp.set_printoptions(threshold=np.inf)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.0model = 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.SparseCategoricalCrossentropy(from_logits=False),metrics=['sparse_categorical_accuracy'])checkpoint_save_path = "./checkpoint/fashion.ckpt"
if os.path.exists(checkpoint_save_path + '.index'):print('-------------load the model-----------------')model.load_weights(checkpoint_save_path)cp_callback = tf.keras.callbacks.ModelCheckpoint(filepath=checkpoint_save_path,save_weights_only=True,save_best_only=True)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()print(model.trainable_variables)
file = open('./weights.txt', 'w')
for v in model.trainable_variables:file.write(str(v.name) + '\n')file.write(str(v.shape) + '\n')file.write(str(v.numpy()) + '\n')
file.close()
效果:
总结
课程链接:MOOC人工智能实践:TensorFlow笔记2