TensorFLow用Saver保存和恢复变量

作者:Deephome 时间:2021-09-18 12:17:17 

本文为大家分享了TensorFLow用Saver保存和恢复变量的具体代码,供大家参考,具体内容如下

建立文件tensor_save.py, 保存变量v1,v2的tensor到checkpoint files中,名称分别设置为v3,v4。


import tensorflow as tf

# Create some variables.
v1 = tf.Variable(3, name="v1")
v2 = tf.Variable(4, name="v2")

# Create model
y=tf.add(v1,v2)

# Add an op to initialize the variables.
init_op = tf.initialize_all_variables()

# Add ops to save and restore all the variables.
saver = tf.train.Saver({'v3':v1,'v4':v2})

# Later, launch the model, initialize the variables, do some work, save the
# variables to disk.
with tf.Session() as sess:
sess.run(init_op)
print("v1 = ", v1.eval())
print("v2 = ", v2.eval())
# Save the variables to disk.
save_path = saver.save(sess, "f:/tmp/model.ckpt")
print ("Model saved in file: ", save_path)

建立文件tensor_restror.py, 将checkpoint files中名称分别为v3,v4的tensor分别恢复到变量v3,v4中。


import tensorflow as tf

# Create some variables.
v3 = tf.Variable(0, name="v3")
v4 = tf.Variable(0, name="v4")

# Create model
y=tf.mul(v3,v4)

# Add ops to save and restore all the variables.
saver = tf.train.Saver()

# Later, launch the model, use the saver to restore variables from disk, and
# do some work with the model.
with tf.Session() as sess:
# Restore variables from disk.
saver.restore(sess, "f:/tmp/model.ckpt")
print ("Model restored.")
print ("v3 = ", v3.eval())
print ("v4 = ", v4.eval())
print ("y = ",sess.run(y))

来源:http://blog.csdn.net/muyiyushan/article/details/68486497

标签:TensorFLow,Saver,变量
0
投稿

猜你喜欢

  • python神经网络Keras构建CNN网络训练

    2022-08-16 08:42:37
  • 详解django中Template语言

    2022-12-01 21:20:59
  • MySQL数据库的授权原则

    2008-12-29 13:39:00
  • SQL Server转换为XQuery及反向转换

    2009-01-20 13:32:00
  • Python编程中的异常处理教程

    2022-10-16 04:26:23
  • asp中isNull(str), isEmpty(str)和str=""的区别

    2008-02-15 13:10:00
  • 用Python将Excel数据导入到SQL Server的例子

    2021-08-21 19:24:30
  • Python中条件判断语句的简单使用方法

    2022-10-12 00:34:34
  • php7 参数、整形及字符串处理机制修改实例分析

    2023-11-23 19:48:31
  • Python环境下安装PyGame和PyOpenGL的方法

    2021-12-09 10:14:38
  • Python实现Tab自动补全和历史命令管理的方法

    2022-06-16 10:12:46
  • Python必备技巧之函数的使用详解

    2021-03-02 11:19:59
  • Python try-except-else-finally的具体使用

    2022-10-18 14:31:45
  • 利用Python实现绘制论文中的曲线图

    2022-12-14 12:04:37
  • next在python中返回迭代器的实例方法

    2022-10-28 12:35:41
  • Python实现绘制3D地球旋转效果

    2021-04-17 22:25:37
  • 一个滑动门菜单例子源码

    2007-12-31 10:16:00
  • 巧妙规划使用Oracle数据空间

    2009-03-20 11:51:00
  • 记录模型训练时loss值的变化情况

    2022-03-22 23:39:48
  • pandas中的ExcelWriter和ExcelFile的实现方法

    2023-09-20 00:10:36
  • asp之家 网络编程 m.aspxhome.com