TensorFlow函數(三)tf.variable_scope() 和 tf.name_scope()


tf.name_scope()

此函數作用是共享變量。在一個作用域scope內共享一些變量,簡單來說,就是給變量名前面加個變量空間名,只限於tf.Variable()的變量

tf.variable_scope()

和tf.name_scope()作用一樣,不過包括tf.get_variable()的變量和tf.Variable()的變量

在同一個程序中多次調用,在第一次調用之后需要將reuse參數設置為True

1 with tf.variable_scope("one"):
2     a = tf.get_variable("v", [1]) #a.name == "one/v:0"
3 with tf.variable_scope("one"):
4     b = tf.get_variable("v", [1]) #創建兩個名字一樣的變量會報錯 ValueError: Variable one/v already exists 
5 with tf.variable_scope("one", reuse = True): #注意reuse的作用。
6     c = tf.get_variable("v", [1]) #c.name == "one/v:0" 成功共享,因為設置了reuse

 


免責聲明!

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



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