tf.variable_scope 参数
最近在看TensorFlow的變量管理,發(fā)現(xiàn)很多代碼中tf.variable_scope()參數(shù)的數(shù)量及意義還不太清楚,特此記錄:
def __init__(self,
name_or_scope,
default_name=None,
values=None,
initializer=None,
regularizer=None,
caching_device=None,
partitioner=None,
custom_getter=None,
reuse=None,
dtype=None,
use_resource=None,
constraint=None,
auxiliary_name_scope=True):
"""Initialize the context manager.
Args:
name_or_scope: `string` or `VariableScope`: the scope to open.
default_name: The default name to use if the `name_or_scope` argument is
`None`, this name will be uniquified. If name_or_scope is provided it
won't be used and therefore it is not required and can be None.
values: The list of `Tensor` arguments that are passed to the op function.
initializer: default initializer for variables within this scope.
regularizer: default regularizer for variables within this scope.
caching_device: default caching device for variables within this scope.
partitioner: default partitioner for variables within this scope.
custom_getter: default custom getter for variables within this scope.
reuse: `True`, None, or tf.AUTO_REUSE; if `True`, we go into reuse mode
for this scope as well as all sub-scopes; if tf.AUTO_REUSE, we create
variables if they do not exist, and return them otherwise; if None, we
inherit the parent scope's reuse flag. When eager execution is enabled,
this argument is always forced to be tf.AUTO_REUSE.
dtype: type of variables created in this scope (defaults to the type
in the passed scope, or inherited from parent scope).
use_resource: If False, all variables will be regular Variables. If True,
experimental ResourceVariables with well-defined semantics will be used
instead. Defaults to False (will later change to True). When eager
execution is enabled this argument is always forced to be True.
constraint: An optional projection function to be applied to the variable
after being updated by an `Optimizer` (e.g. used to implement norm
constraints or value constraints for layer weights). The function must
take as input the unprojected Tensor representing the value of the
variable and return the Tensor for the projected value
(which must have the same shape). Constraints are not safe to
use when doing asynchronous distributed training.
auxiliary_name_scope: If `True`, we create an auxiliary name scope with
the scope. If `False`, we don't touch name scope.
Returns:
A scope that can be captured and reused.
Raises:
ValueError: when trying to reuse within a create scope, or create within
a reuse scope.
TypeError: when the types of some arguments are not appropriate.
"""
上面的代碼是tf.variable函數(shù)的定義,其中
name_or_scope: `string` or `VariableScope`: the scope to open. 是變量空間的名稱
default_name: 當(dāng)name_or_scope 使用時(shí)它就可以忽略,基本沒(méi)什么用
values: 傳入該scope的tensor參數(shù)
initializer=None: 默認(rèn)的參數(shù)初始化函數(shù)
regularizer: d默認(rèn)的正則化函數(shù)
caching_device: default caching device for variables within this scope.
partitioner: default partitioner for variables within this scope.
custom_getter: default custom getter for variables within this scope.
reuse: `True`, None, or tf.AUTO_REUSE; if `True`, we go into reuse mode
for this scope as well as all sub-scopes; if tf.AUTO_REUSE, we create
variables if they do not exist, and return them otherwise; if None, we
inherit the parent scope's reuse flag. When eager execution is enabled,
this argument is always forced to be tf.AUTO_REUSE
reuse有三種取值,默認(rèn)取值是None:
True: 參數(shù)空間使用reuse 模式,即該空間下的所有tf.get_variable()函數(shù)將直接獲取已經(jīng)創(chuàng)建的變量,如果參數(shù)不存在tf.get_variable()函數(shù)將會(huì)報(bào)錯(cuò)。
AUTO_REUSE:若參數(shù)空間的參數(shù)不存在就創(chuàng)建他們,如果已經(jīng)存在就直接獲取它們。
None 或者False 這里創(chuàng)建函數(shù)tf.get_variable()函數(shù)只能創(chuàng)建新的變量,當(dāng)同名變量已經(jīng)存在時(shí),函數(shù)就報(bào)錯(cuò)
下面是幾個(gè)例子:
with tf.variable_scope("foo"):
v = tf.get_variable("v", [1], initializer=tf.constant_initializer(1.0))
#with tf.variable_scope("foo"):
# v = tf.get_variable("v", [1])
with tf.variable_scope("foo", reuse=True):
v1 = tf.get_variable("v", [1])
print(v == v1)
#with tf.variable_scope("bar", reuse=True):
# v = tf.get_variable("v", [1])
輸出是True
with tf.variable_scope("root"): # reuse 在默認(rèn)情況下為false 或者和 上一層保持一致
print(tf.get_variable_scope().reuse)
with tf.variable_scope("foo", reuse=True):
print(tf.get_variable_scope().reuse)
with tf.variable_scope("bar"):
print(tf.get_variable_scope().reuse)
print(tf.get_variable_scope().reuse)
tf.name_scope和tf.variable_scope的關(guān)系
tf.name_scope和tf.variable_scope是兩個(gè)作用域,主要與創(chuàng)建/調(diào)用變量函數(shù)tf.Variable() 和tf.get_variable()搭配使用。首先說(shuō)tf.Variable() 和tf.get_variable()的區(qū)別:
????tf.Variable() 每次調(diào)用都會(huì)產(chǎn)生一個(gè)新的變量,他會(huì)自動(dòng)檢測(cè)命名沖突并自行處理,變量名稱是一個(gè)可選參數(shù),例如:
a1 = tf.Variable(tf.random_normal(shape=[2, 3], mean=0, stddev=1), name='a1')
a2 = tf.Variable(tf.random_normal(shape=[2, 3], mean=0, stddev=1), name='a1')
print(a1.name)
print(a2.name)
print(a1==a2)
運(yùn)行結(jié)果:
f1/a1:0
f1/a1_1:0
False? ? # a2實(shí)際變成了a1_1并且和a1 不是同一個(gè)變量
而tf.get_variable()則不同,遇到重命名的變量創(chuàng)建且變量名沒(méi)有設(shè)置成共享變量(所謂的共享是指在同一參數(shù)空間下的共享,參數(shù)空間名稱不一樣就不能共享了)時(shí),就會(huì)報(bào)錯(cuò) ;相對(duì)應(yīng)的,變量名稱這個(gè)參數(shù)是必填參數(shù),tf.get_variable()會(huì)根據(jù)這個(gè)參數(shù)去創(chuàng)建或者獲取變量。
tf.name_scope()主要用于管理圖中各種op,而tf.variable_scope()主要用于管理圖中變量的名字,在 tf.name_scope下時(shí),tf.get_variable()創(chuàng)建的變量名不受 name_scope 的影響(不受它的約束)
import tensorflow as tf
with tf.variable_scope('f2'):
#a1 = tf.get_variable(name='a1', shape=[1], initializer=tf.constant_initializer(1))
a1 = tf.Variable(tf.random_normal(shape=[2, 3], mean=0, stddev=1), name='a1')
a2 = tf.Variable(tf.random_normal(shape=[2, 3], mean=0, stddev=1), name='a1')
a5 = tf.get_variable(name='a1', shape=[1], initializer=tf.constant_initializer(1))
with tf.variable_scope('f2', reuse=True):
a3 = tf.get_variable(name='a1', shape=[1], initializer=tf.constant_initializer(1))
a4 = tf.Variable(tf.random_normal(shape=[2, 3], mean=0, stddev=1), name='a2')
print(a1.name)
print(a2.name)
print(a1==a2)
print(a5.name)
print(a3.name)
print(a4.name)
運(yùn)行結(jié)果:
f2/a1:0
f2/a1_1:0
False
f2/a1_2:0
f2/a1_2:0
f2_1/a2:0
---------------------
作者:hyxing520
來(lái)源:CSDN
原文:https://blog.csdn.net/hyxing520/article/details/80889496
版權(quán)聲明:本文為博主原創(chuàng)文章,轉(zhuǎn)載請(qǐng)附上博文鏈接!
總結(jié)
以上是生活随笔為你收集整理的tf.variable_scope 参数的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: tf.shape()
- 下一篇: Tensorflow函数——tf.var