課程來源:人工智能實踐: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