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实现端口复用实例代码
Jul 03 Python
八大排序算法的Python实现
Jan 28 Python
Python简单实现TCP包发送十六进制数据的方法
Apr 16 Python
Python爬虫:通过关键字爬取百度图片
Feb 17 Python
详解Python自建logging模块
Jan 29 Python
基于Python数据分析之pandas统计分析
Mar 03 Python
解决numpy矩阵相减出现的负值自动转正值的问题
Jun 03 Python
Python生成器传参数及返回值原理解析
Jul 22 Python
Python中的With语句的使用及原理
Jul 29 Python
Pycharm调试程序技巧小结
Aug 08 Python
Python实例方法、类方法、静态方法区别详解
Sep 05 Python
python实现腾讯滑块验证码识别
Apr 27 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
linux系统上支持php的 iconv()函数的方法
2011/10/01 PHP
PHP 第二节 数据类型之字符串类型
2012/04/28 PHP
PHP手机号码归属地查询代码(API接口/mysql)
2012/09/04 PHP
YII路径的用法总结
2014/07/09 PHP
PHP的PDO操作简单示例
2016/03/30 PHP
php新建文件的方法实例
2019/09/26 PHP
CI框架简单分页类用法示例
2020/06/06 PHP
几个javascript操作word的参考代码
2009/10/26 Javascript
jQuery 渐变下拉菜单
2009/12/15 Javascript
收集的一些Array及String原型对象的扩展实现代码
2010/12/05 Javascript
AngularJS 实现JavaScript 动画效果详解
2016/09/08 Javascript
解决JS外部文件中文注释出现乱码问题
2017/07/09 Javascript
js实现简易聊天对话框
2017/08/17 Javascript
JavaScript去掉数组重复项的方法分析【测试可用】
2018/07/19 Javascript
Layui组件Table绑定行点击事件和获取行数据的方法
2018/08/19 Javascript
Node批量爬取头条视频并保存方法
2018/09/20 Javascript
微信小程序使用npm包的方法步骤
2019/08/13 Javascript
vue实现Input输入框模糊查询方法
2021/01/29 Javascript
JS中数组实现代码(倒序遍历数组,数组连接字符串)
2019/12/29 Javascript
js this 绑定机制深入详解
2020/04/30 Javascript
[01:12]DOTA2次级职业联赛 - Newbee.Y 战队宣传片
2014/12/01 DOTA
python清除字符串里非数字字符的方法
2015/07/02 Python
python dataframe常见操作方法:实现取行、列、切片、统计特征值
2018/06/09 Python
TensorFlow实现模型评估
2018/09/07 Python
Django框架配置mysql数据库实现过程
2020/04/22 Python
Django 用户认证Auth组件的使用
2020/11/30 Python
探讨HTML5移动开发的几大特性(必看)
2015/12/30 HTML / CSS
兰蔻加拿大官方网站:Lancome加拿大
2016/08/05 全球购物
美国著名的户外用品品牌:L.L.Bean
2018/01/05 全球购物
爱尔兰最大的体育零售商:Life Style Sports
2019/06/12 全球购物
英语专业学生个人求职信
2014/01/28 职场文书
集体备课反思
2014/02/12 职场文书
建设工程授权委托书
2014/09/22 职场文书
教师查摆问题自查报告
2014/10/11 职场文书
公司年夜饭通知
2015/04/25 职场文书
傲慢与偏见电影观后感
2015/06/10 职场文书