二 TensorFlow的變數管理

2021-10-04 10:38:14 字數 4074 閱讀 8022

龐大的神經網路構建及訓練過程中,需要對變數進行多次索引,tf框架本身設計了比較完善的變數管理機制。變數作用域能夠更好的管理模組的變數(特別是將來會重用到的變數)。

tf常用的變數作用域有兩種:tf.name_scope和tf.variable_scope,

tf本身的變數管理有如下介面:tf.get_variables 和 tf.variable

主要與tf.variable搭配使用,呼叫方式為:

tf.name_scope(

'scope_name'

)#or

tf.name_scope(named_scope)

以上兩種方式均可。當傳入字串時,用以給變數名新增字首,類似於目錄,如下述**所示;

import tensorflow as tf

# case 1:

with tf.name_scope(

's1'):

with tf.name_scope(

's2'):

w1 = tf.variable([1

,2,3

], name=

'w')

bias1 = tf.variable(

[0.1

], name=

'biases'

)print

(w1.name, bias1.name)

# >>> s1/s2/w:0 s1/s2/biases:0

# case 2:

with tf.name_scope(

's1'

)as scope_1:

w2 = tf.variable([1

,2,3

], name=

'w')

bias2 = tf.variable(

[0.1

], name=

'biases'

)with tf.name_scope(

's2'):

w3 = tf.variable([1

,2,3

], name=

'w')

bias3 = tf.variable(

[0.1

], name=

'biases'

)with tf.name_scope(scope_1)

: w4 = tf.variable([1

,2,3

], name=

'w')

bias4 = tf.variable(

[0.1

], name=

'biases'

)print

(w2.name, bias0.name, w3.name, bias3.name, w4.name, bias4.name)

# >>> s1_1/w:0 s1_1/biases:0 s1_1/s2_1/w:0 s1_1/s2_1/biases:0 s1_1/w1_1:0 s1_1/biases_1:0

當傳入已存在的name_scope物件時,則其範圍內變數的字首只與當前傳入的物件有關,與更上層的name_scope無關,如case2所示。

注意:當name_scope重名時,會自動加入字尾,variable也是如此。

常與get_variable搭配使用,多用於變數共享,呼叫方式為:

tf.variable_scope(

'scope_name'

, reuse=

none

)#or

tf.variable_scope(named_scope)

其中 reuse 引數可設為 none、tf.auto_reuse、true、false;

與name_scope一樣:當傳入字串時,用以給變數名新增字首,類似於目錄;

當傳入已存在的variable_scope物件時,則其範圍內變數的字首只與當前傳入的物件有關,與更上層的variable_scope無關。

resue作用域:當 reuse=none(預設情況)時,與上層variable_scope的reuse引數一樣。

當 reuse=tf.auto_reuse 時,自動復用,如果變數存在則復用,不存在則建立。這是最安全的用法。相當於python字典的get方法。

with tf.variable_scope(

's1'):

with tf.variable_scope(

's2'):

init = tf.constant_initializer(

0.1)

w1 = tf.get_variable(

'w',[2

,2])

bias1 = tf.get_variable(

'biases',[

2,2]

)print

(w1.name, bias1.name)

# >>> s1/s2/w:0 s1/s2/biases_1:0

with tf.variable_scope(

's1'

, reuse=tf.auto_reuse)

:with tf.variable_scope(

's2'):

init = tf.constant_initializer(

0.1)

w2 = tf.get_variable(

'w',[2

,2])

bias2 = tf.get_variable(

'biases',[

2,2]

)print

(w2.name, bias2.name)

# >>> s1/s2/w:0 s1/s2/biases_1:0

當 reuse=true 時,tf.get_variable會查詢該命名變數,如果沒有找到,則會報錯;所以設定reuse=true之前,要保證該命名變數已存在。

下述**如為第一次執行,當變數未定義,則會報錯

with tf.variable_scope(

's1'

, reuse=

true):

with tf.variable_scope(

's2'):

init = tf.constant_initializer(

0.1)

w1 = tf.get_variable(

'w',[2

,2])

bias1 = tf.get_variable(

'biases',[

2,2]

)# >>> valueerror: variable s1/s2/w does not exist, or was not created with tf.get_variable(). did you mean to set reuse=tf.auto_reuse in varscope?

當 reuse=false 時,tf.get_variable會呼叫tf.variable來建立變數,並檢查建立的變數是否已存在,如果已存在,則報錯。

with tf.variable_scope(

's1'

, reuse=

true):

with tf.variable_scope(

's2'):

init = tf.constant_initializer(

0.1)

w1 = tf.get_variable(

'w',[2

,2])

bias1 = tf.get_variable(

'biases',[

2,2]

)# first run

# >>> s1/s2/w:0 s1/s2/biases:0

# second run

# >>> valueerror: variable s1/s2/w already exists, disallowed. did you mean to set reuse=true or reuse=tf.auto_reuse in varscope?

上述**第一次執行正確,第二次會發現變數已存在,繼而報錯。

Tensorflow 變數的共享

tensorflow exp example sparse tensor classification train validate.py 當你需要train的過程中validate的時候,如果用placeholder來接收輸入資料 那麼乙個compute graph可以完成這個任務。如果你用的是t...

Tensorflow 變數的共享

tensorflow exp example sparse tensor classification train validate.py 當你需要train的過程中validate的時候,如果用placeholder來接收輸入資料 那麼乙個compute graph可以完成這個任務。如果你用的是t...

Tensorflow 變數的共享

tensorflow exp example sparse tensor classification train validate.py 當你需要train的過程中validate的時候,如果用placeholder來接收輸入資料 那麼乙個compute graph可以完成這個任務。如果你用的是t...