【问题标题】:What characters are allowed in tensorflow variable names?tensorflow 变量名中允许使用哪些字符?
【发布时间】:2018-03-12 14:37:30
【问题描述】:

为张量流变量选择名称时,可以:

>> tf.Variable(2, name='a')

<tf.Variable 'finegrained_1/decoder/unreadable/a:0' shape=() dtype=int32_ref>

但是,这不是:

>> tf.Variable(2, name='a:b')

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
~/mm/appy-finegrained/sign_classification/model_finegrained.py in <module>()
----> 1 tf.Variable(2, name='a:b')

~/anaconda3/lib/python3.6/site-packages/tensorflow/python/ops/variables.py in __init__(self, initial_value, trainable, collections, validate_shape, caching_device, name, variable_def, dtype, expected_shape, import_scope, constraint)
    211           dtype=dtype,
    212           expected_shape=expected_shape,
--> 213           constraint=constraint)
    214 
    215   def __repr__(self):

~/anaconda3/lib/python3.6/site-packages/tensorflow/python/ops/variables.py in _init_from_args(self, initial_value, trainable, collections, validate_shape, caching_device, name, dtype, expected_shape, constraint)
    287     with ops.control_dependencies(None):
    288       with ops.name_scope(name, "Variable", [] if init_from_fn else
--> 289                           [initial_value]) as name:
    290 
    291         if init_from_fn:

~/anaconda3/lib/python3.6/site-packages/tensorflow/python/framework/ops.py in __enter__(self)
   4930       self._g_manager.__enter__()
   4931       self._name_scope = g.name_scope(self._name)
-> 4932       return self._name_scope.__enter__()
   4933 
   4934   def __exit__(self, type_arg, value_arg, traceback_arg):

~/anaconda3/lib/python3.6/contextlib.py in __enter__(self)
     79     def __enter__(self):
     80         try:
---> 81             return next(self.gen)
     82         except StopIteration:
     83             raise RuntimeError("generator didn't yield") from None

~/anaconda3/lib/python3.6/site-packages/tensorflow/python/framework/ops.py in name_scope(self, name)
   3512         # (viz. '-', '\', '/', and '_').
   3513         if not _VALID_SCOPE_NAME_REGEX.match(name):
-> 3514           raise ValueError("'%s' is not a valid scope name" % name)
   3515       else:
   3516         # Scopes created in the root must match the more restrictive

ValueError: 'a:b' is not a valid scope name

允许在名称中添加/,但它可能会破坏一些范围界定:

>> tf.Variable(2, name='a/b')

<tf.Variable 'finegrained_1/decoder/unreadable/a/b:0' shape=() dtype=int32_ref>

对于变量名中允许使用的字符集是否有任何定义的规则?

并且(关于 a/b 名称)对于不应该使用的内容是否有任何额外的指导?

【问题讨论】:

  • 您始终可以放心使用并坚持使用a-Z0-9_
  • :N 后缀标识了您指定名称的节点中的特定 张量。这就是为什么名称中不能包含 : 的原因。对于所有需要指定张量名称的场合,如果没有指定后缀,则暗示 :0。因此,例如get_tensor_by_name('a')get_tensor_by_name('a:0') 都将返回节点a 输出的第一个张量。

标签: python variables tensorflow graph scope


【解决方案1】:

至于 ops 命名,见tf.Operation 文档:

注意:此构造函数验证 Operation 的名称(传递为 node_def.name)。有效的Operation 名称与以下常规匹配 表达式:

[A-Za-z0-9.][A-Za-z0-9_.\\-/]*

每个变量都被翻译成若干个ops,并将其名称放入作用域,其命名规则非常相似(见python/framework/ops.py):

_VALID_OP_NAME_REGEX = re.compile("^[A-Za-z0-9.][A-Za-z0-9_.\\-/]*$")
_VALID_SCOPE_NAME_REGEX = re.compile("^[A-Za-z0-9_.\\-/]*$")

当然,'/'':' 不允许在名称中,因为它们用于内部 TensorFlow 目的:

  • '/' 是嵌套范围的分隔符(正则表达式允许这样做,但这意味着在单个声明中使用 多个范围);
  • ':' 是张量名称和输出索引的分隔符(详见this question)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-12-30
    • 1970-01-01
    • 2021-10-18
    • 2010-10-29
    • 1970-01-01
    • 2011-10-30
    相关资源
    最近更新 更多