- ubuntu12.04环境下使用kvm ioctl接口实现最简单的虚拟机
- Ubuntu 通过无线网络安装Ubuntu Server启动系统后连接无线网络的方法
- 在Ubuntu上搭建网桥的方法
- ubuntu 虚拟机上网方式及相关配置详解
CFSDN坚持开源创造价值,我们致力于搭建一个资源共享平台,让每一个IT人在这里找到属于你的精彩世界.
这篇CFSDN的博客文章TensorFlow模型保存和提取的方法由作者收集整理,如果你对这篇文章有兴趣,记得点赞哟.
1、TensorFlow模型保存和提取方法 。
1. TensorFlow通过tf.train.Saver类实现神经网络模型的保存和提取。tf.train.Saver对象saver的save方法将TensorFlow模型保存到指定路径中,saver.save(sess,"Model/model.ckpt") ,实际在这个文件目录下会生成4个人文件:
checkpoint文件保存了一个录下多有的模型文件列表,model.ckpt.meta保存了TensorFlow计算图的结构信息,model.ckpt保存每个变量的取值,此处文件名的写入方式会因不同参数的设置而不同,但加载restore时的文件路径名是以checkpoint文件中的“model_checkpoint_path”值决定的.
2. 加载这个已保存的TensorFlow模型的方法是saver.restore(sess,"./Model/model.ckpt") ,加载模型的代码中也要定义TensorFlow计算图上的所有运算并声明一个tf.train.Saver类,不同的是加载模型时不需要进行变量的初始化,而是将变量的取值通过保存的模型加载进来,注意加载路径的写法。若不希望重复定义计算图上的运算,可直接加载已经持久化的图,saver =tf.train.import_meta_graph("Model/model.ckpt.meta") .
3.tf.train.Saver类也支持在保存和加载时给变量重命名,声明Saver类对象的时候使用一个字典dict重命名变量即可,{"已保存的变量的名称name": 重命名变量名},saver = tf.train.Saver({"v1":u1, "v2": u2})即原来名称name为v1的变量现在加载到变量u1(名称name为other-v1)中.
4. 上一条做的目的之一就是方便使用变量的滑动平均值。如果在加载模型时直接将影子变量映射到变量自身,则在使用训练好的模型时就不需要再调用函数来获取变量的滑动平均值了。载入时,声明Saver类对象时通过一个字典将滑动平均值直接加载到新的变量中,saver = tf.train.Saver({"v/ExponentialMovingAverage": v}),另通过tf.train.ExponentialMovingAverage的variables_to_restore()函数获取变量重命名字典.
此外,通过convert_variables_to_constants函数将计算图中的变量及其取值通过常量的方式保存于一个文件中.
2、TensorFlow程序实现 。
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
|
# 本文件程序为配合教材及学习进度渐进进行,请按照注释分段执行
# 执行时要注意IDE的当前工作过路径,最好每段重启控制器一次,输出结果更准确
# Part1: 通过tf.train.Saver类实现保存和载入神经网络模型
# 执行本段程序时注意当前的工作路径
import
tensorflow as tf
v1
=
tf.Variable(tf.constant(
1.0
, shape
=
[
1
]), name
=
"v1"
)
v2
=
tf.Variable(tf.constant(
2.0
, shape
=
[
1
]), name
=
"v2"
)
result
=
v1
+
v2
saver
=
tf.train.Saver()
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
saver.save(sess,
"Model/model.ckpt"
)
# Part2: 加载TensorFlow模型的方法
import
tensorflow as tf
v1
=
tf.Variable(tf.constant(
1.0
, shape
=
[
1
]), name
=
"v1"
)
v2
=
tf.Variable(tf.constant(
2.0
, shape
=
[
1
]), name
=
"v2"
)
result
=
v1
+
v2
saver
=
tf.train.Saver()
with tf.Session() as sess:
saver.restore(sess,
"./Model/model.ckpt"
)
# 注意此处路径前添加"./"
print
(sess.run(result))
# [ 3.]
# Part3: 若不希望重复定义计算图上的运算,可直接加载已经持久化的图
import
tensorflow as tf
saver
=
tf.train.import_meta_graph(
"Model/model.ckpt.meta"
)
with tf.Session() as sess:
saver.restore(sess,
"./Model/model.ckpt"
)
# 注意路径写法
print
(sess.run(tf.get_default_graph().get_tensor_by_name(
"add:0"
)))
# [ 3.]
# Part4: tf.train.Saver类也支持在保存和加载时给变量重命名
import
tensorflow as tf
# 声明的变量名称name与已保存的模型中的变量名称name不一致
u1
=
tf.Variable(tf.constant(
1.0
, shape
=
[
1
]), name
=
"other-v1"
)
u2
=
tf.Variable(tf.constant(
2.0
, shape
=
[
1
]), name
=
"other-v2"
)
result
=
u1
+
u2
# 若直接生命Saver类对象,会报错变量找不到
# 使用一个字典dict重命名变量即可,{"已保存的变量的名称name": 重命名变量名}
# 原来名称name为v1的变量现在加载到变量u1(名称name为other-v1)中
saver
=
tf.train.Saver({
"v1"
: u1,
"v2"
: u2})
with tf.Session() as sess:
saver.restore(sess,
"./Model/model.ckpt"
)
print
(sess.run(result))
# [ 3.]
# Part5: 保存滑动平均模型
import
tensorflow as tf
v
=
tf.Variable(
0
, dtype
=
tf.float32, name
=
"v"
)
for
variables
in
tf.global_variables():
print
(variables.name)
# v:0
ema
=
tf.train.ExponentialMovingAverage(
0.99
)
maintain_averages_op
=
ema.
apply
(tf.global_variables())
for
variables
in
tf.global_variables():
print
(variables.name)
# v:0
# v/ExponentialMovingAverage:0
saver
=
tf.train.Saver()
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
sess.run(tf.assign(v,
10
))
sess.run(maintain_averages_op)
saver.save(sess,
"Model/model_ema.ckpt"
)
print
(sess.run([v, ema.average(v)]))
# [10.0, 0.099999905]
# Part6: 通过变量重命名直接读取变量的滑动平均值
import
tensorflow as tf
v
=
tf.Variable(
0
, dtype
=
tf.float32, name
=
"v"
)
saver
=
tf.train.Saver({
"v/ExponentialMovingAverage"
: v})
with tf.Session() as sess:
saver.restore(sess,
"./Model/model_ema.ckpt"
)
print
(sess.run(v))
# 0.0999999
# Part7: 通过tf.train.ExponentialMovingAverage的variables_to_restore()函数获取变量重命名字典
import
tensorflow as tf
v
=
tf.Variable(
0
, dtype
=
tf.float32, name
=
"v"
)
# 注意此处的变量名称name一定要与已保存的变量名称一致
ema
=
tf.train.ExponentialMovingAverage(
0.99
)
print
(ema.variables_to_restore())
# {'v/ExponentialMovingAverage': <tf.Variable 'v:0' shape=() dtype=float32_ref>}
# 此处的v取自上面变量v的名称name="v"
saver
=
tf.train.Saver(ema.variables_to_restore())
with tf.Session() as sess:
saver.restore(sess,
"./Model/model_ema.ckpt"
)
print
(sess.run(v))
# 0.0999999
# Part8: 通过convert_variables_to_constants函数将计算图中的变量及其取值通过常量的方式保存于一个文件中
import
tensorflow as tf
from
tensorflow.python.framework
import
graph_util
v1
=
tf.Variable(tf.constant(
1.0
, shape
=
[
1
]), name
=
"v1"
)
v2
=
tf.Variable(tf.constant(
2.0
, shape
=
[
1
]), name
=
"v2"
)
result
=
v1
+
v2
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
# 导出当前计算图的GraphDef部分,即从输入层到输出层的计算过程部分
graph_def
=
tf.get_default_graph().as_graph_def()
output_graph_def
=
graph_util.convert_variables_to_constants(sess,
graph_def, [
'add'
])
with tf.gfile.GFile(
"Model/combined_model.pb"
,
'wb'
) as f:
f.write(output_graph_def.SerializeToString())
# Part9: 载入包含变量及其取值的模型
import
tensorflow as tf
from
tensorflow.python.platform
import
gfile
with tf.Session() as sess:
model_filename
=
"Model/combined_model.pb"
with gfile.FastGFile(model_filename,
'rb'
) as f:
graph_def
=
tf.GraphDef()
graph_def.ParseFromString(f.read())
result
=
tf.import_graph_def(graph_def, return_elements
=
[
"add:0"
])
print
(sess.run(result))
# [array([ 3.], dtype=float32)]
|
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我.
原文链接:http://blog.csdn.net/marsjhao/article/details/72829635 。
最后此篇关于TensorFlow模型保存和提取的方法的文章就讲到这里了,如果你想了解更多关于TensorFlow模型保存和提取的方法的内容请搜索CFSDN的文章或继续浏览相关文章,希望大家以后支持我的博客! 。
我尝试根据表单元素的更改禁用/启用保存按钮。但是,当通过弹出按钮选择更改隐藏输入字段值时,保存按钮不受影响。 下面是我的代码。我正在尝试序列化旧的表单值并与更改后的表单值进行比较。但我猜隐藏的字段值无
我正在尝试保存模型的实例,但我得到了 Invalid EmbeddedDocumentField item (1) 其中 1 是项目的 ID(我认为)。 模型定义为 class Graph(Docum
我有一个非常奇怪的问题......在我的 iPhone 应用程序中,用户可以打开相机胶卷中的图像,在我的示例中 1920 x 1080 像素 (72 dpi) 的壁纸。 现在,想要将图像的宽度调整为例
目前,我正在使用具有排序/过滤功能的数据表成功地从我的数据库中显示图像元数据。在我的数据表下方,我使用第三方图像覆盖流( http://www.jacksasylum.eu/ContentFlow/
我的脚本有问题。我想按此顺序执行以下步骤: 1. 保存输入字段中的文本。 2. 删除输入字段中的所有文本。 3. 在输入字段中重新加载之前删除的相同文本。 我的脚本的问题是 ug()- 函数在我的文本
任何人都可以帮助我如何保存多对多关系吗?我有任务,用户可以有很多任务,任务可以有很多用户(多对多),我想要实现的是,在更新表单中,管理员可以将多个用户分配给特定任务。这是通过 html 多选输入来完成
我在 Tensorflow 中训练了一个具有批归一化的模型。我想保存模型并恢复它以供进一步使用。批量归一化是通过 完成的 def batch_norm(input, phase): retur
我遇到了 grails 的问题。我有一个看起来像这样的域: class Book { static belongsTo = Author String toString() { tit
所以我正在开发一个应用程序,一旦用户连接(通过 soundcloud),就会出现以下对象: {userid: userid, username: username, genre: genre, fol
我正在开发一个具有多选项卡布局的 Angular 7 应用程序。每个选项卡都包含一个组件,该组件可以引用其他嵌套组件。 当用户选择一个新的/另一个选项卡时,当前选项卡上显示的组件将被销毁(我不仅仅是隐
我尝试使用 JEditorPane 进行一些简单的文本格式化,但随着知识的增长,我发现 JTextPane 更容易实现并且更强大。 我的问题是如何将 JTextPane 中的格式化文本保存到文件?它应
使用 Docker 相当新。 我为 Oracle 11g Full 提取了一个图像。创建了一个数据库并将应用程序安装到容器中。 正确配置后,我提交了生成 15GB 镜像的容器。 测试了该图像的新容器,
我是使用 Xcode 和 swift 的新手,仍在学习中。我在将核心数据从实体传递到文本字段/标签时遇到问题,然后用户可以选择编辑和保存记录。我的目标是,当用户从 friendslistViewCon
我正在用 Java 编写 Android 游戏,我需要一种可靠的方法来快速保存和加载应用程序状态。这个问题似乎适用于大多数 OO 语言。 了解我需要保存的内容:我正在使用策略模式来控制我的游戏实体。我
我想知道使用 fstream 加载/保存某种结构类型的数组是否是个好主意。注意,我说的是加载/保存到二进制文件。我应该加载/保存独立变量,例如 int、float、boolean 而不是结构吗?我这么
我希望能够将 QNetworkReply 保存到 QString/QByteArray。在我看到的示例中,它们总是将流保存到另一个文件。 目前我的代码看起来像这样,我从主机那里得到一个字符串,我想做的
我正在创建一个绘图应用程序。我有一个带有 Canvas 的自定义 View ,它根据用户输入绘制线条: class Line { float startX, startY, stopX, stop
我有 3 个 Activity 第一个 Activity 调用第二个 Activity ,第二个 Activity 调用第三个 Activity 。 第二个 Activity 使用第一个 Activi
我想知道如何在 Xcode 中保存 cookie。我想使用从一个网页获取的 cookie 并使用它访问另一个网页。我使用下面的代码登录该网站,我想保存从该连接获得的 cookie,以便在我建立另一个连
我有一个 SQLite 数据库存储我的所有日历事件,建模如下: TimerEvent *Attributes -date -dateForMark -reminder *Relat
我是一名优秀的程序员,十分优秀!