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微信库itchat实现微信自动回复功能
May 18 Python
python使用锁访问共享变量实例解析
Feb 08 Python
Python二进制串转换为通用字符串的方法
Jul 23 Python
python使用Matplotlib画饼图
Sep 25 Python
Django实现一对多表模型的跨表查询方法
Dec 18 Python
Django在pycharm下修改默认启动端口的方法
Jul 26 Python
win10安装tensorflow-gpu1.8.0详细完整步骤
Jan 20 Python
解决Python数据可视化中文部分显示方块问题
May 16 Python
Django中Aggregation聚合的基本使用方法
Jul 09 Python
简述 Python 的类和对象
Aug 21 Python
一行代码python实现文件共享服务器
Apr 22 Python
python图片灰度化处理的几种方法
Jun 23 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
配置Apache2.2+PHP5+CakePHP1.2+MySQL5运行环境
2009/04/25 PHP
比较全面的PHP数组的使用方法小结
2010/09/23 PHP
9个经典的PHP代码片段分享
2014/12/18 PHP
PHP弱类型的安全问题详细总结
2016/09/25 PHP
PHP实现浏览器中直接输出图片的方法示例
2018/03/14 PHP
JS实现浏览器菜单命令
2006/09/05 Javascript
Javascript中valueOf与toString区别浅析
2013/03/19 Javascript
js动态设置鼠标事件示例代码
2013/10/30 Javascript
利用jq让你的div居中的好方法分享
2013/11/21 Javascript
利用jQuery实现WordPress中@的ID悬浮显示评论内容
2015/12/11 Javascript
Jquery uploadify上传插件使用详解
2016/01/13 Javascript
Hammer.js+轮播原理实现简洁的滑屏功能
2016/02/02 Javascript
vue实现列表的添加点击
2016/12/29 Javascript
js学习总结_基于数据类型检测的四种方式(必看)
2017/07/04 Javascript
JavaScript正则表达式校验与递归函数实际应用实例解析
2017/08/04 Javascript
解决JSON.stringify()自动将中文转译成unicode的问题
2018/01/05 Javascript
解决layer.prompt无效的问题
2019/09/24 Javascript
Python中将字典转换为XML以及相关的命名空间解析
2015/10/15 Python
Python中urllib+urllib2+cookielib模块编写爬虫实战
2016/01/20 Python
python爬虫实战之爬取京东商城实例教程
2017/04/24 Python
python爬虫框架scrapy实战之爬取京东商城进阶篇
2017/04/24 Python
python xpath获取页面注释的方法
2019/01/14 Python
详解使用PyInstaller将Pygame库编写的小游戏程序打包为exe文件
2019/08/23 Python
Django项目uwsgi+Nginx保姆级部署教程实现
2020/04/19 Python
使用keras和tensorflow保存为可部署的pb格式
2020/05/25 Python
python 发送邮件的四种方法汇总
2020/12/02 Python
Kipling凯浦林美国官网:世界著名时尚休闲包袋品牌
2016/08/24 全球购物
美国定制钻石订婚戒指:Ritani
2017/12/08 全球购物
韩国保养品、日本药妆购物网:小三美日
2018/12/30 全球购物
C/C++程序员常见面试题一
2012/12/08 面试题
加入学生会演讲稿
2014/04/24 职场文书
读书活动总结范文
2014/04/26 职场文书
中国梦演讲稿开场白
2014/08/28 职场文书
2014年大学生党员自我评议
2014/09/22 职场文书
2016年父亲节寄语
2015/12/04 职场文书
详解Spring Bean的配置方式与实例化
2022/06/10 Java/Android