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 列表理解及使用方法
Oct 27 Python
Pandas实现数据类型转换的一些小技巧汇总
May 07 Python
pandas 选择某几列的方法
Jul 03 Python
python使用matplotlib库生成随机漫步图
Aug 27 Python
python爬取网易云音乐评论
Nov 16 Python
Python中使用双下划线防止类属性被覆盖问题
Jun 27 Python
python如何获取列表中每个元素的下标位置
Jul 01 Python
django xadmin中form_layout添加字段显示方式
Mar 30 Python
Django REST 异常处理详解
Jul 15 Python
Flask中jinja2的继承实现方法及实例
Mar 03 Python
利用Python网络爬虫爬取各大音乐评论的代码
Apr 13 Python
基于Python的EasyGUI学习实践
May 07 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
ThinkPHP使用smarty模板引擎的方法
2014/07/01 PHP
php格式化json函数示例代码
2016/05/12 PHP
PHP入门教程之数学运算技巧总结
2016/09/11 PHP
php pdo操作数据库示例
2017/03/10 PHP
Javascript实例教程(19) 使用HoTMetal(3)
2006/12/23 Javascript
js 居中漂浮广告
2010/03/21 Javascript
javascript各浏览器中option元素的表现差异
2011/04/07 Javascript
js实现单行文本向上滚动效果实例代码
2013/11/28 Javascript
深入分析下javascript中的[]()+!
2015/07/07 Javascript
jQuery移动web开发之页面跳转和加载外部页面的实现
2015/12/04 Javascript
使用jQuery实现一个类似GridView的编辑,更新,取消和删除的功能
2017/03/15 Javascript
微信小程序 数据遍历的实现
2017/04/05 Javascript
JavaScript制作简单的框选图表
2017/05/15 Javascript
简单的网页广告特效实例
2017/08/19 Javascript
在vue项目中安装使用Mint-UI的方法
2017/12/27 Javascript
实战node静态文件服务器的示例代码
2018/03/08 Javascript
vue实现简单loading进度条
2018/06/06 Javascript
vue表单中遍历表单操作按钮的显示隐藏示例
2019/10/30 Javascript
我所理解的JavaScript中的this指向
2020/09/04 Javascript
Python操作sqlite3快速、安全插入数据(防注入)的实例
2014/04/26 Python
python使用装饰器和线程限制函数执行时间的方法
2015/04/18 Python
python实现共轭梯度法
2019/07/03 Python
浅谈python多进程共享变量Value的使用tips
2019/07/16 Python
python实现的读取网页并分词功能示例
2019/10/29 Python
解决django FileFIELD的编码问题
2020/03/30 Python
Pedro官网:新加坡时尚品牌
2019/08/27 全球购物
linux下进程间通信的方式
2013/01/23 面试题
安全员岗位职责
2013/11/11 职场文书
普通话宣传标语
2014/06/26 职场文书
退学证明范本3篇
2014/10/29 职场文书
群众路线教育实践活动方案
2014/10/31 职场文书
违反学校规则制度检讨书
2015/01/01 职场文书
2015年教师党员自我评价材料
2015/03/04 职场文书
Python 居然可以在 Excel 中画画你知道吗
2022/02/15 Python
Python内置包对JSON文件数据进行编码和解码
2022/04/12 Python
SqlServer常用函数及时间处理小结
2023/05/08 SQL Server