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

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

tf.name_scope与tf.variable_scope

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

1.scope(作用域)

??在TensorFlow中有兩個作用域 (scope),一個是variable_scope,另一個是name_scope。它們究竟有什么區(qū)別呢?簡而言之,variable_scope主要是給variable_name加前綴,也可以給op_name加前綴;name_scope是給op_name加前綴。下面我們就來分別介紹。

2. variable_scope示例

2.1 reuse=False/True

variable_scope變量作用域機(jī)制在TensorFlow中主要由兩部分組成:

v = tf.get_variable(name, shape, dtype, initializer) # 通過所給的名字創(chuàng)建或是返回一個變量 tf.variable_scope(<scope_name>) # 為變量指定命名空間

當(dāng)tf.get_variable_scope().reuse == False時,variable_scope作用域只能用來創(chuàng)建新變量:

with tf.variable_scope("foo"):v = tf.get_variable("v", [1])v2 = tf.get_variable("v", [1]) assert v.name == "foo/v:0"

note:
????上述程序會拋出V alueError錯誤,因為v這個變量已經(jīng)被定義過了,但tf.get_variable_scope().reuse默認(rèn)為False,所以不能重用。

當(dāng)tf.get_variable_scope().reuse == True時,作用域可以共享變量:

with tf.variable_scope("foo") as scope:v = tf.get_variable("v", [1]) with tf.variable_scope("foo", reuse=True):#也可以寫成:#scope.reuse_variables()v1 = tf.get_variable("v", [1]) assert v1 == v

2.2 獲取變量作用域

可以直接通過tf.variable_scope()來獲取變量作用域:

with tf.variable_scope("foo") as foo_scope: v = tf.get_variable("v", [1]) with tf.variable_scope(foo_scope) w = tf.get_variable("w", [1])

如果在開啟的一個變量作用域里使用之前預(yù)先定義的一個作用域,則會跳過當(dāng)前變量的作用域,保持預(yù)先存在的作用域不變。

with tf.variable_scope("foo") as foo_scope:assert foo_scope.name == "foo" with tf.variable_scope("bar")with tf.variable_scope("baz") as other_scope:assert other_scope.name == "bar/baz"with tf.variable_scope(foo_scope) as foo_scope2:assert foo_scope2.name == "foo" # 保持不變

2.3 變量作用域的初始化

變量作用域可以默認(rèn)攜帶一個初始化器,在這個作用域中的子作用域或變量都可以繼承或者重寫父作用域初始化器中的值。方法如下:

with tf.variable_scope("foo", initializer=tf.constant_initializer(0.4)):v = tf.get_variable("v", [1])assert v.eval() == 0.4 # 被作用域初始化w = tf.get_variable("w", [1], initializer=tf.constant_initializer(0.3)):assert w.eval() == 0.3 # 重寫初始化器的值with tf.variable_scope("bar"):v = tf.get_variable("v", [1])assert v.eval() == 0.4 # 繼承默認(rèn)的初始化器with tf.variable_scope("baz", initializer=tf.constant_initializer(0.2)):v = tf.get_variable("v", [1])assert v.eval() == 0.2 # 重寫父作用域的初始化器的值

上面講的是variable_name,那對于op_name呢?在variable_scope作用域下的操作,也會被加上前綴:

with tf.variable_scope("foo"):x = 1.0 + tf.get_variable("v", [1]) assert x.op.name == "foo/add"

3.name_scope示例

name_scope會影響op_name,不會影響用get_variable()創(chuàng)建的變量,而會影響通過V ariable()創(chuàng)建的變量。因此:

with tf.variable_scope("foo"):with tf.name_scope("bar"):v = tf.get_variable("v", [1])b = tf.Variable(tf.zeros([1]), name='b')x = 1.0 + v assert v.name == "foo/v:0" assert b.name == "foo/bar/b:0" assert x.op.name == "foo/bar/add"

可以看出,tf.name_scope()返回的是一個字符串,如上述的"bar"。name_scope對用get_variable()創(chuàng)建的變量的名字不會有任何影響,而V ariable()創(chuàng)建的操作會被加上前綴,并且會給操作加上名字前綴。

4.參考

https://blog.csdn.net/scotthuang1989/article/details/77477026
https://blog.csdn.net/lucky7213/article/details/78967306

總結(jié)

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

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。