TensorFlow变量管理详解


Posted in Python onMarch 10, 2018

一、TensorFlow变量管理

1. TensorFLow还提供了tf.get_variable函数来创建或者获取变量,tf.variable用于创建变量时,其功能和tf.Variable基本是等价的。tf.get_variable中的初始化方法(initializer)的参数和tf.Variable的初始化过程也类似,initializer函数和tf.Variable的初始化方法是一一对应的,详见下表。

TensorFlow变量管理详解

tf.get_variable和tf.Variable最大的区别就在于指定变量名称的参数。对于tf.Variable函数,变量名称是一个可选的参数,通过name=”v”的形式给出,对于tf.get_variable函数,变量名称是一个必填的参数,tf.get_variable会根据这个名称去创建或者获取变量。

2. 通过tf.variable_scope函数可以控制tf.get_variable函数的语义。当tf.variable_scope函数的参数reuse=True生成上下文管理器时,该上下文管理器内的所有的tf.get_variable函数会直接获取已经创建的变量,如果变量不存在则报错;当tf.variable_scope函数的参数reuse=False或者None时创建的上下文管理器中,tf.get_variable函数则直接创建新的变量,若同名的变量已经存在则报错。

3. 另tf.variable_scope函数是可以嵌套使用的。嵌套的时候,若某层上下文管理器未声明reuse参数,则该层上下文管理器的reuse参数与其外层保持一致。

4.tf.variable_scope函数提供了一个管理变量命名空间的方式。在tf.variable_scope中创建的变量,名称.name中名称前面会加入命名空间的名称,并通过“/”来分隔命名空间的名称和变量的名称。tf.get_variable("foou/baru/u", [1]),可以通过带命名空间名称的变量名来获取其命名空间下的变量。

二、TensorFlow编程演示

import tensorflow as tf 
 
# 在名字为foo的命名空间内创建名字为v的变量 
with tf.variable_scope("foo"): 
  v = tf.get_variable("v", [1], initializer=tf.constant_initializer(1.0)) 
 
''''' 
# 因为命名空间foo内已经存在变量v,再次创建则报错 
with tf.variable_scope("foo"): 
  v = tf.get_variable("v", [1]) 
# ValueError: Variable foo/v already exists, disallowed. 
# Did you mean to set reuse=True in VarScope? 
''' 
# 将参数reuse参数设置为True,则tf.get_variable可直接获取已声明的变量 
with tf.variable_scope("foo", reuse=True): 
  v1 = tf.get_variable("v", [1]) 
  print(v == v1) # True 
 
''''' 
# 当reuse=True时,tf.get_variable只能获取指定命名空间内的已创建的变量 
with tf.variable_scope("bar", reuse=True): 
  v2 = tf.get_variable("v", [1]) 
# ValueError: Variable bar/v does not exist, or was not created with 
# tf.get_variable(). Did you mean to set reuse=None in VarScope? 
''' 
 
with tf.variable_scope("root"): 
  # 通过tf.get_variable_scope().reuse函数获取当前上下文管理器内的reuse参数取值 
  print(tf.get_variable_scope().reuse) # False 
 
  with tf.variable_scope("foo1", reuse=True): 
    print(tf.get_variable_scope().reuse) # True 
 
    with tf.variable_scope("bar1"): 
      # 嵌套在上下文管理器foo1内的bar1内未指定reuse参数,则保持与外层一致 
      print(tf.get_variable_scope().reuse) # True 
 
  print(tf.get_variable_scope().reuse) # False 
 
# tf.variable_scope函数提供了一个管理变量命名空间的方式 
u1 = tf.get_variable("u", [1]) 
print(u1.name) # u:0 
with tf.variable_scope("foou"): 
  u2 = tf.get_variable("u", [1]) 
  print(u2.name) # foou/u:0 
 
with tf.variable_scope("foou"): 
  with tf.variable_scope("baru"): 
    u3 = tf.get_variable("u", [1]) 
    print(u3.name) # foou/baru/u:0 
 
  u4 = tf.get_variable("u1", [1]) 
  print(u4.name) # foou/u1:0 
 
# 可直接通过带命名空间名称的变量名来获取其命名空间下的变量 
with tf.variable_scope("", reuse=True): 
  u5 = tf.get_variable("foou/baru/u", [1]) 
  print(u5.name) # foou/baru/u:0 
  print(u5 == u3) # True 
  u6 = tf.get_variable("foou/u1", [1]) 
  print(u6.name) # foou/u1:0 
  print(u6 == u4) # True

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

Python 相关文章推荐
Python字符转换
Sep 06 Python
python应用程序在windows下不出现cmd窗口的办法
May 29 Python
Python解析nginx日志文件
May 11 Python
详解python里使用正则表达式的全匹配功能
Oct 19 Python
Python数据持久化shelve模块用法分析
Jun 29 Python
python批量修改图片大小的方法
Jul 24 Python
python创建文件备份的脚本
Sep 11 Python
python将字符串以utf-8格式保存在txt文件中的方法
Oct 30 Python
使用Django连接Mysql数据库步骤
Jan 15 Python
Pyqt5如何让QMessageBox按钮显示中文示例代码
Apr 11 Python
python json.dumps中文乱码问题解决
Apr 01 Python
Django websocket原理及功能实现代码
Nov 14 Python
TensorFlow神经网络优化策略学习
Mar 09 #Python
TensorFlow实现AutoEncoder自编码器
Mar 09 #Python
TensorFlow实现MLP多层感知机模型
Mar 09 #Python
TensorFlow实现Softmax回归模型
Mar 09 #Python
用python实现百度翻译的示例代码
Mar 09 #Python
TensorFlow深度学习之卷积神经网络CNN
Mar 09 #Python
TensorFlow实现卷积神经网络CNN
Mar 09 #Python
You might like
php小技巧之过滤ascii控制字符
2014/05/14 PHP
10个超级有用值得收藏的PHP代码片段
2015/01/22 PHP
PHP实现上一篇下一篇的方法实例总结
2016/09/22 PHP
thinkPHP通用控制器实现方法示例
2017/11/23 PHP
PHP+Apache环境中如何隐藏Apache版本
2017/11/24 PHP
PHP实现对图片的反色处理功能【测试可用】
2018/02/01 PHP
Yii框架引入coreseek分页功能示例
2019/02/08 PHP
Knockoutjs快速入门(经典)
2012/12/24 Javascript
Jquery实现的tab效果可以指定默认显示第几页
2013/10/16 Javascript
弹出窗口并且此窗口带有半透明的遮罩层效果
2014/03/13 Javascript
分享一个自己写的简单的javascript分页组件
2015/02/15 Javascript
详解React开发中使用require.ensure()按需加载ES6组件
2017/05/12 Javascript
Javascript获取某个月的天数
2018/05/30 Javascript
JS实现页面跳转与刷新的方法汇总
2019/08/30 Javascript
js禁止查看源文件屏蔽Ctrl+u/s、F12、右键等兼容IE火狐chrome
2020/10/01 Javascript
[02:48]DOTA2英雄基础教程 拉席克
2013/12/12 DOTA
Python配置文件解析模块ConfigParser使用实例
2015/04/13 Python
在Python的web框架中中编写日志列表的教程
2015/04/30 Python
python实现颜色空间转换程序(Tkinter)
2015/12/31 Python
使用python读取.text文件特定行的数据方法
2019/01/28 Python
很酷的python表白工具 你喜欢我吗
2019/04/11 Python
Pandas_cum累积计算和rolling滚动计算的用法详解
2019/07/04 Python
对tensorflow中tf.nn.conv1d和layers.conv1d的区别详解
2020/02/11 Python
Python word文本自动化操作实现方法解析
2020/11/05 Python
css3实现波纹特效、H5实现动态波浪效果
2018/01/31 HTML / CSS
一款基于css3和jquery实现的动画显示弹出层按钮教程
2015/01/04 HTML / CSS
Grow Gorgeous美国官网:只要八天,体验唤醒毛囊后新生的茂密秀发
2018/06/04 全球购物
竞选班长的演讲稿
2014/04/24 职场文书
小学生感恩演讲稿
2014/04/25 职场文书
525心理活动总结
2014/07/04 职场文书
党员学习党的群众路线思想汇报(5篇)
2014/09/10 职场文书
2019大学竞选班长发言稿
2019/06/27 职场文书
MySQL InnoDB ReplicaSet(副本集)简单介绍
2021/04/24 MySQL
详解MySQL 联合查询优化机制
2021/05/10 MySQL
基于Redis6.2.6版本部署Redis Cluster集群的问题
2022/04/01 Redis
阿里云日志过滤器配置日志服务
2022/04/09 Servers