您好,登錄后才能下訂單哦!
話不多說,干就完了。
變量重命名的用處?
簡單定義:簡單來說就是將模型A中的參數parameter_A賦給模型B中的parameter_B
使用場景:當需要使用已經訓練好的模型參數,尤其是使用別人訓練好的模型參數時,往往別人模型中的參數命名方式與自己當前的命名方式不同,所以在加載模型參數時需要對參數進行重命名,使得代碼更簡潔易懂。
實現方法:
1)、模型保存
import os import tensorflow as tf weights = tf.Variable(initial_value=tf.truncated_normal(shape=[1024, 2], mean=0.0, stddev=0.1), dtype=tf.float32, name="weights") biases = tf.Variable(initial_value=tf.zeros(shape=[2]), dtype=tf.float32, name="biases") weights_2 = tf.Variable(initial_value=weights.initialized_value(), dtype=tf.float32, name="weights_2") # saver checkpoint if os.path.exists("checkpoints") is False: os.makedirs("checkpoints") saver = tf.train.Saver() with tf.Session() as sess: init_op = [tf.global_variables_initializer()] sess.run(init_op) saver.save(sess=sess, save_path="checkpoints/variable.ckpt")
2)、模型加載(變量名稱保持不變)
import tensorflow as tf from matplotlib import pyplot as plt import os current_path = os.path.dirname(os.path.abspath(__file__)) def restore_variable(sess): # need not initilize variable, but need to define the same variable like checkpoint weights = tf.Variable(initial_value=tf.truncated_normal(shape=[1024, 2], mean=0.0, stddev=0.1), dtype=tf.float32, name="weights") biases = tf.Variable(initial_value=tf.zeros(shape=[2]), dtype=tf.float32, name="biases") weights_2 = tf.Variable(initial_value=weights.initialized_value(), dtype=tf.float32, name="weights_2") saver = tf.train.Saver() ckpt_path = os.path.join(current_path, "checkpoints", "variable.ckpt") saver.restore(sess=sess, save_path=ckpt_path) weights_val, weights_2_val = sess.run( [ tf.reshape(weights, shape=[2048]), tf.reshape(weights_2, shape=[2048]) ] ) plt.subplot(1, 2, 1) plt.scatter([i for i in range(len(weights_val))], weights_val) plt.subplot(1, 2, 2) plt.scatter([i for i in range(len(weights_2_val))], weights_2_val) plt.show() if __name__ == '__main__': with tf.Session() as sess: restore_variable(sess)
3)、模型加載(變量重命名)
import tensorflow as tf from matplotlib import pyplot as plt import os current_path = os.path.dirname(os.path.abspath(__file__)) def restore_variable_renamed(sess): conv1_w = tf.Variable(initial_value=tf.truncated_normal(shape=[1024, 2], mean=0.0, stddev=0.1), dtype=tf.float32, name="conv1_w") conv1_b = tf.Variable(initial_value=tf.zeros(shape=[2]), dtype=tf.float32, name="conv1_b") conv2_w = tf.Variable(initial_value=conv1_w.initialized_value(), dtype=tf.float32, name="conv2_w") # variable named 'weights' in ckpt assigned to current variable conv1_w # variable named 'biases' in ckpt assigned to current variable conv1_b # variable named 'weights_2' in ckpt assigned to current variable conv2_w saver = tf.train.Saver({ "weights": conv1_w, "biases": conv1_b, "weights_2": conv2_w }) ckpt_path = os.path.join(current_path, "checkpoints", "variable.ckpt") saver.restore(sess=sess, save_path=ckpt_path) conv1_w__val, conv2_w__val = sess.run( [ tf.reshape(conv1_w, shape=[2048]), tf.reshape(conv2_w, shape=[2048]) ] ) plt.subplot(1, 2, 1) plt.scatter([i for i in range(len(conv1_w__val))], conv1_w__val) plt.subplot(1, 2, 2) plt.scatter([i for i in range(len(conv2_w__val))], conv2_w__val) plt.show() if __name__ == '__main__': with tf.Session() as sess: restore_variable_renamed(sess)
總結:
# 之前模型中叫 'weights'的變量賦值給當前的conv1_w變量
# 之前模型中叫 'biases' 的變量賦值給當前的conv1_b變量
# 之前模型中叫 'weights_2'的變量賦值給當前的conv2_w變量
saver = tf.train.Saver({
"weights": conv1_w,
"biases": conv1_b,
"weights_2": conv2_w
})
以上這篇tensorflow模型保存、加載之變量重命名實例就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持億速云。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。