TensorFlow-tf.variable_scope与tf.name_scope函数的区别

这两个函数都提供了命名空间管理的功能,它们在大部分情况下是等价的,唯一的区别是在使用tf.get_variable函数时。以下代码简单地说明了这两个函数的区别:


import tensorflow as tf

with tf.variable_scope("foo"):
    # 在命名空间foo下获取变量"bar", 于是得到的变量名称为“foo/bar”
    a = tf.get_variable("bar", [1])
    print(a.name)            # 输出: foo/bar:0


with tf.variable_scope("bar"):
    # 在命名空间bar下获取变量“bar”,于是得到变量名称为“bar/bar”。
    # 此时,变量“bar/bar”和变量“foo/bar”并不冲突,于是可以正常运行
    b = tf.get_variable("bar", [1])
    print(b.name)            # 输出: bar/bar:0

with tf.name_scope("a"):
    # 使用tf.Variable函数生成变量会受tf.name_scope影响,于是这个变量的名称为“a/Variable"
    a = tf.Variable([1])
    print(a.name)       # 输出: a/Variable:0

    # tf.get_variable函数不受tf.name_scope函数影响,于是变量并不在a这个命名空间中。
    a = tf.get_variable("b", [1])
    print(a.name)         # 输出: b:0

with tf.name_scope("b"):
    # 因为tf.get_variable不受tf.name_scope影响,所以这时将试图获取名称为“b”的变量。
    # 然而这个变量已经被声明了,于是这里会报重复声明的错误:
    tf.get_variable("b", [1])

输出:

C:\ProgramData\Anaconda3\python.exe "C:\Program Files\JetBrains\PyCharm Community Edition 2017.2.3\helpers\pydev\pydevd.py" --multiproc --qt-support=auto --client 127.0.0.1 --port 56737 --file D:/doc.pycharm/TFbook/p231_variable_scope_and_name_scope.py
pydev debugger: process 295916 is connecting

Connected to pydev debugger (build 172.3968.37)
foo/bar:0
bar/bar:0
a/Variable:0
b:0
Traceback (most recent call last):
  File "C:\Program Files\JetBrains\PyCharm Community Edition 2017.2.3\helpers\pydev\pydevd.py", line 1599, in <module>
    globals = debugger.run(setup['file'], None, None, is_module)
  File "C:\Program Files\JetBrains\PyCharm Community Edition 2017.2.3\helpers\pydev\pydevd.py", line 1026, in run
    pydev_imports.execfile(file, globals, locals)  # execute the script
  File "C:\Program Files\JetBrains\PyCharm Community Edition 2017.2.3\helpers\pydev\_pydev_imps\_pydev_execfile.py", line 18, in execfile
    exec(compile(contents+"\n", file, 'exec'), glob, loc)
  File "D:/doc.pycharm/TFbook/p231_variable_scope_and_name_scope.py", line 34, in <module>
    tf.get_variable("b", [1])
  File "C:\ProgramData\Anaconda3\lib\site-packages\tensorflow\python\ops\variable_scope.py", line 1065, in get_variable
    use_resource=use_resource, custom_getter=custom_getter)
  File "C:\ProgramData\Anaconda3\lib\site-packages\tensorflow\python\ops\variable_scope.py", line 962, in get_variable
    use_resource=use_resource, custom_getter=custom_getter)
  File "C:\ProgramData\Anaconda3\lib\site-packages\tensorflow\python\ops\variable_scope.py", line 367, in get_variable
    validate_shape=validate_shape, use_resource=use_resource)
  File "C:\ProgramData\Anaconda3\lib\site-packages\tensorflow\python\ops\variable_scope.py", line 352, in _true_getter
    use_resource=use_resource)
  File "C:\ProgramData\Anaconda3\lib\site-packages\tensorflow\python\ops\variable_scope.py", line 664, in _get_single_variable
    name, "".join(traceback.format_list(tb))))
ValueError: Variable b already exists, disallowed. Did you mean to set reuse=True in VarScope? Originally defined at:

  File "C:\ProgramData\Anaconda3\lib\site-packages\tensorflow\python\framework\ops.py", line 1269, in __init__
    self._traceback = _extract_stack()
  File "C:\ProgramData\Anaconda3\lib\site-packages\tensorflow\python\framework\ops.py", line 2506, in create_op
    original_op=self._default_original_op, op_def=op_def)
  File "C:\ProgramData\Anaconda3\lib\site-packages\tensorflow\python\framework\op_def_library.py", line 767, in apply_op
    op_def=op_def)


Process finished with exit code 1

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值