TensorFlow变量管理详解
作者:marsjhao 时间:2023-11-23 07:11:18
一、TensorFlow变量管理
1. TensorFLow还提供了tf.get_variable函数来创建或者获取变量,tf.variable用于创建变量时,其功能和tf.Variable基本是等价的。tf.get_variable中的初始化方法(initializer)的参数和tf.Variable的初始化过程也类似,initializer函数和tf.Variable的初始化方法是一一对应的,详见下表。
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
来源:http://blog.csdn.net/marsjhao/article/details/72829806
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
pyqt5的QWebEngineView 使用模板的方法
如何利用Python 快速找到最大文件
![](https://img.aspxhome.com/file/2023/2/90822_0s.jpg)
Selenium python时间控件输入问题解决方案
![](https://img.aspxhome.com/file/2023/8/79608_0s.png)
深度定制Python的Flask框架开发环境的一些技巧总结
python 自定义异常和主动抛出异常(raise)的操作
![](https://img.aspxhome.com/file/2023/8/92148_0s.jpg)
opencv 图像轮廓的实现示例
![](https://img.aspxhome.com/file/2023/5/115505_0s.jpg)
详解Python字符串切片
![](https://img.aspxhome.com/file/2023/5/67425_0s.png)
Python Pygame实战之赛车游戏的实现
![](https://img.aspxhome.com/file/2023/1/135431_0s.png)
最强Python可视化绘图库Plotly详解用法
![](https://img.aspxhome.com/file/2023/9/127019_0s.png)
Python 遍历子文件和所有子文件夹的代码实例
![](https://img.aspxhome.com/file/2023/9/78589_0s.png)
如何实现更换Jupyter Notebook内核Python版本
![](https://img.aspxhome.com/file/2023/6/120476_0s.png)
超详细教你怎么升级Mysql的版本
![](https://img.aspxhome.com/file/2023/8/107708_0s.png)
详解使用PyInstaller将Pygame库编写的小游戏程序打包为exe文件
![](https://img.aspxhome.com/file/2023/2/109992_0s.png)
如何基于python把文字图片写入word文档
go doudou应用中使用枚举类型教程示例
python装饰器代替set get方法实例
剑走偏锋:体验ebay的AIR
![](https://img.aspxhome.com/file/UploadPic/200811/13/01-14s.jpg)
MySql登录时闪退的快速解决办法
![](https://img.aspxhome.com/file/2023/9/118229_0s.png)
实现用python算法计算圆周率的小诀窍
![](https://img.aspxhome.com/file/2023/0/124740_0s.png)
Python3监控疫情的完整代码
![](https://img.aspxhome.com/file/2023/2/83782_0s.jpg)