激情久久久_欧美视频区_成人av免费_不卡视频一二三区_欧美精品在欧美一区二区少妇_欧美一区二区三区的

腳本之家,腳本語言編程技術(shù)及教程分享平臺!
分類導(dǎo)航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|

服務(wù)器之家 - 腳本之家 - Python - tensorflow模型保存、加載之變量重命名實(shí)例

tensorflow模型保存、加載之變量重命名實(shí)例

2020-04-05 12:26* star * Python

今天小編就為大家分享一篇tensorflow模型保存、加載之變量重命名實(shí)例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧

話不多說,干就完了。

變量重命名的用處?

簡單定義:簡單來說就是將模型A中的參數(shù)parameter_A賦給模型B中的parameter_B

使用場景:當(dāng)需要使用已經(jīng)訓(xùn)練好的模型參數(shù),尤其是使用別人訓(xùn)練好的模型參數(shù)時,往往別人模型中的參數(shù)命名方式與自己當(dāng)前的命名方式不同,所以在加載模型參數(shù)時需要對參數(shù)進(jìn)行重命名,使得代碼更簡潔易懂。

實(shí)現(xiàn)方法:

1)、模型保存

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
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)、模型加載(變量名稱保持不變)

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
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)、模型加載(變量重命名)

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
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)

總結(jié):

# 之前模型中叫 'weights'的變量賦值給當(dāng)前的conv1_w變量

# 之前模型中叫 'biases' 的變量賦值給當(dāng)前的conv1_b變量

# 之前模型中叫 'weights_2'的變量賦值給當(dāng)前的conv2_w變量

saver = tf.train.Saver({

"weights": conv1_w,

"biases": conv1_b,

"weights_2": conv2_w

})

以上這篇tensorflow模型保存、加載之變量重命名實(shí)例就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持服務(wù)器之家。

原文鏈接:https://blog.csdn.net/cxx654/article/details/88927962

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 免费国产在线精品 | 国产日韩在线视频 | 蜜桃视频在线观看视频 | 黄色特级一级片 | 国产交换3p国产精品 | 免费毛片视频 | 最新中文字幕第一页视频 | 国产精品久久久久久久久久东京 | 亚洲精品久久久久久久久久久 | 9999久久| 欧美日韩在线视频一区二区 | 国产一区二区在线观看视频 | 天天草夜夜骑 | 伊人99re | 一级毛片免费一级 | 叉逼视频| 国产一级淫片在线观看 | sm高h视频| 国产免费网站视频 | 久色乳综合思思在线视频 | av影院在线播放 | 国产精品久久久久久久久久 | 中国国语毛片免费观看视频 | 夜夜b| 欧美精品久久久久久久久久 | 欧美999| 黄视频网站免费 | 久久久久国产视频 | 成人精品视频在线 | 福利在线小视频 | 日韩欧美电影一区二区三区 | 免费在线观看成年人视频 | 久久人人做 | 欧美www| 午夜天堂在线 | 加勒比综合 | qyl在线视频精品免费观看 | 圆产精品久久久久久久久久久 | 久久亚洲第一 | 在线成人精品视频 | 久久久久成人网 |