TENSORFLOW變量作用域的示例分析-創(chuàng)新互聯(lián)

這篇文章主要介紹TENSORFLOW變量作用域的示例分析,文中介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們一定要看完!

創(chuàng)新互聯(lián)建站是一家集網(wǎng)站建設(shè),汝州企業(yè)網(wǎng)站建設(shè),汝州品牌網(wǎng)站建設(shè),網(wǎng)站定制,汝州網(wǎng)站建設(shè)報(bào)價(jià),網(wǎng)絡(luò)營銷,網(wǎng)絡(luò)優(yōu)化,汝州網(wǎng)站推廣為一體的創(chuàng)新建站企業(yè),幫助傳統(tǒng)企業(yè)提升企業(yè)形象加強(qiáng)企業(yè)競爭力??沙浞譂M足這一群體相比中小企業(yè)更為豐富、高端、多元的互聯(lián)網(wǎng)需求。同時(shí)我們時(shí)刻保持專業(yè)、時(shí)尚、前沿,時(shí)刻以成就客戶成長自我,堅(jiān)持不斷學(xué)習(xí)、思考、沉淀、凈化自己,讓我們?yōu)楦嗟钠髽I(yè)打造出實(shí)用型網(wǎng)站。

舉例說明

TensorFlow中的變量一般就是模型的參數(shù)。當(dāng)模型復(fù)雜的時(shí)候共享變量會(huì)無比復(fù)雜。

官網(wǎng)給了一個(gè)case,當(dāng)創(chuàng)建兩層卷積的過濾器時(shí),每輸入一次圖片就會(huì)創(chuàng)建一次過濾器對(duì)應(yīng)的變量,但是我們希望所有圖片都共享同一過濾器變量,一共有4個(gè)變量:conv1_weights,conv1_biases,conv2_weights, and conv2_biases。

通常的做法是將這些變量設(shè)置為全局變量。但是存在的問題是打破封裝性,這些變量必須文檔化被其他代碼文件引用,一旦代碼變化,調(diào)用方也可能需要變化。

還有一種保證封裝性的方式是將模型封裝成類。

不過TensorFlow提供了Variable Scope 這種獨(dú)特的機(jī)制來共享變量。這個(gè)機(jī)制涉及兩個(gè)主要函數(shù):

tf.get_variable(<name>, <shape>, <initializer>) 創(chuàng)建或返回給定名稱的變量
tf.variable_scope(<scope_name>) 管理傳給get_variable()的變量名稱的作用域

在下面的代碼中,通過tf.get_variable()創(chuàng)建了名稱分別為weights和biases的兩個(gè)變量。

def conv_relu(input, kernel_shape, bias_shape):
  # Create variable named "weights".
  weights = tf.get_variable("weights", kernel_shape,
    initializer=tf.random_normal_initializer())
  # Create variable named "biases".
  biases = tf.get_variable("biases", bias_shape,
    initializer=tf.constant_initializer(0.0))
  conv = tf.nn.conv2d(input, weights,
    strides=[1, 1, 1, 1], padding='SAME')
  return tf.nn.relu(conv + biases)

但是我們需要兩個(gè)卷積層,這時(shí)可以通過tf.variable_scope()指定作用域進(jìn)行區(qū)分,如with tf.variable_scope("conv1")這行代碼指定了第一個(gè)卷積層作用域?yàn)閏onv1,

在這個(gè)作用域下有兩個(gè)變量weights和biases。

def my_image_filter(input_images):
  with tf.variable_scope("conv1"):
    # Variables created here will be named "conv1/weights", "conv1/biases".
    relu1 = conv_relu(input_images, [5, 5, 32, 32], [32])
  with tf.variable_scope("conv2"):
    # Variables created here will be named "conv2/weights", "conv2/biases".
    return conv_relu(relu1, [5, 5, 32, 32], [32])

最后在image_filters這個(gè)作用域重復(fù)使用第一張圖片輸入時(shí)創(chuàng)建的變量,調(diào)用函數(shù)reuse_variables(),代碼如下:

with tf.variable_scope("image_filters") as scope:
  result1 = my_image_filter(image1)
  scope.reuse_variables()
  result2 = my_image_filter(image2)

tf.get_variable()工作機(jī)制

tf.get_variable()工作機(jī)制是這樣的:

當(dāng)tf.get_variable_scope().reuse == False,調(diào)用該函數(shù)會(huì)創(chuàng)建新的變量

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

當(dāng)tf.get_variable_scope().reuse == True,調(diào)用該函數(shù)會(huì)重用已經(jīng)創(chuàng)建的變量

with tf.variable_scope("foo"):
  v = tf.get_variable("v", [1])
with tf.variable_scope("foo", reuse=True):
  v1 = tf.get_variable("v", [1])
assert v1 is v

變量都是通過作用域/變量名來標(biāo)識(shí),后面會(huì)看到作用域可以像文件路徑一樣嵌套。

tf.variable_scope理解

tf.variable_scope()用來指定變量的作用域,作為變量名的前綴,支持嵌套,如下:

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

當(dāng)前環(huán)境的作用域可以通過函數(shù)tf.get_variable_scope()獲取,并且reuse標(biāo)志可以通過調(diào)用reuse_variables()設(shè)置為True,這個(gè)非常有用,如下

with tf.variable_scope("foo"):
  v = tf.get_variable("v", [1])
  tf.get_variable_scope().reuse_variables()
  v1 = tf.get_variable("v", [1])
assert v1 is v

作用域中的resuse默認(rèn)是False,調(diào)用函數(shù)reuse_variables()可設(shè)置為True,一旦設(shè)置為True,就不能返回到False,并且該作用域的子空間reuse都是True。如果不想重用變量,那么可以退回到上層作用域,相當(dāng)于exit當(dāng)前作用域,如

with tf.variable_scope("root"):
  # At start, the scope is not reusing.
  assert tf.get_variable_scope().reuse == False
  with tf.variable_scope("foo"):
    # Opened a sub-scope, still not reusing.
    assert tf.get_variable_scope().reuse == False
  with tf.variable_scope("foo", reuse=True):
    # Explicitly opened a reusing scope.
    assert tf.get_variable_scope().reuse == True
    with tf.variable_scope("bar"):
      # Now sub-scope inherits the reuse flag.
      assert tf.get_variable_scope().reuse == True
  # Exited the reusing scope, back to a non-reusing one.
  assert tf.get_variable_scope().reuse == False

一個(gè)作用域可以作為另一個(gè)新的作用域的參數(shù),如:

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])
with tf.variable_scope(foo_scope, reuse=True):
  v1 = tf.get_variable("v", [1])
  w1 = tf.get_variable("w", [1])
assert v1 is v
assert w1 is w

不管作用域如何嵌套,當(dāng)使用with tf.variable_scope()打開一個(gè)已經(jīng)存在的作用域時(shí),就會(huì)跳轉(zhuǎn)到這個(gè)作用域。

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" # Not changed.

variable scope的Initializers可以創(chuàng)遞給子空間和tf.get_variable()函數(shù),除非中間有函數(shù)改變,否則不變。

with tf.variable_scope("foo", initializer=tf.constant_initializer(0.4)):
  v = tf.get_variable("v", [1])
  assert v.eval() == 0.4 # Default initializer as set above.
  w = tf.get_variable("w", [1], initializer=tf.constant_initializer(0.3)):
  assert w.eval() == 0.3 # Specific initializer overrides the default.
  with tf.variable_scope("bar"):
    v = tf.get_variable("v", [1])
    assert v.eval() == 0.4 # Inherited default initializer.
  with tf.variable_scope("baz", initializer=tf.constant_initializer(0.2)):
    v = tf.get_variable("v", [1])
    assert v.eval() == 0.2 # Changed default initializer.

算子(ops)會(huì)受變量作用域(variable scope)影響,相當(dāng)于隱式地打開了同名的名稱作用域(name scope),如+這個(gè)算子的名稱為foo/add

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

除了變量作用域(variable scope),還可以顯式打開名稱作用域(name scope),名稱作用域僅僅影響算子的名稱,不影響變量的名稱。另外如果tf.variable_scope()傳入字符參數(shù),創(chuàng)建變量作用域的同時(shí)會(huì)隱式創(chuàng)建同名的名稱作用域。如下面的例子,變量v的作用域是foo,而算子x的算子變?yōu)閒oo/bar,因?yàn)橛须[式創(chuàng)建名稱作用域foo

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

注意: 如果tf.variable_scope()傳入的不是字符串而是scope對(duì)象,則不會(huì)隱式創(chuàng)建同名的名稱作用域。

以上是“TENSORFLOW變量作用域的示例分析”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對(duì)大家有幫助,更多相關(guān)知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!

文章名稱:TENSORFLOW變量作用域的示例分析-創(chuàng)新互聯(lián)
標(biāo)題路徑:http://muchs.cn/article18/dcjidp.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供App設(shè)計(jì)、App開發(fā)網(wǎng)站改版、小程序開發(fā)定制開發(fā)、全網(wǎng)營銷推廣

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

微信小程序開發(fā)