【譯】Effective TensorFlow Chapter3——理解變量域Scope以及什麼時候應該使用它們

本文翻譯自: 《Scopes and when to use them》, 若有侵權請聯繫刪除,僅限於學術交流,請勿商用。若有謬誤,請聯繫指出。python

在TensorFlow中,變量(Variables)和張量(tensors)有一個名字(name)屬性,用於在符號圖中標識它們。若是在建立變量或張量時未指定名稱,TensorFlow會自動爲您指定名稱:git

a = tf.constant(1)
print(a.name)  # prints "Const:0"

b = tf.Variable(1)
print(b.name)  # prints "Variable:0"
複製代碼

您能夠經過顯式指定來覆蓋默認名稱:github

a = tf.constant(1, name="a")
print(a.name)  # prints "a:0"

b = tf.Variable(1, name="b")
print(b.name)  # prints "b:0"
複製代碼

TensorFlow引入了兩個不一樣的上下文管理器來改變張量和變量的名稱。第一個是tf.name_scopebash

with tf.name_scope("scope"):
  a = tf.constant(1, name="a")
  print(a.name)  # prints "scope/a:0"

  b = tf.Variable(1, name="b")
  print(b.name)  # prints "scope/b:0"

  c = tf.get_variable(name="c", shape=[])
  print(c.name)  # prints "c:0"
複製代碼

請注意,有兩種方法能夠在TensorFlow中定義新變量,一是建立tf.Variable對象或是調用tf.get_variable方法。使用新名稱調用tf.get_variable會致使建立新變量,但若是存在具備相同名稱的變量,則會引起ValueError異常,告訴咱們不容許從新聲明變量。網絡

tf.name_scope影響使用tf.Variable建立的張量和變量的名稱,但不影響使用tf.get_variable建立的變量。函數

tf.name_scope不一樣,tf.variable_scope也修改了使用tf.get_variable建立的變量的名稱:ui

with tf.variable_scope("scope"):
  a = tf.constant(1, name="a")
  print(a.name)  # prints "scope/a:0"

  b = tf.Variable(1, name="b")
  print(b.name)  # prints "scope/b:0"

  c = tf.get_variable(name="c", shape=[])
  print(c.name)  # prints "scope/c:0"
with tf.variable_scope("scope"):
  a1 = tf.get_variable(name="a", shape=[])
  a2 = tf.get_variable(name="a", shape=[])  # Disallowed
複製代碼

可是,若是咱們真的想要複用先前聲明的變量呢?變量範圍還提供了執行此操做的功能:spa

with tf.variable_scope("scope"):
  a1 = tf.get_variable(name="a", shape=[])
with tf.variable_scope("scope", reuse=True):
  a2 = tf.get_variable(name="a", shape=[])  # OK
複製代碼

這在使用內置神經網絡層時變得很方便:翻譯

with tf.variable_scope('my_scope'):
  features1 = tf.layers.conv2d(image1, filters=32, kernel_size=3)
# Use the same convolution weights to process the second image:
with tf.variable_scope('my_scope', reuse=True):
  features2 = tf.layers.conv2d(image2, filters=32, kernel_size=3)
複製代碼

或者,您能夠將reuse屬性設置爲tf.AUTO_REUSE,這種操做告訴TensorFlow若是不存在具備相同名稱的變量,就建立新變量,不然就複用:code

with tf.variable_scope("scope", reuse=tf.AUTO_REUSE):
  features1 = tf.layers.conv2d(image1, filters=32, kernel_size=3)
  
with tf.variable_scope("scope", reuse=tf.AUTO_REUSE):
  features2 = tf.layers.conv2d(image2, filters=32, kernel_size=3)
複製代碼

若是你想共享不少變量,跟蹤定義新變量以及複用這些變量的時候可能很麻煩且容易出錯。tf.AUTO_REUSE則簡化了此任務,但增長了共享不該共享的變量的風險。TensorFlow模板是解決這一問題的另外一種方法,它沒有這種風險:

conv3x32 = tf.make_template("conv3x32", lambda x: tf.layers.conv2d(x, 32, 3))
features1 = conv3x32(image1)
features2 = conv3x32(image2)  # Will reuse the convolution weights.
複製代碼

您能夠將任何功能轉換爲TensorFlow模板。在第一次調用模板時,在函數內部定義的變量會被聲明,而且在連續調用中,它們將被自動複用。

相關文章
相關標籤/搜索