1樓:「已登出」
不逼逼,貼原始碼和注釋
,還不懂你打死我
# coding: utf-8
import
osimport
sysimport
time
import
reimport
math
import
numpy
asnp
import
pandas
aspd
import
tensorflow
astf##
("""
# 規則1: name_scope只會影響通過非tf.get_variable 構造的變數, 不會影響通過 get_variable 構造的變數
""")tf.
reset_default_graph
()withtf.
name_scope
("first"
):withtf.
name_scope
("second"
):v1=tf
.constant(2
,name
="cons")v2
=tf.Variable(2
,name
="var")v3
=tf.multiply(tf
.constant(2
),tf
.constant(3
),name
="multi")v4
=tf.get_variable
("get_v",[
1])# 不會有first/second/字首
print(v1
.name,v2
.name,v3
.name,v4
.name
,sep='
\n')# first/second/cons:0
# first/second/var:0
# first/second/multi:0
# get_v:0##
####
("""
# 規則2: 預設情況下,重新開啟同乙個name_scope(name引數相同),會幫你建立乙個新的name_scope, 名字是在之前的基礎上加上_n
# 比如之前是first, 再次開啟就是first_1.
# 如果想要避免這種情況, 可以在再次開啟相同scope時帶上/, 但是此時就會在變數名字後面帶上字尾_n
(1): name_scope中以 / 結尾的名字都是完整的名字,不會在前面加上字首
(2): "" 或者 None 作為name_scope的名字引數, name_scope會被重置為頂層name_scope
(3): 其他情況下, 當前name_scope名字作為字尾新增進去, 如果已經存在了, 則再在後面加上字尾_n
""")tf.
reset_default_graph
()withtf.
name_scope
("first"
):v1=tf
.constant(2
,name
="cons")v2
=tf.Variable(2
,name
="var")v3
=tf.multiply(tf
.constant(2
),tf
.constant(3
),name
="multi")v4
=tf.get_variable
("get_v",[
1])# 不會有first/second/字首
withtf.
name_scope
("first"
):v11=tf
.constant(2
,name
="cons"
)v21=tf
.Variable(2
,name
="var"
)v31=tf
.multiply(tf
.constant(2
),tf
.constant(3
),name
="multi"
)v41=tf
.get_variable
("get_v1",[
1])# 不會受到name_scope影響
print(v1
.name,v2
.name,v3
.name,v4
.name
,sep='
\n')print
(v11
.name
,v21
.name
,v31
.name
,v41
.name
,sep='
\n')# first/cons:0
# first/var:0
# first/multi:0
# get_v:0
# first_1/cons:0
# first_1/var:0
# first_1/multi:0
# get_v1:0
print(൪
)tf.reset_default_graph
()withtf.
name_scope
("first"
):v1=tf
.constant(2
,name
="cons")v2
=tf.Variable(2
,name
="var")v3
=tf.multiply(tf
.constant(2
),tf
.constant(3
),name
="multi")v4
=tf.get_variable
("get_v",[
1])# 不會有first/second/字首
# 雖然此時再次開啟的name_scope是一樣的了,但是仍然不是同乙個變數,tf會把下面的Variable變數加上字尾_n
withtf.
name_scope
("first/"
):v11=tf
.constant(2
,name
="cons"
)v21=tf
.Variable(2
,name
="var"
)v31=tf
.multiply(tf
.constant(2
),tf
.constant(3
),name
="multi"
)v41=tf
.get_variable
("get_v1",[
1])# 不會受到name_scope影響,因此沒有first/字首
# 這裡v411沒有name_scope, 名字也為get_v1,看起來等同於v41,其實不是, 通過tf.Variable建立的變數
# 如果有相同name的變數存在,則tf會自動在name引數後面帶上字尾_n
v411=tf
.Variable(2
,name
="get_v1"
)print(v1
.name,v2
.name,v3
.name,v4
.name
,sep='
\n')print
(v11
.name
,v21
.name
,v31
.name
,v41
.name
,sep='
\n')print(v1
==v11,v2
==v21,v3
==v31,v4
==v41
(v411
.name
,v41
.name
,v411
==v41
)# first/cons:0
# first/var:0
# first/multi:0
# get_v:0
# first/cons_1:0
# first/var_1:0
# first/multi_1:0
# get_v1:0
# False False False False
# get_v1_1:0 get_v1:0 False
("""
# 規則3: variable_scope被get_variable使用, 且再次開啟同乙個variable_scope,預設情況下是復用的
,不像name_scope預設不復用(需要以/結尾)
""")tf.
reset_default_graph
()withtf.
variable_scope
("first"
):withtf.
variable_scope
("second"
):# first/second/get_v:0
print(tf
.get_variable
("get_v",[
1]).name)tf
.reset_default_graph
()withtf.
variable_scope
("first"
):pass
withtf.
variable_scope
("first"
):# first/get_variable:0
print(tf
.get_variable
("get_variable",[
1]).name
("""
規則4: 預設情況下:建立variable_scope時會同時建立乙個同名的name_scope(多次開啟時預設行為不一樣)
(規則1, name_scope不影響 get_variable建立的變數)
所以,如果多次開啟同乙個variable_scope,預設情況下第一次的name_scope和variable_scope是一樣名字
但是第二次過後,會在名字後面加上字尾_n(參考規則2)
但是我們可以設定 auxiliary_name_scope = False 阻止預設行為.
或者新增name_scope以 / 結尾 "" None作為引數也可以
""")tf.
reset_default_graph
()withtf.
variable_scope
("first"
):v1=tf
.constant(2
,name
="cons")v2
=tf.Variable(2
,name
="var")v3
=tf.multiply(tf
.constant(2
),tf
.constant(3
),name
="multi")v4
=tf.get_variable
("get_v",[
1])print(v1
.name,v2
.name,v3
.name,v4
.name
,sep='
\n')# first/cons:0
# first/var:0
# first/multi:0
# first/get_v:0
print(൪
)tf.reset_default_graph
()withtf.
variable_scope
("first"
):pass
withtf.
variable_scope
("first"
):v1=tf
.constant(2
,name
="cons")v2
=tf.Variable(2
,name
="var")v3
=tf.multiply(tf
.constant(2
),tf
.constant(3
),name
="multi")v4
=tf.get_variable
("get_v",[
1])print(v1
.name,v2
.name,v3
.name,v4
.name
,sep='
\n')# first_1/cons:0
# first_1/var:0
# first_1/multi:0
# first/get_v:0tf.
reset_default_graph
()withtf.
variable_scope
("first"
):pass
withtf.
variable_scope
("first"
,auxiliary_name_scope
=False
):# 阻止variable_scope自動建立name_scopev1=
tf.constant(2
,name
="cons")v2
=tf.Variable(2
,name
="var")v3
=tf.multiply(tf
.constant(2
),tf
.constant(3
),name
="multi")v4
=tf.get_variable
("get_v",[
1])print(v1
.name,v2
.name,v3
.name,v4
.name
,sep='
\n')# cons:0 # 頂層變數
# var:0
# multi:0
# first/get_v:0
("練習題34;
("ex134;)tf
.reset_default_graph
()withtf.
variable_scope
("first"
):# 規則4
print(tf
.constant(2
,name
="c").
name
)# first/c
print(tf
.Variable(2
,name
="v").
name
)# first/v
print(tf
.multiply(tf
.constant(2
),tf
.constant(3
),name
="m").
name
)# first/m
print(tf
.get_variable
("g",[
1]).name
)# first/g
("ex234;)tf
.reset_default_graph
()withtf.
variable_scope
("first"
):pass
withtf.
variable_scope
("first"
):# 規則4
print(tf
.constant(2
,name
="c").
name
)# first_1/c
print(tf
.Variable(2
,name
="v").
name
)# first_1/v
print(tf
.multiply(tf
.constant(2
),tf
.constant(3
),name
="m").
name
)# first_1/g
print(tf
.get_variable
("g",[
1]).name
)# first/g
("ex334;)tf
.reset_default_graph
()withtf.
variable_scope
("first"
,auxiliary_name_scope
=False
):print(tf
.constant(2
,name
="c").
name
)# c
print(tf
.Variable(2
,name
="v").
name
)# v
print(tf
.multiply(tf
.constant(2
),tf
.constant(3
),name
="m").
name
)# m
print(tf
.get_variable
("g",[
1]).name
)# first/g
("ex434;)tf
.reset_default_graph
()withtf.
variable_scope
("first"
):# 規則4+規則2
withtf.
name_scope
("another/"
):# note the trailing slash
print(tf
.constant(2
,name
="c").
name
)# another/c
print(tf
.Variable(2
,name
="v").
name
)# another/v
print(tf
.multiply(tf
.constant(2
),tf
.constant(3
),name
="m").
name
)# another/m
print(tf
.get_variable
("g",[
1]).name
)# first/g
("ex534;)tf
.reset_default_graph
()withtf.
variable_scope
("first")as
scope
:pass
withtf.
variable_scope
(scope
,auxiliary_name_scope
=False
):withtf.
name_scope
(scope
.original_name_scope
):print(tf
.constant(2
,name
="c").
name
)# first/c
print(tf
.Variable(2
,name
="v").
name
)# first/v
print(tf
.multiply(tf
.constant(2
),tf
.constant(3
),name
="m").
name
)## first/m
print(tf
.get_variable
("g",[
1]).name
)# first/g
("ex634;)tf
.reset_default_graph
()withtf.
variable_scope
("first"
):pass
withtf.
variable_scope
("first"
,auxiliary_name_scope
=False
):withtf.
name_scope
("first/"
):print(tf
.constant(2
,name
="c").
name
)# first/c
print(tf
.Variable(2
,name
="v").
name
)# first/v
print(tf
.multiply(tf
.constant(2
),tf
.constant(3
),name
="m").
name
)# first/m
print(tf
.get_variable
("g",[
1]).name
)# first/g
Tensorflow 中 Embedding 層為什麼要用矩陣乘來實現?
一棵有思想的韭菜 我的猜想是這樣的 用one hot matrix 乘 embedding matrix來選擇,對與GPU來說是非常高效的,如果用CPU就會慢很多,但是如果這個實現用的是你說的index定址,這就實打實的是線性時間來,有GPU也無法加速。 Chan Yu 引用一下官方1.14版本的文...
tensorflow和pytorch是否可以在同乙個環境下使用?
阿爾法 浪 估計題主先使用的TensorFlow在這塊卡上,並沒有設定TensorFlow gpu相關配置,預設TensorFlow會將整塊gpu卡占滿,因此在跑pytorch的時候,此卡就不可用了。 陳桓 不建立虛擬環境會有一些麻煩,你可以建立乙個conda環境,在這乙個環境裡面同時裝兩套,然後將...
pytorch與tensorflow未來哪乙個會佔據更大的使用者群體?
HetingJian pytorch永遠滴神,對於初學者半個小時就能寫個CNN還要啥自行車,我是完全從不會入手的pytorch,就簡單的功能來說對新手太友好遼。 破圓的角 這麼說吧。很久沒用tf,2.0出來的時候用colab玩了一下。我人傻了。推薦用顯式的檢測錯誤的方式來檢測epoch的結束。確實我...