tf.variable_scope 參數


最近在看TensorFlow的變量管理,發現很多代碼中tf.variable_scope()參數的數量及意義還不太清楚,特此記錄:

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函數的定義,其中
name_or_scope: `string` or `VariableScope`: the scope to open. 是變量空間的名稱
default_name: 當name_or_scope 使用時它就可以忽略,基本沒什么用
values: 傳入該scope的tensor參數
initializer=None: 默認的參數初始化函數
regularizer: d默認的正則化函數
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有三種取值,默認取值是None:

True: 參數空間使用reuse 模式,即該空間下的所有tf.get_variable()函數將直接獲取已經創建的變量,如果參數不存在tf.get_variable()函數將會報錯。

AUTO_REUSE:若參數空間的參數不存在就創建他們,如果已經存在就直接獲取它們。

None 或者False 這里創建函數tf.get_variable()函數只能創建新的變量,當同名變量已經存在時,函數就報錯

下面是幾個例子:

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 在默認情況下為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的關系

tf.name_scope和tf.variable_scope是兩個作用域,主要與創建/調用變量函數tf.Variable() 和tf.get_variable()搭配使用。首先說tf.Variable() 和tf.get_variable()的區別:

    tf.Variable() 每次調用都會產生一個新的變量,他會自動檢測命名沖突並自行處理,變量名稱是一個可選參數,例如:

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)

運行結果:

f1/a1:0
f1/a1_1:0

False    # a2實際變成了a1_1並且和a1 不是同一個變量

而tf.get_variable()則不同,遇到重命名的變量創建且變量名沒有設置成共享變量(所謂的共享是指在同一參數空間下的共享,參數空間名稱不一樣就不能共享了)時,就會報錯 ;相對應的,變量名稱這個參數是必填參數,tf.get_variable()會根據這個參數去創建或者獲取變量。

tf.name_scope()主要用於管理圖中各種op,而tf.variable_scope()主要用於管理圖中變量的名字,在 tf.name_scope下時,tf.get_variable()創建的變量名不受 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)
運行結果:

f2/a1:0
f2/a1_1:0
False
f2/a1_2:0
f2/a1_2:0
f2_1/a2:0


---------------------
作者:hyxing520
來源:CSDN
原文:https://blog.csdn.net/hyxing520/article/details/80889496
版權聲明:本文為博主原創文章,轉載請附上博文鏈接!


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM