亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

tensorflow模型的save與restore,及checkpoint中讀取變量方式

 更新時(shí)間:2020年05月26日 09:33:13   作者:J_______ll  
這篇文章主要介紹了tensorflow模型的save與restore,及checkpoint中讀取變量方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧

創(chuàng)建一個(gè)NN

import tensorflow as tf
import numpy as np

#fake data
x = np.linspace(-1, 1, 100)[:, np.newaxis] #shape(100,1)
noise = np.random.normal(0, 0.1, size=x.shape)
y = np.power(x, 2) + noise  #shape(100,1) + noise
tf_x = tf.placeholder(tf.float32, x.shape) #input x
tf_y = tf.placeholder(tf.float32, y.shape) #output y
l = tf.layers.dense(tf_x, 10, tf.nn.relu) #hidden layer
o = tf.layers.dense(l, 1)     #output layer
loss = tf.losses.mean_squared_error(tf_y, o ) #compute loss
train_op = tf.train.GradientDescentOptimizer(learning_rate=0.5).minimize(loss)

1.使用save對(duì)模型進(jìn)行保存

sess= tf.Session()
sess.run(tf.global_variables_initializer())  #initialize var in graph
saver = tf.train.Saver() # define a saver for saving and restoring
for step in range(100):   #train
 sess.run(train_op,{tf_x:x, tf_y:y})
saver.save(sess, 'params/params.ckpt', write_meta_graph=False) # mate_graph is not recommend

生成三個(gè)文件,分別是checkpoint,.ckpt.data-00000-of-00001,.ckpt.index

2.使用restore對(duì)提取模型

在提取模型時(shí),需要將模型結(jié)構(gòu)再定義一遍,再將各參數(shù)加載出來

#bulid entire net again and restore
tf_x = tf.placeholder(tf.float32, x.shape)
tf_y = tf.placeholder(tf.float32, y.shape)
l_ = tf.layers.dense(tf_x, 10, tf.nn.relu)
o_ = tf.layers.dense(l_, 1)
loss_ = tf.losses.mean_squared_error(tf_y, o_)
 
sess = tf.Session()
# don't need to initialize variables, just restoring trained variables
saver = tf.train.Saver() # define a saver for saving and restoring
saver.restore(sess, './params/params.ckpt')

3.有時(shí)會(huì)報(bào)錯(cuò)Not found:b1 not found in checkpoint

這時(shí)我們想知道我在文件中到底保存了什么內(nèi)容,即需要讀取出checkpoint中的tensor

import os
from tensorflow.python import pywrap_tensorflow
checkpoint_path = os.path.join('params','params.ckpt')
# Read data from checkpoint file
reader = pywrap_tensorflow.NewCheckpointReader(checkpoint_path)
var_to_shape_map = reader.get_variable_to_shape_map()
# Print tensor name and value
f = open('params.txt','w')
for key in var_to_shape_map: # write tensors' names and values in file
 print(key,file=f)
 print(reader.get_tensor(key),file=f)
f.close()

運(yùn)行后生成一個(gè)params.txt文件,在其中可以看到模型的參數(shù)。

補(bǔ)充知識(shí):TensorFlow按時(shí)間保存檢查點(diǎn)

一 實(shí)例

介紹一種更簡(jiǎn)便地保存檢查點(diǎn)功能的方法——tf.train.MonitoredTrainingSession函數(shù),該函數(shù)可以直接實(shí)現(xiàn)保存及載入檢查點(diǎn)模型的文件。

演示使用MonitoredTrainingSession函數(shù)來自動(dòng)管理檢查點(diǎn)文件。

二 代碼

import tensorflow as tf
tf.reset_default_graph()
global_step = tf.train.get_or_create_global_step()
step = tf.assign_add(global_step, 1)
with tf.train.MonitoredTrainingSession(checkpoint_dir='log/checkpoints',save_checkpoint_secs = 2) as sess:
 print(sess.run([global_step]))
 while not sess.should_stop():
  i = sess.run( step)
  print( i)

三 運(yùn)行結(jié)果

1 第一次運(yùn)行后,會(huì)發(fā)現(xiàn)log文件夾下產(chǎn)生如下文件

2 第二次運(yùn)行后,結(jié)果如下:

INFO:tensorflow:Create CheckpointSaverHook.
INFO:tensorflow:Restoring parameters from log/checkpoints\model.ckpt-15147
INFO:tensorflow:Saving checkpoints for 15147 into log/checkpoints\model.ckpt.
[15147]
15148
15149
15150
15151
15152
15153
15154
15155
15156
15157
15158
15159

四 說明

本例是按照訓(xùn)練時(shí)間來保存的。通過指定save_checkpoint_secs參數(shù)的具體秒數(shù),來設(shè)置每訓(xùn)練多久保存一次檢查點(diǎn)。

可見程序自動(dòng)載入檢查點(diǎn)是從第15147次開始運(yùn)行的。

五 注意

1 如果不設(shè)置save_checkpoint_secs參數(shù),默認(rèn)的保存時(shí)間是10分鐘,這種按照時(shí)間保存的模式更適合用于使用大型數(shù)據(jù)集來訓(xùn)練復(fù)雜模型的情況。

2 使用該方法,必須要定義global_step變量,否則會(huì)報(bào)錯(cuò)誤。

以上這篇tensorflow模型的save與restore,及checkpoint中讀取變量方式就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • Python?List計(jì)算列表平方的9種常見方法

    Python?List計(jì)算列表平方的9種常見方法

    平方操作是指將一個(gè)數(shù)值乘以自身,即計(jì)算數(shù)值的平方,這篇文章主要給大家介紹了關(guān)于Python?List計(jì)算列表平方的9種常見方法,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2024-03-03
  • opencv-python 提取sift特征并匹配的實(shí)例

    opencv-python 提取sift特征并匹配的實(shí)例

    今天小編就為大家分享一篇opencv-python 提取sift特征并匹配的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2019-12-12
  • 用map函數(shù)來完成Python并行任務(wù)的簡(jiǎn)單示例

    用map函數(shù)來完成Python并行任務(wù)的簡(jiǎn)單示例

    這篇文章主要介紹了用map函數(shù)來完成Python并行任務(wù)的簡(jiǎn)單示例,多線程和多進(jìn)程編程的問題一直都是Python中的熱點(diǎn)和難點(diǎn),需要的朋友可以參考下
    2015-04-04
  • Python類的繼承與多態(tài)詳細(xì)介紹

    Python類的繼承與多態(tài)詳細(xì)介紹

    大家好,本篇文章主要講的是Python類的繼承與多態(tài)詳細(xì)介紹,感興趣的同學(xué)趕快來看一看吧,對(duì)你有幫助的話記得收藏一下,方便下次瀏覽
    2022-01-01
  • CentOS中升級(jí)Python版本的方法詳解

    CentOS中升級(jí)Python版本的方法詳解

    本文給大家分享的是再centos系統(tǒng)中將Python版本從2.6升級(jí)到2.7的方法和升級(jí)過程中遇到的問題的處理,非常詳細(xì),有需要的小伙伴可以參考下
    2017-07-07
  • python裝飾器代替set get方法實(shí)例

    python裝飾器代替set get方法實(shí)例

    今天小編就為大家分享一篇python裝飾器代替set get方法實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2019-12-12
  • Pandas中Series和DataFrame的索引實(shí)現(xiàn)

    Pandas中Series和DataFrame的索引實(shí)現(xiàn)

    這篇文章主要介紹了Pandas中Series和DataFrame的索引實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-06-06
  • Python 確定多項(xiàng)式擬合/回歸的階數(shù)實(shí)例

    Python 確定多項(xiàng)式擬合/回歸的階數(shù)實(shí)例

    今天小編就為大家分享一篇Python 確定多項(xiàng)式擬合/回歸的階數(shù)實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2018-12-12
  • python Zmail模塊簡(jiǎn)介與使用示例

    python Zmail模塊簡(jiǎn)介與使用示例

    這篇文章主要介紹了python Zmail模塊簡(jiǎn)介與使用示例,幫助大家利用python收發(fā)郵件,感興趣的朋友可以了解下
    2020-12-12
  • Python定時(shí)器實(shí)例代碼

    Python定時(shí)器實(shí)例代碼

    這篇文章主要介紹了Python定時(shí)器實(shí)例代碼,向大家分享了兩部分代碼示例,一個(gè)是通過線程實(shí)現(xiàn)定時(shí)器timer,另一個(gè)是Python實(shí)現(xiàn)的精度可調(diào)的定時(shí)器實(shí)例,具有一定參考價(jià)值,需要的朋友可以了解下。
    2017-11-11

最新評(píng)論