日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 综合教程 >内容正文

综合教程

tensorflow学习笔记(三十四):Saver(保存与加载模型)

發(fā)布時間:2023/12/31 综合教程 17 生活家
生活随笔 收集整理的這篇文章主要介紹了 tensorflow学习笔记(三十四):Saver(保存与加载模型) 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

Saver
tensorflow 中的 Saver 對象是用于 參數(shù)保存和恢復的。如何使用呢?
這里介紹了一些基本的用法。
官網(wǎng)中給出了這么一個例子:

v1 = tf.Variable(..., name='v1')
v2 = tf.Variable(..., name='v2')

# Pass the variables as a dict:
saver = tf.train.Saver({'v1': v1, 'v2': v2})

# Or pass them as a list.
saver = tf.train.Saver([v1, v2])
# Passing a list is equivalent to passing a dict with the variable op names
# as keys:
saver = tf.train.Saver({v.op.name: v for v in [v1, v2]})

#注意,如果不給Saver傳var_list 參數(shù)的話, 他將已 所有可以保存的 variable作為其var_list的值。
1
2
3
4
5
6
7
8
9
10
11
12
13
這里使用了三種不同的方式來創(chuàng)建 saver 對象, 但是它們內(nèi)部的原理是一樣的。我們都知道,參數(shù)會保存到 checkpoint 文件中,通過鍵值對的形式在 checkpoint中存放著。如果 Saver 的構造函數(shù)中傳的是 dict,那么在 save 的時候,checkpoint文件中存放的就是對應的 key-value。如下:

import tensorflow as tf
# Create some variables.
v1 = tf.Variable(1.0, name="v1")
v2 = tf.Variable(2.0, name="v2")

saver = tf.train.Saver({"variable_1":v1, "variable_2": v2})
# Use the saver object normally after that.
with tf.Session() as sess:
tf.global_variables_initializer().run()
saver.save(sess, 'test-ckpt/model-2')
1
2
3
4
5
6
7
8
9
10
我們通過官方提供的工具來看一下 checkpoint 中保存了什么

from tensorflow.python.tools.inspect_checkpoint import print_tensors_in_checkpoint_file

print_tensors_in_checkpoint_file("test-ckpt/model-2", None, True)
# 輸出:
#tensor_name: variable_1
#1.0
#tensor_name: variable_2
#2.0
1
2
3
4
5
6
7
8
如果構建saver對象的時候,我們傳入的是 list, 那么將會用對應 Variable 的 variable.op.name 作為 key。

import tensorflow as tf
# Create some variables.
v1 = tf.Variable(1.0, name="v1")
v2 = tf.Variable(2.0, name="v2")

saver = tf.train.Saver([v1, v2])
# Use the saver object normally after that.
with tf.Session() as sess:
tf.global_variables_initializer().run()
saver.save(sess, 'test-ckpt/model-2')
1
2
3
4
5
6
7
8
9
10
我們再使用官方工具打印出 checkpoint 中的數(shù)據(jù),得到

tensor_name: v1
1.0
tensor_name: v2
2.0
1
2
3
4

如果我們現(xiàn)在想將 checkpoint 中v2的值restore到v1 中,v1的值restore到v2中,我們該怎么做?
這時,我們只能采用基于 dict 的 saver

import tensorflow as tf
# Create some variables.
v1 = tf.Variable(1.0, name="v1")
v2 = tf.Variable(2.0, name="v2")

saver = tf.train.Saver({"variable_1":v1, "variable_2": v2})
# Use the saver object normally after that.
with tf.Session() as sess:
tf.global_variables_initializer().run()
saver.save(sess, 'test-ckpt/model-2')
1
2
3
4
5
6
7
8
9
10
save 部分的代碼如上所示,下面寫 restore 的代碼,和save代碼有點不同。

```python
import tensorflow as tf
# Create some variables.
v1 = tf.Variable(1.0, name="v1")
v2 = tf.Variable(2.0, name="v2")
#restore的時候,variable_1對應到v2,variable_2對應到v1,就可以實現(xiàn)目的了。
saver = tf.train.Saver({"variable_1":v2, "variable_2": v1})
# Use the saver object normally after that.
with tf.Session() as sess:
tf.global_variables_initializer().run()
saver.restore(sess, 'test-ckpt/model-2')
print(sess.run(v1), sess.run(v2))
# 輸出的結果是 2.0 1.0,如我們所望
1
2
3
4
5
6
7
8
9
10
11
12
13
我們發(fā)現(xiàn),其實 創(chuàng)建 saver對象時使用的鍵值對就是表達了一種對應關系:

save時, 表示:variable的值應該保存到 checkpoint文件中的哪個 key下
restore時,表示:checkpoint文件中key對應的值,應該restore到哪個variable
其它
一個快速找到ckpt文件的方式

ckpt = tf.train.get_checkpoint_state(ckpt_dir)
if ckpt and ckpt.model_checkpoint_path:
saver.restore(sess, ckpt.model_checkpoint_path)
1
2
3
參考資料
https://www.tensorflow.org/api_docs/python/tf/train/Saver
---------------------
作者:ke1th
來源:CSDN
原文:https://blog.csdn.net/u012436149/article/details/56665612
版權聲明:本文為博主原創(chuàng)文章,轉載請附上博文鏈接!

總結

以上是生活随笔為你收集整理的tensorflow学习笔记(三十四):Saver(保存与加载模型)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。