欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

 更新時間:2020年01月21日 10:22:18   作者:* star *  
今天小編就為大家分享一篇tensorflow模型保存、加載之變量重命名實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧

話不多說,干就完了。

變量重命名的用處?

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

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

實現(xiàn)方法:

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模型保存、加載之變量重命名實例就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關文章

  • 基于進程內通訊的python聊天室實現(xiàn)方法

    基于進程內通訊的python聊天室實現(xiàn)方法

    這篇文章主要介紹了基于進程內通訊的python聊天室實現(xiàn)方法,實例分析了Python聊天室的相關實現(xiàn)技巧,需要的朋友可以參考下
    2015-06-06
  • python裝飾器原理源碼示例分析

    python裝飾器原理源碼示例分析

    本文由淺入深介紹了python的裝飾器,并且通過代碼展現(xiàn)了如何自己手寫裝飾器函數(shù)和類裝飾器,有需要的朋友可以參考下,希望可以有所幫助
    2021-09-09
  • python猜數(shù)字小游戲實現(xiàn)代碼

    python猜數(shù)字小游戲實現(xiàn)代碼

    大家好,本篇文章主要講的是python猜數(shù)字小游戲實現(xiàn)代碼,感興趣的同學趕快來看一看吧,對你有幫助的話記得收藏一下,方便下次瀏覽
    2022-01-01
  • 基于pytorch實現(xiàn)對圖片進行數(shù)據(jù)增強

    基于pytorch實現(xiàn)對圖片進行數(shù)據(jù)增強

    圖像數(shù)據(jù)增強是一種在訓練機器學習和深度學習模型時常用的策略,尤其是在計算機視覺領域,具體而言,它通過創(chuàng)建和原始圖像稍有不同的新圖像來擴大訓練集,本文給大家介紹了如何基于pytorch實現(xiàn)對圖片進行數(shù)據(jù)增強,需要的朋友可以參考下
    2024-01-01
  • Blueprint實現(xiàn)路由分組及Flask中session的使用詳解

    Blueprint實現(xiàn)路由分組及Flask中session的使用詳解

    這篇文章主要為大家介紹了Blueprint實現(xiàn)路由分組及Flask中session的使用詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-11-11
  • Python使用xpath對解析內容進行數(shù)據(jù)提取

    Python使用xpath對解析內容進行數(shù)據(jù)提取

    XPath 使用路徑表達式來選取HTML/ XML 文檔中的節(jié)點或節(jié)點集,節(jié)點是通過沿著路徑 (path) 或者步 (steps) 來選取的,本文將給大家介紹Python使用xpath對解析內容進行數(shù)據(jù)提取的方法,需要的朋友可以參考下
    2024-05-05
  • PyQt4實現(xiàn)下拉菜單可供選擇并打印出來

    PyQt4實現(xiàn)下拉菜單可供選擇并打印出來

    這篇文章主要為大家詳細介紹了PyQt4實現(xiàn)下拉菜單可供選擇并打印出來,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-04-04
  • Python實戰(zhàn)項目之MySQL tkinter pyinstaller實現(xiàn)學生管理系統(tǒng)

    Python實戰(zhàn)項目之MySQL tkinter pyinstaller實現(xiàn)學生管理系統(tǒng)

    讀萬卷書不如行萬里路,只學書上的理論是遠遠不夠的,只有在實戰(zhàn)中才能獲得能力的提升,本篇文章手把手帶你用MySQL、tkinter、 pyinstaller實現(xiàn)一個學生管理系統(tǒng),大家可以通過案例查缺補漏,提升水平
    2021-10-10
  • Python中的heapq模塊解析

    Python中的heapq模塊解析

    這篇文章主要介紹了Python中的heapq模塊解析,heapq模塊是Python標準庫中的一個模塊,用于實現(xiàn)堆隊列(heapq)數(shù)據(jù)結構,它提供了一種方便的方式來實現(xiàn)堆排序等算法,需要的朋友可以參考下
    2023-09-09
  • python中xlrd模塊的使用詳解

    python中xlrd模塊的使用詳解

    這篇文章主要介紹了python中xlrd模塊的使用詳解,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2021-02-02

最新評論