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

歡迎訪問 生活随笔!

生活随笔

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

编程问答

tf.name_scope tf.variable_scope学习

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

1. 首先看看比較簡單的 tf.name_scope(‘scope_name’).

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

''' Signature: tf.name_scope(*args, **kwds) Docstring: Returns a context manager for use when defining a Python op. ''' # 也就是說,它的主要目的是為了更加方便地管理參數命名。 # 與 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')print (Weights1.name) print (bias1.name)# 下面來共享上面已經定義好的變量 # 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]) # ‘bias未定義,所以會報錯print (Weights2.name) print (bias2.name)# 這樣子的話就會報錯 # 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

轉載https://www.cnblogs.com/adong7639/p/8136273.html

轉載于:https://www.cnblogs.com/gaofighting/p/9626584.html

總結

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

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