Python语言技术文档

微信小程序技术文档

php语言技术文档

jsp语言技术文档

asp语言技术文档

C#/.NET语言技术文档

html5/css技术文档

javascript

点击排行

TensorFlow变量管理详解

来源:中文源码网    浏览:208 次    日期:2024-05-17 12:15:28
【下载文档:  TensorFlow变量管理详解.txt 】


TensorFlow变量管理详解
一、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
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持中文源码网。

相关内容