深度學習之儲存和讀取tensorflow模型
阿新 • • 發佈:2018-12-05
儲存和讀取 TensorFlow 模型
訓練一個模型的時間很長。但是你一旦關閉了 TensorFlow session,你所有訓練的權重和偏置項都丟失了。如果你計劃在之後重新使用這個模型,你需要重新訓練!
幸運的是,TensorFlow 可以讓你通過一個叫 tf.train.Saver 的類把你的程序儲存下來。這個類可以把任何 tf.Variable 存到你的檔案系統。
儲存變數
讓我們通過一個簡單地例子來儲存 weights 和 bias Tensors。第一個例子你只是存兩個變數,後面會教你如何把一個實際模型的所有權重儲存下來。
import tensorflow as tf # The file path to save the data # 檔案儲存路徑 save_file = './model.ckpt' # Two Tensor Variables: weights and bias # 兩個 Tensor 變數:權重和偏置項 weights = tf.Variable(tf.truncated_normal([2, 3])) bias = tf.Variable(tf.truncated_normal([3])) # Class used to save and/or restore Tensor Variables # 用來存取 Tensor 變數的類 saver = tf.train.Saver() with tf.Session() as sess: # Initialize all the Variables # 初始化所有變數 sess.run(tf.global_variables_initializer()) # Show the values of weights and bias # 顯示變數和權重 print('Weights:') print(sess.run(weights)) print('Bias:') print(sess.run(bias)) # Save the model # 儲存模型 saver.save(sess, save_file)
Weights:
[[-0.97990924 1.03016174 0.74119264]
[-0.82581609 -0.07361362 -0.86653847]]
Bias:
[ 1.62978125 -0.37812829 0.64723819]
weights
和 bias
Tensors。 用 tf.truncated_normal()
函式設定了隨機值。用 tf.train.Saver.save()
函式把這些值被儲存在save_file
位置,命名為 "model.ckpt"
,(".ckpt"
擴充套件名錶示"checkpoint"
)。
如果你使用 TensorFlow 0.11.0RC1 或者更新的版本,還會生成一個包含了 TensorFlow graph 的檔案 "model.ckpt.meta"
載入變數
現在這些變數已經存好了,讓我們把它們載入到新模型裡。
# Remove the previous weights and bias
# 移除之前的權重和偏置項
tf.reset_default_graph()
# Two Variables: weights and bias
# 兩個變數:權重和偏置項
weights = tf.Variable(tf.truncated_normal([2, 3]))
bias = tf.Variable(tf.truncated_normal([3]))
# Class used to save and/or restore Tensor Variables
# 用來存取 Tensor 變數的類
saver = tf.train.Saver()
with tf.Session() as sess:
# Load the weights and bias
# 載入權重和偏置項
saver.restore(sess, save_file)
# Show the values of weights and bias
# 顯示權重和偏置項
print('Weight:')
print(sess.run(weights))
print('Bias:')
print(sess.run(bias))
輸出結果為:
Weights:
[[-0.97990924 1.03016174 0.74119264]
[-0.82581609 -0.07361362 -0.86653847]]
Bias:
[ 1.62978125 -0.37812829 0.64723819]
注意,你依然需要在 Python
中建立 weights
和 bias
Tensors。tf.train.Saver.restore()
函式把之前儲存的資料載入到 weights
和 bias
當中。
因為 tf.train.Saver.restore()
設定了 TensorFlow
變數,這裡你不需要呼叫 tf.global_variables_initializer()
了。
儲存一個訓練好的模型
讓我們看看如何訓練一個模型並儲存它的權重。
訓練一個模型並儲存它的權重
從一個模型開始:
# Remove previous Tensors and Operations
# 移除之前的 Tensors 和運算
tf.reset_default_graph()
from tensorflow.examples.tutorials.mnist import input_data
import numpy as np
learning_rate = 0.001
n_input = 784 # MNIST 資料輸入 (圖片尺寸: 28*28)
n_classes = 10 # MNIST 總計類別 (數字 0-9)
# Import MNIST data
# 載入 MNIST 資料
mnist = input_data.read_data_sets('.', one_hot=True)
# Features and Labels
# 特徵和標籤
features = tf.placeholder(tf.float32, [None, n_input])
labels = tf.placeholder(tf.float32, [None, n_classes])
# Weights & bias
# 權重和偏置項
weights = tf.Variable(tf.random_normal([n_input, n_classes]))
bias = tf.Variable(tf.random_normal([n_classes]))
# Logits - xW + b
logits = tf.add(tf.matmul(features, weights), bias)
# Define loss and optimizer
# 定義損失函式和優化器
cost = tf.reduce_mean(\
tf.nn.softmax_cross_entropy_with_logits(logits=logits, labels=labels))
optimizer = tf.train.GradientDescentOptimizer(learning_rate=learning_rate)\
.minimize(cost)
# Calculate accuracy
# 計算準確率
correct_prediction = tf.equal(tf.argmax(logits, 1), tf.argmax(labels, 1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
讓我們訓練模型並儲存權重:
import math
save_file = './train_model.ckpt'
batch_size = 128
n_epochs = 100
saver = tf.train.Saver()
# Launch the graph
# 啟動圖
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
# Training cycle
# 訓練迴圈
for epoch in range(n_epochs):
total_batch = math.ceil(mnist.train.num_examples / batch_size)
# Loop over all batches
# 遍歷所有 batch
for i in range(total_batch):
batch_features, batch_labels = mnist.train.next_batch(batch_size)
sess.run(
optimizer,
feed_dict={features: batch_features, labels: batch_labels})
# Print status for every 10 epochs
# 每執行10個 epoch 列印一次狀態
if epoch % 10 == 0:
valid_accuracy = sess.run(
accuracy,
feed_dict={
features: mnist.validation.images,
labels: mnist.validation.labels})
print('Epoch {:<3} - Validation Accuracy: {}'.format(
epoch,
valid_accuracy))
# Save the model
# 儲存模型
saver.save(sess, save_file)
print('Trained Model Saved.')
Epoch 0 - Validation Accuracy: 0.06859999895095825
Epoch 10 - Validation Accuracy: 0.20239999890327454
Epoch 20 - Validation Accuracy: 0.36980000138282776
Epoch 30 - Validation Accuracy: 0.48820000886917114
Epoch 40 - Validation Accuracy: 0.5601999759674072
Epoch 50 - Validation Accuracy: 0.6097999811172485
Epoch 60 - Validation Accuracy: 0.6425999999046326
Epoch 70 - Validation Accuracy: 0.6733999848365784
Epoch 80 - Validation Accuracy: 0.6916000247001648
Epoch 90 - Validation Accuracy: 0.7113999724388123
Trained Model Saved.
載入訓練好的模型
讓我們從磁碟中載入權重和偏置項,驗證測試集準確率。
saver = tf.train.Saver()
# Launch the graph
# 載入圖
with tf.Session() as sess:
saver.restore(sess, save_file)
test_accuracy = sess.run(
accuracy,
feed_dict={features: mnist.test.images, labels: mnist.test.labels})
print('Test Accuracy: {}'.format(test_accuracy))
Test Accuracy: 0.7229999899864197
就是這樣!你現在知道如何儲存再載入一個 TensorFlow
的訓練模型了。下一章節讓我們看看如何把權重和偏置項載入到修改過的模型中。