【译】Effective TensorFlow Chapter3——范围以及何时使用它们

栏目: 数据库 · 发布时间: 5年前

内容简介:本文翻译自:TensorFlow中的变量和张量具有name属性,用于在符号图中标识它们。如果在创建变量或张量时未指定名称,TensorFlow会自动为您指定名称:您可以通过显式指定来覆盖默认名称:

本文翻译自: 《Scopes and when to use them》 , 如有侵权请联系删除,仅限于学术交流,请勿商用。如有谬误,请联系指出。

TensorFlow中的变量和张量具有name属性,用于在符号图中标识它们。如果在创建变量或张量时未指定名称,TensorFlow会自动为您指定名称:

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

b = tf.Variable(1)
print(b.name)  # prints "Variable:0"
复制代码

您可以通过显式指定来覆盖默认名称:

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_scope

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 创建的变量的名称:

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
复制代码

但是,如果我们真的想要复用先前声明的变量呢?变量范围还提供了执行此操作的功能:

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如果不存在具有相同名称的变量,就创建新变量,否则就复用:

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模板。在第一次调用模板时,在函数内部定义的变量会被声明,并且在连续调用中,它们将被自动复用。


以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

机器学习基础教程

机器学习基础教程

(英)Simon Rogers,、Mark Girolami / 郭茂祖、王春宇 刘扬 刘晓燕、刘扬、刘晓燕 / 机械工业出版社 / 2014-1 / 45.00

本书是一本机器学习入门教程,包含了数学和统计学的核心技术,用于帮助理解一些常用的机器学习算法。书中展示的算法涵盖了机器学习的各个重要领域:分类、聚类和投影。本书对一小部分算法进行了详细描述和推导,而不是简单地将大量算法罗列出来。 本书通过大量的MATLAB/Octave脚本将算法和概念由抽象的等式转化为解决实际问题的工具,利用它们读者可以重新绘制书中的插图,并研究如何改变模型说明和参数取值。......一起来看看 《机器学习基础教程》 这本书的介绍吧!

Base64 编码/解码
Base64 编码/解码

Base64 编码/解码

XML 在线格式化
XML 在线格式化

在线 XML 格式化压缩工具

HEX HSV 转换工具
HEX HSV 转换工具

HEX HSV 互换工具