TensorFLow用Saver保存和恢复变量


Posted in Python onMarch 10, 2018

本文为大家分享了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))

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

Python 相关文章推荐
python实现通过pil模块对图片格式进行转换的方法
Mar 24 Python
python登录pop3邮件服务器接收邮件的方法
Apr 30 Python
python目录与文件名操作例子
Aug 28 Python
Python实现简易端口扫描器代码实例
Mar 15 Python
Pycharm学习教程(2) 代码风格
May 02 Python
python中解析json格式文件的方法示例
May 03 Python
python 计算两个日期相差多少个月实例代码
May 24 Python
Tornado高并发处理方法实例代码
Jan 15 Python
解决Python3 抓取微信账单信息问题
Jul 19 Python
Pytorch自己加载单通道图片用作数据集训练的实例
Jan 18 Python
Python基于stuck实现scoket文件传输
Apr 02 Python
python 调用Google翻译接口的方法
Dec 09 Python
tensorflow创建变量以及根据名称查找变量
Mar 10 #Python
Python2中文处理纪要的实现方法
Mar 10 #Python
python实现冒泡排序算法的两种方法
Mar 10 #Python
Python使用pyh生成HTML文档的方法示例
Mar 10 #Python
tensorflow获取变量维度信息
Mar 10 #Python
TensorFlow变量管理详解
Mar 10 #Python
TensorFlow神经网络优化策略学习
Mar 09 #Python
You might like
PHP+FLASH实现上传文件进度条相关文件 下载
2007/07/21 PHP
关于shopex同步ucenter的redirect问题,导致script不运行
2013/04/10 PHP
PHP实现的封装验证码类详解
2013/06/18 PHP
thinkphp连贯操作实例分析
2014/11/22 PHP
php实现四舍五入的方法小结
2015/03/03 PHP
php中使用base HTTP验证的方法
2015/04/20 PHP
支持中文、字母、数字的PHP验证码
2015/05/04 PHP
javascript浏览器窗口之间传递数据的方法
2015/01/20 Javascript
JavaScript使用pop方法移除数组最后一个元素用法实例
2015/04/06 Javascript
理解Javascript的call、apply
2015/12/16 Javascript
JavaScript和jquery获取父级元素、子级元素、兄弟元素的方法
2016/06/05 Javascript
微信小程序开发之toast等弹框提示使用教程
2017/06/08 Javascript
vue时间格式化实例代码
2017/06/13 Javascript
JS使用正则表达式验证身份证号码
2017/06/23 Javascript
Vue+mui实现图片的本地缓存示例代码
2018/05/24 Javascript
node.js 模块和其下载资源的镜像设置的方法
2018/09/06 Javascript
监控Nodejs的性能实例代码
2019/07/02 NodeJs
通过实例了解JS 连续赋值
2019/09/24 Javascript
通过layer实现可输入的模态框的例子
2019/09/27 Javascript
详解利用eventemitter2实现Vue组件通信
2019/11/04 Javascript
基于ant design日期控件使用_仅月份的操作
2020/10/27 Javascript
[32:07]完美世界DOTA2联赛PWL S3 LBZS vs Rebirth 第一场 12.16
2020/12/17 DOTA
Django开发的简易留言板案例详解
2018/12/04 Python
python操作日志的封装方法(两种方法)
2019/05/23 Python
Python学习笔记之文件的读写操作实例分析
2019/08/07 Python
用Python将Excel数据导入到SQL Server的例子
2019/08/24 Python
浅谈优化Django ORM中的性能问题
2020/07/09 Python
如何在python中判断变量的类型
2020/07/29 Python
新西兰优惠网站:Treat Me
2019/07/04 全球购物
七年级英语教学反思
2014/01/15 职场文书
销售顾问工作计划书
2014/09/15 职场文书
房产公证委托书范本
2014/09/20 职场文书
2015年业务员工作总结范文
2015/04/07 职场文书
大学生各类奖学金申请书
2019/06/24 职场文书
Python 图片添加美颜效果
2022/04/28 Python
Nginx代理Redis哨兵主从配置的实现
2022/07/15 Servers