tensorflow - tensorflow 中的名称范围和变量范围有什么区别?

标签 tensorflow

这些函数之间有什么区别?

tf.variable_op_scope(values, name, default_name, initializer=None)

Returns a context manager for defining an op that creates variables. This context manager validates that the given values are from the same graph, ensures that that graph is the default graph, and pushes a name scope and a variable scope.

<小时/>

tf.op_scope(values, name, default_name=None)

Returns a context manager for use when defining a Python op. This context manager validates that the given values are from the same graph, ensures that that graph is the default graph, and pushes a name scope.

<小时/>

tf.name_scope(name)

Wrapper for Graph.name_scope() using the default graph. See Graph.name_scope() for more details.

<小时/>

tf.variable_scope(name_or_scope, reuse=None, initializer=None)

Returns a context for variable scope. Variable scope allows to create new variables and to share already created ones while providing checks to not create or share by accident. For details, see the Variable Scope How To, here we present only a few basic examples.

最佳答案

让我们首先简单介绍一下变量共享。它是 TensorFlow 中的一种机制,允许共享在代码的不同部分访问的变量,而无需传递对变量的引用。

方法tf.get_variable可以与变量名称作为参数一起使用,以创建具有该名称的新变量或检索之前创建的变量。这与使用 tf.Variable 不同。构造函数,每次调用时都会创建一个新变量(如果已经存在具有此类名称的变量,则可能会在变量名称中添加后缀)。

正是为了变量共享机制,引入了单独类型的作用域(变量作用域)。

因此,我们最终有两种不同类型的范围:

两个作用域对所有操作以及使用 tf.Variable 创建的变量具有相同的效果,即作用域将作为前缀添加到操作或变量名称中。

但是,tf.get_variable 会忽略名称范围。我们可以在下面的示例中看到这一点:

with tf.name_scope("my_scope"):
    v1 = tf.get_variable("var1", [1], dtype=tf.float32)
    v2 = tf.Variable(1, name="var2", dtype=tf.float32)
    a = tf.add(v1, v2)

print(v1.name)  # var1:0
print(v2.name)  # my_scope/var2:0
print(a.name)   # my_scope/Add:0

将使用 tf.get_variable 访问的变量放置在作用域中的唯一方法是使用变量作用域,如下例所示:

with tf.variable_scope("my_scope"):
    v1 = tf.get_variable("var1", [1], dtype=tf.float32)
    v2 = tf.Variable(1, name="var2", dtype=tf.float32)
    a = tf.add(v1, v2)

print(v1.name)  # my_scope/var1:0
print(v2.name)  # my_scope/var2:0
print(a.name)   # my_scope/Add:0

这使我们能够轻松地在程序的不同部分之间共享变量,甚至在不同的名称范围内:

with tf.name_scope("foo"):
    with tf.variable_scope("var_scope"):
        v = tf.get_variable("var", [1])
with tf.name_scope("bar"):
    with tf.variable_scope("var_scope", reuse=True):
        v1 = tf.get_variable("var", [1])
assert v1 == v
print(v.name)   # var_scope/var:0
print(v1.name)  # var_scope/var:0
<小时/>

更新

从版本 r0.11 开始,op_scopevariable_op_scope 均为 deprecated并替换为 name_scopevariable_scope

关于tensorflow - tensorflow 中的名称范围和变量范围有什么区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35919020/

相关文章:

ubuntu - 在 Ubuntu 18.04 上构建具有 OpenCL 支持的 TensorFlow 失败

python - 在Conda环境中安装包,但只能在Python中运行而不是在iPython中运行?

tensorflow - 使用 Google ML 引擎和 Google Storage 存储大量图像进行训练的最佳实践

python - 使用不规则张量和 while 循环时,XLA 无法推导出跨步切片的编译时间常数输出形状

Tensorflow 似乎没有看到我的 GPU

tensorflow - tf 如何从同一个变量中恢复两个变量

python - 类型错误 : '...' has type str, 但需要以下之一:字节

django - Django 中的机器学习(tensorflow/sklearn)?

tensorflow - 没有用于 int32 变量操作的 GPU 内核

python - 在 GAN 中使用经过训练的判别器来计算概率