關于tensorflow中的reuse=True ,or,False
在tf.variable_scope()函數(shù)中,設置reuse=False時炼蛤,在其命名空間"foo"中執(zhí)行函數(shù)get_variable()時,表示創(chuàng)建變量"v"瘦馍,若在該命名空間中已經有了變量"v"情龄,則在創(chuàng)建時會報錯
設置reuse=True時,函數(shù)get_variable()表示獲取變量如下列
import tensorflow as tf
with tf.variable_scope("foo"):
? ? v=tf.get_variable("v",[1],initializer=tf.constant_initializer(1.0))
with tf.variable_scope("foo",reuse=True):
? ? v1=tf.get_variable("v",[1])
print(v1==v)
結果為:
True
在tf.variable_scope()函數(shù)中癣丧,設置reuse=True時槽畔,在其命名空間"foo"中執(zhí)行函數(shù)get_variable()時,表示獲取變量"v"胁编。若在該命名空間中還沒有該變量厢钧,則在獲取時會報錯鳞尔,如下面的例子
import tensorflow as tf
with tf.variable_scope("foo",reuse=True):
? ? v1=tf.get_variable("v",[1])
ValueError? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? Traceback (most recent call last)
<ipython-input-1-019a05c4b9a4> in <module>()
? ? ? 2
? ? ? 3 with tf.variable_scope("foo",reuse=True):
----> 4? ? v1=tf.get_variable("v",[1])
? ? ? 5
ValueError: Variable foo/v does not exist, or was not created with tf.get_variable().
Did you mean to set reuse=tf.AUTO_REUSE in VarScope?
TensorFlow通過tf. get_variable()和tf.variable_scope()兩個函數(shù),可以創(chuàng)建多個并列的或嵌套的命名空間早直,用于存儲神經網絡中的各層的權重寥假、偏置、學習率霞扬、滑動平均衰減率糕韧、正則化系數(shù)等參數(shù)值,神經網絡不同層的參數(shù)可放置在不同的命名空間中喻圃。同時萤彩,變量重用檢錯和讀取不存在變量檢錯兩種機制保證了數(shù)據(jù)存放的安全性。