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 相关文章推荐
探索Python3.4中新引入的asyncio模块
Apr 08 Python
Python中的Matplotlib模块入门教程
Apr 15 Python
python实现12306火车票查询器
Apr 20 Python
NumPy 如何生成多维数组的方法
Feb 05 Python
Python实现基于TCP UDP协议的IPv4 IPv6模式客户端和服务端功能示例
Mar 22 Python
解决python中使用plot画图,图不显示的问题
Jul 04 Python
Python中@property的理解和使用示例
Jun 11 Python
实现Python与STM32通信方式
Dec 18 Python
Windows下实现将Pascal VOC转化为TFRecords
Feb 17 Python
python中常用的数据结构介绍
Jan 12 Python
AI:如何训练机器学习的模型
Apr 16 Python
详解Go语言运用广度优先搜索走迷宫
Jun 23 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中“简单工厂模式”实例代码讲解
2012/09/04 PHP
ThinkPHP实现支付宝接口功能实例
2014/12/02 PHP
php通过baihui网API实现读取word文档并展示
2015/06/22 PHP
php中10个不同等级压缩优化图片操作示例
2016/11/14 PHP
JSON 学习之JSON in JavaScript详细使用说明
2010/02/23 Javascript
jquerymobile checkbox及时刷新才能获取其准确值
2012/04/14 Javascript
php图像生成函数之间的区别分析
2012/12/06 Javascript
jQuery中innerHeight()方法用法实例
2015/01/19 Javascript
jQuery 3.0 的 setter和getter 模式详解
2016/07/11 Javascript
JS 实现计算器详解及实例代码(一)
2017/01/08 Javascript
利用webstrom调试Vue.js单页面程序的方法教程
2017/06/06 Javascript
vue.js绑定事件监听器示例【基于v-on事件绑定】
2018/07/07 Javascript
更优雅的微信小程序骨架屏实现详解
2019/08/07 Javascript
微信小程序上传帖子的实例代码(含有文字图片的微信验证)
2020/07/11 Javascript
javascript全局自定义鼠标右键菜单
2020/12/08 Javascript
在Python的Django框架中获取单个对象数据的简单方法
2015/07/17 Python
Python自动化测试ConfigParser模块读写配置文件
2016/08/15 Python
Django视图和URL配置详解
2018/01/31 Python
用python 实现在不确定行数情况下多行输入方法
2019/01/28 Python
python实现矩阵打印
2019/03/02 Python
Python基础学习之类与实例基本用法与注意事项详解
2019/06/17 Python
使用pandas读取文件的实现
2019/07/31 Python
Python scipy的二维图像卷积运算与图像模糊处理操作示例
2019/09/06 Python
Python3 实现减少可调用对象的参数个数
2019/12/20 Python
Python利用imshow制作自定义渐变填充柱状图(colorbar)
2020/12/10 Python
HTML中fieldset标签概述及使用方法
2013/02/01 HTML / CSS
欧洲最大的拼图游戏商店:JigsawPuzzle.co.uk
2018/07/04 全球购物
科颜氏印度官网:Kiehl’s印度
2021/02/20 全球购物
蛋糕店的商业计划书范文
2014/01/27 职场文书
小学生家长评语集锦
2014/01/30 职场文书
思想品德自我评价
2014/02/04 职场文书
2014年会策划方案
2014/05/11 职场文书
优秀语文教师事迹
2014/05/18 职场文书
2014企业领导班子四风对照检查材料思想汇报
2014/09/17 职场文书
2014红色之旅心得体会
2014/10/07 职场文书
python实战之90行代码写个猜数字游戏
2021/04/22 Python