日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

tf.name_scope()与tf.variable_scope()

發布時間:2025/3/21 编程问答 17 豆豆
生活随笔 收集整理的這篇文章主要介紹了 tf.name_scope()与tf.variable_scope() 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

? ? ? ? TensorFlow的tf.name_scope()、tf.variable_scope()是兩個作用域函數,一般與兩個創建/調用變量的函數tf.variable() 和tf.get_variable()搭配使用。常用于:1)變量共享;2)tensorboard畫流程圖進行可視化封裝變量。

? ? ? ?命名域?(name scope),通過tf.name_scope 或 tf.op_scope創建;
? ? ? ?變量域?(variable scope),通過tf.variable_scope 或 tf.variable_op_scope創建。
? ? ? ?通俗理解就是:tf.name_scope()、tf.variable_scope()會在模型中開辟各自的空間,而其中的變量均在這個空間內進行管理,但是之所以有兩個,主要還是有著各自的區別。

? ? ?(1)tf.Variable() :每次都會新建變量。

? ? ?(2)tf.get_variable():如果希望重用(共享)一些變量,就需要用到tf.get_variable(),它會去搜索變量名,有就直接用,沒有再新建。擁有一個變量檢查機制,會檢測已經存在的變量是否設置為共享變量,如果已經存在的變量沒有設置為共享變量,TensorFlow 運行到第二個擁有相同名字的變量的時候,就會報錯。

1.?tf.name_scope(‘scope_name’)

tf.name_scope 主要結合 tf.Variable() 來使用,方便參數命名管理。

# 在一個命名空間來定義變量 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')# 所以,實際上weights1 和 weights2 這兩個引用名指向了不同的空間,不會沖突 print(weights1.name) print(weights2.name)''' conv1/weights:0 conv2/weights:0 '''

這時候如果再次執行上面的代碼, 就會再生成其他命名空間:?

# 注意,這里的 with 和 python 中其他的 with 是不一樣的 # 執行完 with 里邊的語句之后,這個 conv1/ 和 conv2/ 空間還是在內存中的。 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.下面來看看 tf.variable_scope(‘scope_name’)

? ? ?tf.variable_scope() 主要結合 tf.get_variable() 來使用,實現 變量共享。

# 這里是正確的打開方式~~~可以看出,name 參數才是對象的唯一標識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])# 下面來共享上面已經定義好的變量 # note: 在下面的 scope 中的變量必須已經定義過了,才能設置 reuse=True,否則會報錯 with tf.variable_scope('v_scope', reuse=True) as scope2:Weights2 = tf.get_variable('Weights')print(Weights1.name) print(Weights2.name)# 可以看到這兩個引用名稱指向的是同一個內存對象''' v_scope/Weights:0 v_scope/Weights:0 '''

也可以結合 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')# 下面來共享上面已經定義好的變量 # note: 在下面的 scope 中的get_variable()變量必須已經定義過了,才能設置 reuse=True,否則會報錯 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中的變量沒有已經定義,會報錯!!

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') # tf.Variable不會報錯print(Weights1.name) print(bias1.name)''' v_scope/Weights:0 v_scope/bias:0 '''# 下面來共享上面已經定義好的變量 # note: 在下面的 scope 中的get_variable()變量必須已經定義過了,才能設置 reuse=True,否則會報錯 with tf.variable_scope('v_scope', reuse=True) as scope2:Weights2 = tf.get_variable('Weights')bias2 = tf.get_variable('bias', [1]) # tf.get_variable會報錯print(Weights2.name) print(bias2.name)# 這樣會報錯 # Variable v_scope/bias does not exist, or was not created with tf.get_variable()

?

總結

以上是生活随笔為你收集整理的tf.name_scope()与tf.variable_scope()的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。