Tensorflow中的默认variable_scope是什么? [英] What is the default variable_scope in Tensorflow?

查看:419
本文介绍了Tensorflow中的默认variable_scope是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Tensorflow中默认的全局variable_scope是什么?如何检查物体?有人对此有想法吗?

解决方案

从技术上讲,所有变量都没有全局变量作用域.如果您运行

x = tf.Variable(0.0, name='x')

从脚本的顶层开始,将在默认图形中创建一个没有变量作用域的新变量x.

但是, tf.get_variable() 函数的情况有所不同:

x = tf.get_variable(name='x')

它要做的第一件事是调用 tf.get_variable_scope() 函数,该函数返回当前变量作用域,该作用域又从本地堆栈中查找作用域:

def get_variable_scope():
  """Returns the current variable scope."""
  scope = ops.get_collection(_VARSCOPE_KEY)
  if scope:  # This collection has at most 1 element, the default scope at [0].
    return scope[0]
  scope = VariableScope(False)
  ops.add_to_collection(_VARSCOPE_KEY, scope)
  return scope

请注意,此堆栈可以为空,在这种情况下,只需创建一个新作用域并将其推入堆栈顶部即可.

如果 this 是您需要的对象,则可以通过以下方式访问它:

scope = tf.get_variable_scope()

从顶层开始,或者直接进入ops.get_collection(_VARSCOPE_KEY)(如果您已经在范围内).这正是通过调用tf.get_variable()函数获得新变量的范围.这是类 tf.VariableScope 的普通实例,您可以轻松检查. /p>

What is the default global variable_scope in Tensorflow? How can I inspect the object? Does anyone have ideas about that?

解决方案

Technically, there's no global variable scope for all variables. If you run

x = tf.Variable(0.0, name='x')

from the top level of your script, a new variable x without a variable scope will be created in the default graph.

However, the situation is a bit different for tf.get_variable() function:

x = tf.get_variable(name='x')

The first thing it does is calls tf.get_variable_scope() function, which returns the current variable scope, which in turn looks up the scope from the local stack:

def get_variable_scope():
  """Returns the current variable scope."""
  scope = ops.get_collection(_VARSCOPE_KEY)
  if scope:  # This collection has at most 1 element, the default scope at [0].
    return scope[0]
  scope = VariableScope(False)
  ops.add_to_collection(_VARSCOPE_KEY, scope)
  return scope

Note that this stack can be empty and in this case, a new scope is simply created and pushed on top of the stack.

If this is the object you need, you can access it just by calling:

scope = tf.get_variable_scope()

from the top level, or by going to ops.get_collection(_VARSCOPE_KEY) directly if you're inside a scope already. This is exactly the scope that a new variable will get by a call to tf.get_variable() function. It's an ordinary instance of class tf.VariableScope that you can easily inspect.

这篇关于Tensorflow中的默认variable_scope是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆