tf.name_scope tf.variable_scope学习
生活随笔
收集整理的這篇文章主要介紹了
tf.name_scope tf.variable_scope学习
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
1. 首先看看比較簡(jiǎn)單的 tf.name_scope(‘scope_name’).
tf.name_scope 主要結(jié)合 tf.Variable() 來(lái)使用,方便參數(shù)命名管理。
''' Signature: tf.name_scope(*args, **kwds) Docstring: Returns a context manager for use when defining a Python op. ''' # 也就是說(shuō),它的主要目的是為了更加方便地管理參數(shù)命名。 # 與 tf.Variable() 結(jié)合使用。簡(jiǎn)化了命名 with tf.name_scope('conv1') as scope:weights1 = tf.Variable([1.0, 2.0], name='weights')bias1 = tf.Variable([0.3], name='bias')# 下面是在另外一個(gè)命名空間來(lái)定義變量的 with tf.name_scope('conv2') as scope:weights2 = tf.Variable([4.0, 2.0], name='weights')bias2 = tf.Variable([0.33], name='bias')# 所以,實(shí)際上weights1 和 weights2 這兩個(gè)引用名指向了不同的空間,不會(huì)沖突 print (weights1.name) print (weights2.name)#輸出: conv1/weights:0 conv2/weights:0
# 注意,這里的 with 和 python 中其他的 with 是不一樣的 # 執(zhí)行完 with 里邊的語(yǔ)句之后,這個(gè) conv1/ 和 conv2/ 空間還是在內(nèi)存中的。這時(shí)候如果再次執(zhí)行上面的代碼 # 就會(huì)再生成其他命名空間 with tf.name_scope('conv1') as scope:weights1 = tf.Variable([1.0, 2.0], name='weights')bias1 = tf.Variable([0.3], name='bias')with tf.name_scope('conv2') as scope:weights2 = tf.Variable([4.0, 2.0], name='weights')bias2 = tf.Variable([0.33], name='bias')print (weights1.name) print (weights2.name) #輸出 conv1_1/weights:0 conv2_1/weights:0
2.下面來(lái)看看 tf.variable_scope(‘scope_name’)
tf.variable_scope() 主要結(jié)合 tf.get_variable() 來(lái)使用,實(shí)現(xiàn) 變量共享。
# 這里是正確的打開(kāi)方式~~~可以看出,name 參數(shù)才是對(duì)象的唯一標(biāo)識(shí) import tensorflow as tf with tf.variable_scope('v_scope') as scope1:Weights1 = tf.get_variable('Weights', shape=[2,3])bias1 = tf.get_variable('bias', shape=[3])# 下面來(lái)共享上面已經(jīng)定義好的變量 # note: 在下面的 scope 中的變量必須已經(jīng)定義過(guò)了,才能設(shè)置 reuse=True,否則會(huì)報(bào)錯(cuò) with tf.variable_scope('v_scope', reuse=True) as scope2:Weights2 = tf.get_variable('Weights')print (Weights1.name) print (Weights2.name) # 可以看到這兩個(gè)引用名稱指向的是同一個(gè)內(nèi)存對(duì)象 #輸出 v_scope/Weights:0 v_scope/Weights:0也可以結(jié)合 tf.Variable() 一塊使用。
import tensorflow as tf # 注意, bias1 的定義方式 with tf.variable_scope('v_scope') as scope1:Weights1 = tf.get_variable('Weights', shape=[2,3]) # bias1 = tf.Variable([0.52], name='bias')# 下面來(lái)共享上面已經(jīng)定義好的變量 # note: 在下面的 scope 中的get_variable()變量必須已經(jīng)定義過(guò)了,才能設(shè)置 reuse=True,否則會(huì)報(bào)錯(cuò) with tf.variable_scope('v_scope', reuse=True) as scope2:Weights2 = tf.get_variable('Weights')bias2 = tf.Variable([0.52], name='bias')print (Weights1.name) print (Weights2.name) print (bias2.name) #輸出:v_scope/Weights:0 v_scope/Weights:0 v_scope_1/bias:0#新的命名空間
如果 reuse=True 的scope中的變量沒(méi)有已經(jīng)定義,會(huì)報(bào)錯(cuò)!!
import tensorflow as tf # 注意, bias1 的定義方式 with tf.variable_scope('v_scope') as scope1:Weights1 = tf.get_variable('Weights', shape=[2,3])bias1 = tf.Variable([0.52], name='bias')print (Weights1.name) print (bias1.name)# 下面來(lái)共享上面已經(jīng)定義好的變量 # note: 在下面的 scope 中的get_variable()變量必須已經(jīng)定義過(guò)了,才能設(shè)置 reuse=True,否則會(huì)報(bào)錯(cuò) with tf.variable_scope('v_scope', reuse=True) as scope2:Weights2 = tf.get_variable('Weights')bias2 = tf.get_variable('bias', [1]) # ‘bias未定義,所以會(huì)報(bào)錯(cuò)print (Weights2.name) print (bias2.name)# 這樣子的話就會(huì)報(bào)錯(cuò) # Variable v_scope/bias does not exist, or was not created with tf.get_variable() #輸出 v_scope/Weights:0 v_scope/bias:0本文代碼:https://github.com/yongyehuang/Tensorflow-Tutorial
轉(zhuǎn)載https://www.cnblogs.com/adong7639/p/8136273.html
轉(zhuǎn)載于:https://www.cnblogs.com/gaofighting/p/9626584.html
總結(jié)
以上是生活随笔為你收集整理的tf.name_scope tf.variable_scope学习的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 时间复杂度与O(1), O(n), O(
- 下一篇: javaScript高程笔记--最佳实践