-
2018-08-17 22:41:21
TensorFlow中有两个作用域(scope),分别是name_scope和variable_scope。variable_scope主要是给variable_name(变量名)增加前缀,还可以给op_name(运算名称)增加前缀,而name_scope是,op_name(运算名称)增加前缀。接下来回详细介绍两者的区别和联系。
一、variable_scope的使用
tf.get_variable函数通过所给的名称创建或者获取一个变量
1、variable_scope的使用
with tf.variable_scope("foo") as scope: v = tf.get_variable("v",[1]) print(v.name) #foo/v:0
tf.variable_scope有一个参数reuse默认None,当reuse为None或False时,表示在该作用域下tf.get_variable函数只能创建变量,如果创建两个相同名字的变量就会报错:ValueError: Variable foo/v already exists
with tf.variable_scope("foo") as scope: v = tf.get_variable("v",[1]) v1 = tf.get_variable("v",[1]) print(v.name)
将reuse参数设置为True时,作用域可以共享变量,两个变量相等
with tf.variable_scope("foo",reuse=True): v1 = tf.get_variable("v",[1]) v2 = tf.get_variable("v",[1]) print(v1.name,v2.name,v1 == v2) #foo/v:0 foo/v:0 True
2、获取变量的作用域
with tf.variable_scope("foo") as foo_scope: v = tf.get_variable("v",[1]) with tf.variable_scope(foo_scope): w = tf.get_variable("w",[1]) print(v.name,w.name) #foo/v:0 foo/w:0
如果在开启一个变量的作用域之前预先定义一个作用域,则会跳过当前变量的作用域,使用预先定义的作用域
with tf.variable_scope("bar"): with tf.variable_scope("baz") as other_scope: print(other_scope.name) #bar/baz #使用预先定义的作用域 with tf.variable_scope(foo_scope) as foo_scope2: print(foo_scope2.name) #foo
3、变量作用域的初始化
在定义变量的作用域的时候,可以给该作用域下的变量默认定义一个初始化器,在这个作用域下的子作用域或变量都可以继承或者重写父作用域的初始化值。
sess = tf.Session() #为该作用域下的变量定义一个初始化器,默认变量值为0.4 with tf.variable_scope("foo",initializer=tf.constant_initializer(0.4)): #使用默认值 v = tf.get_variable("v",[1]) #重写作用域的初始化值 w = tf.get_variable("w",[1],initializer=tf.constant_initializer(0.5)) #子作用域,默认使用父作用域的初始化器 with tf.variable_scope("bar"): v1 = tf.get_variable("v1",[1]) #子作用域,重写父作用域的初始化值 with tf.variable_scope("baz",initializer=tf.constant_initializer(0.6)): v2 = tf.get_variable("v2",[1]) init = tf.global_variables_initializer() sess.run(init) print(sess.run(v),sess.run(w),sess.run(v1),sess.run(v2)) #[0.4] [0.5] [0.4] [0.6] sess.close()
4、在variable_scope下的op_name
with tf.variable_scope("foo"): x = 1.0 + tf.get_variable("v",[1]) print(x.op.name) #foo/add
在variable_scope下也会为op_name增加一个前缀
二、name_scope的使用
name_scope下的作用域会影响op_name,但不会影响get_variable创建的变量,会影响通过Variable()创建的变量
with tf.variable_scope("foo"): with tf.name_scope("bar"): v = tf.get_variable("v",[1]) w = tf.Variable(tf.zeros([1]),name="w") z = 1.0 + v print(v.name,w.name,z.op.name) #foo/v:0 foo/bar/w:0 foo/bar/add
更多相关内容 -
tf.name_scope
2019-07-02 20:58:37tf.name_scope 函数是为了解决命名冲突问题。 1. Class name_scope 1.1 Aliases Class tf.keras.backend.name_scope Class tf.name_scope aliases:n. 别名 (alias 的复数) scope [skəʊp]:n. 范围,余地,视野...tf.name_scope site/en/api_docs/api_docs/python/tf/name_scope.md
tf.name_scope 函数是为了解决命名冲突问题。
1. Class
name_scope
1.1 Aliases
- Class
tf.keras.backend.name_scope
- Class
tf.name_scope
aliases:n. 别名 (alias 的复数) scope [skəʊp]:n. 范围,余地,视野,眼界,导弹射程 vt. 审视
A context manager for use when defining a Python op.
定义 Python 操作时使用的上下文管理器。This context manager validates that the given
values
are from the same graph, makes that graph the default graph, and pushes a name scope in that graph (seetf.Graph.name_scope
for more details on that).
此上下文管理器验证给定值来自同一 graph,使该 graph 成为默认 graph,并在该 graph 中推送名称范围 (有关详细信息,请参阅tf.Graph.name_scope
)。1.2 Methods
1.2.1 init
__init__( name, default_name=None, values=None )
Initialize the context manager.
初始化上下文管理器。name
: The name argument that is passed to the op function. - 传递给 op 函数的 name 参数。default_name
: The default name to use if thename
argument isNone
. -如果name
参数为 'None`,则使用默认名称。values
: The list ofTensor
arguments that are passed to the op function. -传递给 op 函数的Tensor
参数列表。
1.2.2 enter
__enter__()
Start the scope block.
启动范围块。Returns
The scope name.Raises
ValueError
: if neithername
nordefault_name
is provided butvalues
are. 如果既没有提供name
也没有default_name
,但是values
存在。1.2.3 exit
__exit__( type_arg, value_arg, traceback_arg )
#!/usr/bin/env python # -*- coding: utf-8 -*- from __future__ import absolute_import from __future__ import division from __future__ import print_function import tensorflow as tf with tf.name_scope(None, "default_name", None): op = tf.constant(0) print(op.name) with tf.name_scope("yongqiang", "default_name", None): op = tf.constant(0) print(op.name)
strong@foreverstrong:~/git_workspace/MonoGRNet$ python yongqiang.py default_name/Const:0 yongqiang/Const:0 strong@foreverstrong:~/git_workspace/MonoGRNet$
values 参数的规定了应该把在 with tf.name_scope 区域里生成的 tensor 放到哪个计算图 (graph) 里。在 with tf.name_scope(**, **, values) 这个区域内声明的 tensor,都被放到了 values 所在的 graph 里。它为我们省去了显式地声明某些 tensor 需要被放在哪个图中的麻烦。values 里的所有 tensor 都应该在同一个图里,否则会报错。
在编写比较复杂的程序时,不同的 tensor 可能需要被放在不同的 graph 里。在需要对多个 tensor 进行并行计算时,我们把它们放进不同的 graph 里,然后分别丢给不同的 session 去 run。
下面代码对比了在 with tf.name_scope 使用 values 和不使用 values 的区别。不用 values 时,在 with tf.name_scope 中声明的 tensor 都被放进了当前的“默认图”中。使用 values 时,在 with tf.name_scope 中声明的 tensor 被放进了 tensor A 所在的图中,即 graph_tensor 这个图中。
#!/usr/bin/env python # -*- coding: utf-8 -*- from __future__ import absolute_import from __future__ import division from __future__ import print_function import tensorflow as tf graph_tensor = tf.Graph() with graph_tensor.as_default(): FS = tf.constant(1) graph_1 = tf.Graph() graph_1.as_default() with tf.name_scope(None, "namescope_1"): op1 = tf.constant(2) graph_2 = tf.Graph() graph_2.as_default() with tf.name_scope(None, "namescope_2", [FS]): op2 = tf.constant(3) print(op1.graph == graph_tensor) print(op2.graph == graph_tensor) print(op1.graph) print(op2.graph) print(graph_tensor)
strong@foreverstrong:~/git_workspace/MonoGRNet$ python yongqiang.py False True <tensorflow.python.framework.ops.Graph object at 0x7f3170247f50> <tensorflow.python.framework.ops.Graph object at 0x7f31e2d53690> <tensorflow.python.framework.ops.Graph object at 0x7f31e2d53690> strong@foreverstrong:~/git_workspace/MonoGRNet$
2. tf.name_scope
命名空间就是给变量包一层名字,方便变量管理。函数是
tf.name_scope
。就像操作系统文件夹命名一样,不同的顶层文件夹下,可以有同名文件夹。不同的命名空间下,可以有名字相同的变量。source code
#!/usr/bin/env python # -*- coding: utf-8 -*- from __future__ import absolute_import from __future__ import division from __future__ import print_function import tensorflow as tf with tf.name_scope("yong"): weights1 = tf.Variable([1.0, 2.0], name='weights') bias1 = tf.Variable([0.3], name='bias') with tf.name_scope("qiang"): weights2 = tf.Variable([4.0, 2.0], name='weights') bias2 = tf.Variable([0.5], name='bias') init = tf.global_variables_initializer() sess = tf.Session() writer = tf.summary.FileWriter("./strong_graph", sess.graph) sess.run(init) print(weights1.name) print(weights2.name)
console
strong@foreverstrong:~/git_workspace/MonoGRNet$ python yongqiang.py 2019-07-02 16:34:23.707195: I tensorflow/core/platform/cpu_feature_guard.cc:137] Your CPU supports instructions that this TensorFlow binary was not compiled to use: SSE4.1 SSE4.2 AVX AVX2 FMA 2019-07-02 16:34:23.793798: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:892] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero 2019-07-02 16:34:23.794076: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1030] Found device 0 with properties: name: GeForce GTX 1080 major: 6 minor: 1 memoryClockRate(GHz): 1.7335 pciBusID: 0000:01:00.0 totalMemory: 7.92GiB freeMemory: 7.51GiB 2019-07-02 16:34:23.794089: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1120] Creating TensorFlow device (/device:GPU:0) -> (device: 0, name: GeForce GTX 1080, pci bus id: 0000:01:00.0, compute capability: 6.1) yong/weights:0 qiang/weights:0 strong@foreverstrong:~/git_workspace/MonoGRNet$
sess.graph - events.out.tfevents.1562056463.foreverstrong
strong@foreverstrong:~/git_workspace/MonoGRNet/strong_graph$ ls -l total 8 -rw-rw-r-- 1 strong strong 5591 Jul 2 16:34 events.out.tfevents.1562056463.foreverstrong strong@foreverstrong:~/git_workspace/MonoGRNet/strong_graph$
tensorboard --logdir="./strong_graph/"
strong@foreverstrong:~/git_workspace/MonoGRNet$ tensorboard --logdir="./strong_graph/" TensorBoard 0.4.0 at http://foreverstrong:6006 (Press CTRL+C to quit)
浏览器中输入上述网址:http://foreverstrong:6006
auxiliary [ɔːɡˈzɪljərɪ]:adj. 辅助的,副的,附加的,(发动机、设备等) 备用的 n. 助动词,辅助者,辅助物,附属机构,(北美) 志愿队,(海军的)辅助舰队
双击 qiang 矩形框
双击 yong 矩形框
总体框架
Press CTRL + C to quit
strong@foreverstrong:~/git_workspace/MonoGRNet$ tensorboard --logdir="./strong_graph/" TensorBoard 0.4.0 at http://foreverstrong:6006 (Press CTRL+C to quit) ^Cstrong@foreverstrong:~/git_workspace/MonoGRNet$ strong@foreverstrong:~/git_workspace/MonoGRNet$
3. tf.name_scope
tf.name_scope() 当传入字符串时,用以给变量名添加前缀,类似于目录。
#!/usr/bin/env python # -*- coding: utf-8 -*- from __future__ import absolute_import from __future__ import division from __future__ import print_function import tensorflow as tf with tf.name_scope('k1'): with tf.name_scope('k2'): weights1 = tf.Variable([1, 2, 3], name='weights') bias1 = tf.Variable([0.1], name='biases') print(weights1.name) print(bias1.name)
strong@foreverstrong:~/git_workspace/MonoGRNet$ python yongqiang.py k1/k2/weights:0 k1/k2/biases:0 strong@foreverstrong:~/git_workspace/MonoGRNet$
当传入已存在的 name_scope 对象时,则其范围内变量的前缀只与当前传入的对象有关,与更上层的 name_scope 无关。
#!/usr/bin/env python # -*- coding: utf-8 -*- from __future__ import absolute_import from __future__ import division from __future__ import print_function import tensorflow as tf with tf.name_scope('k1') as k1_scope: with tf.name_scope('k2'): weights0 = tf.Variable([1, 2, 3], name='weights') bias0 = tf.Variable([0.1], name='biases') with tf.name_scope(k1_scope): weights1 = tf.Variable([1, 2, 3], name='weights') bias1 = tf.Variable([0.1], name='biases') print(weights0.name) print(bias0.name) print(weights1.name) print(bias1.name)
strong@foreverstrong:~/git_workspace/MonoGRNet$ python yongqiang.py k1/k2/weights:0 k1/k2/biases:0 k1/weights:0 k1/biases:0 strong@foreverstrong:~/git_workspace/MonoGRNet$
注意观察与上面两个例子的区别。
#!/usr/bin/env python # -*- coding: utf-8 -*- from __future__ import absolute_import from __future__ import division from __future__ import print_function import tensorflow as tf with tf.name_scope('k1'): with tf.name_scope('k2'): weights1 = tf.Variable([1, 2, 3], name='weights') bias1 = tf.Variable([0.1], name='biases') print(weights1.name) print(bias1.name) with tf.name_scope('k1') as k1_scope: with tf.name_scope('k2'): weights0 = tf.Variable([1, 2, 3], name='weights') bias0 = tf.Variable([0.1], name='biases') with tf.name_scope(k1_scope): weights1 = tf.Variable([1, 2, 3], name='weights') bias1 = tf.Variable([0.1], name='biases') print(weights0.name) print(bias0.name) print(weights1.name) print(bias1.name)
strong@foreverstrong:~/git_workspace/MonoGRNet$ python yongqiang.py k1/k2/weights:0 k1/k2/biases:0 k1_1/k2/weights:0 k1_1/k2/biases:0 k1_1/weights:0 k1_1/biases:0 strong@foreverstrong:~/git_workspace/MonoGRNet$
- Class
-
mxnet-gluon学习笔记之 - name_scope
2020-02-11 17:35:30源码地址:https://mxnet.incubator.apache.org/api/python/docs/_modules/mxnet/gluon/block.html#Block.name_scope 其他参考: 参数和Block命名:...源码地址:https://mxnet.incubator.apache.org/api/python/docs/_modules/mxnet/gluon/block.html#Block.name_scope
其他参考:
参数和Block命名:https://mxnet.incubator.apache.org/api/python/docs/tutorials/packages/gluon/blocks/naming.html写在前面
name_scope函数是在Block中定义的,而又因为HybridBlock是Block的子类,所以
Block和HybridBlock都有这个函数。而gluon.nn,或者gluon.contrib下定义的高层次的组件继承了HybridBlock,一言概之,gluon中的模型,都具有name_scope功能。调用 with xxx.name_scope时候,实际调用的是Block的name_scope函数,而name_scope返回的是self._scope,Block在构造的时候,会创建self._scope属性,而self._scope=_BlockScope(self),所以with语句最终调用的是_BlockScope。
_BlockScope类的定义
import threading class _BlockScope(object): """Scope for collecting child `Block` s.""" _current = threading.local() # 确保_current中的同名变量在多线程之间互不干扰 def __init__(self, block): self._block = block # block是gluon.Block或者gluon.HybridBlock self._counter = {} self._old_scope = None self._name_scope = None @staticmethod def create(prefix, params, hint): # Block调用时,hint是self._alias(),而self._alias()返回的是类名, # self.__class__.__name__.lower() """Creates prefix and params for new `Block`.""" current = getattr(_BlockScope._current, "value", None) if current is None: if prefix is None: if not hasattr(_name.NameManager._current, "value"): _name.NameManager._current.value = _name.NameManager() prefix = _name.NameManager._current.value.get(None, hint) + '_' if params is None: params = ParameterDict(prefix) else: params = ParameterDict(params.prefix, params) return prefix, params if prefix is None: count = current._counter.get(hint, 0) prefix = '%s%d_'%(hint, count) current._counter[hint] = count + 1 if params is None: parent = current._block.params params = ParameterDict(parent.prefix+prefix, parent._shared) else: params = ParameterDict(params.prefix, params) return current._block.prefix+prefix, params def __enter__(self): if self._block._empty_prefix: return self self._old_scope = getattr(_BlockScope._current, "value", None) _BlockScope._current.value = self self._name_scope = _name.Prefix(self._block.prefix) self._name_scope.__enter__() return self def __exit__(self, ptype, value, trace): if self._block._empty_prefix: return self._name_scope.__exit__(ptype, value, trace) self._name_scope = None _BlockScope._current.value = self._old_scope
Block相关函数
在这里插入代码片
-
命名空间name_scope与variable_scope的区别
2018-10-16 23:41:24前言:整理name_scope与variable_...tf.name_scope()和tf.variable_scope()是两个作用域,一般与两个创建/调用变量的函数tf.variable() 和tf.get_variable()搭配使用。它们搭配在一起的两个常见用途: 变量共享...前言:整理name_scope与variable_scope是因为自己在学习的过程中碰到的一些疑惑,后来看书和网上的一些解释。故这里做一些学习记录。
1、大体的认识
tf.name_scope()和tf.variable_scope()是两个作用域,一般与两个创建/调用变量的函数tf.variable() 和tf.get_variable()搭配使用。它们搭配在一起的两个常见用途:
- 变量共享
- tensorboard画流程图时为了可视化封装变量
这两种用途有特定的搭配方式,掌握好就可以用了。
- name_scope:为了更好地管理变量的命名空间而提出的。比如在 tensorboard 中,因为引入了 name_scope, 我们的 Graph 看起来才井然有序。
- variable_scope:大部分情况下,跟 tf.get_variable() 配合使用,实现变量共享的功能。
2、name_scope与variable_scope的区别
先直接给出结论,然后再进行说明,是其他博主整理的结论,这只是借鉴一下,原文是这个。
- name_scope 对 get_variable新建变量的name属性无影响;对variable新建变量的name属性增加了“范围”标识。
- variable_scope对get_variable新建变量的name属性和variable新建变量的name属性都增加了“范围”标识。
- get_variable新建变量如果遇见重复的name则会因为重复而报错。
- variable新建的变量如果遇见重复的name则会自动修改前缀,以避免重复出现。
2.1 tf.name_scope
tf.name_scope 主要结合 tf.Variable() 来使用,方便参数命名管理。
2.1.1 name_scope()对variable的影响
''' Signature: tf.name_scope(*args, **kwds) Docstring: Returns a context manager for use when defining a Python op. ''' # 也就是说,它的主要目的是为了更加方便地管理参数命名。 # 与 tf.Variable() 结合使用。简化了命名 with tf.name_scope('conv1') as scope: weights1 = tf.Variable([1.0, 2.0], name='weights') bias1 = tf.Variable([0.3], name='bias') # 下面是在另外一个命名空间来定义变量的 with tf.name_scope('conv2') as scope: weights2 = tf.Variable([4.0, 2.0], name='weights') bias2 = tf.Variable([0.33], name='bias') # 所以,实际上weights1 和 weights2 这两个引用名指向了不同的空间,不会冲突 print weights1.name print weights2.name conv1/weights:0 conv2/weights:0
从上面的例子可以看出name_scope()对variable的变量名自动添上命名空间的名称,形式为/
2.1.2 tf.name_scope对tf.get_variable()的影响
with tf.name_scope("a_name_scope") as myscope: initializer = tf.constant_initializer(value=1) var1 = tf.get_variable(name='var1', shape=[1], dtype=tf.float32, initializer=initializer) print(var1.name) var1:0
可见并没有在变量名前添加命名空间的名称。
2.2 tf.variable_scope
tf.variable_scope() 主要结合 tf.get_variable() 来使用,实现变量共享
2.2.1 variable_scope()对tf.get_variable() 的影响
with tf.variable_scope('v_scope') as scope1: Weights1 = tf.get_variable('Weights', shape=[2,3]) bias1 = tf.get_variable('bias', shape=[3]) # 下面来共享上面已经定义好的变量 # note: 在下面的 scope 中的变量必须已经定义过了,才能设置 reuse=True,否则会报错 with tf.variable_scope('v_scope', reuse=True) as scope2: Weights2 = tf.get_variable('Weights') print Weights1.name print Weights2.name v_scope/Weights:0 v_scope/Weights:0
对variable的影响这里就不展示了,这里只提醒一下,当需要共用变量的时候,设置的reuse=True必须是要已经创建过的变量,否则则会报错。
也许目前还不知道这会有什么用,但给个例子就会发现这样的好处。这里借鉴一下这篇博客的内容。(也可以参考下这篇文章,讲的还算清楚,对变量的共享,因为在卷积神经网络中,卷积层的参数就是共享的呢)
TensorFlow中的变量一般就是模型的参数。当模型复杂的时候共享变量会无比复杂。
官网给了一个case,当创建两层卷积的过滤器时,每输入一次图片就会创建一次过滤器对应的变量,但是我们希望所有图片都共享同一过滤器变量,一共有4个变量:conv1_weights,conv1_biases,conv2_weights, and conv2_biases。
通常的做法是将这些变量设置为全局变量。但是存在的问题是打破封装性,这些变量必须文档化被其他代码文件引用,一旦代码变化,调用方也可能需要变化。
还有一种保证封装性的方式是将模型封装成类。
不过TensorFlow提供了Variable Scope 这种独特的机制来共享变量。这个机制涉及两个主要函数:
def conv_relu(input, kernel_shape, bias_shape): # Create variable named "weights". weights = tf.get_variable("weights", kernel_shape,initializer=tf.random_normal_initializer()) # Create variable named "biases". biases = tf.get_variable("biases", bias_shape,initializer=tf.constant_initializer(0.0)) conv = tf.nn.conv2d(input, weights,strides=[1, 1, 1, 1], padding='SAME') return tf.nn.relu(conv + biases) 但是我们需要两个卷积层,这时可以通过tf.variable_scope()指定作用域进行区分, 如with tf.variable_scope("conv1")这行代码指定了第一个卷积层作用域为conv1, 在这个作用域下有两个变量weights和biases。 def my_image_filter(input_images): with tf.variable_scope("conv1"): # Variables created here will be named "conv1/weights", "conv1/biases". relu1 = conv_relu(input_images, [5, 5, 32, 32], [32]) with tf.variable_scope("conv2"): # Variables created here will be named "conv2/weights", "conv2/biases". return conv_relu(relu1, [5, 5, 32, 32], [32]) 最后在image_filters这个作用域重复使用第一张图片输入时创建的变量,调用函数reuse_variables(),代码如下: with tf.variable_scope("image_filters") as scope: result1 = my_image_filter(image1) scope.reuse_variables() result2 = my_image_filter(image2)
总之,当神经网络结构更加复杂、参数更多时,使用这种变量的管理方式将大大提高程序的可读性。
参考资料:
https://blog.csdn.net/jerr__y/article/details/60877873
https://blog.csdn.net/lucky7213/article/details/78967306
https://www.cnblogs.com/MY0213/p/9208503.html
-
tf.name_scope()详解
2019-03-14 17:05:37参考文献: ...函数是:tf.name_scope 另外,就像操作系统文件夹命名一样,不同的顶层文件夹下,可以有同名文件夹。这里,不同的命名空间下,可以有名字相同的变量。 import tensorflow as tf with tf.n... -
tf.name_scope()、tf.variable_scope()函数解析(最清晰的解释)
2019-03-26 20:22:03最近在看tf.slim实现的inception_v1的代码,发现一个函数tf.variable_scope()不太清楚,查询到网上多对比name和variable,特意记录一下。 论文翻译:GoogLeNet - Going Deeper with Convolutions全文翻译 代码实现 ... -
tf.name_scope与tf.variable_scope用法区别
2018-11-20 10:41:19本文由网络上一些回答和博文汇总而成。 要将这个问题解释清楚,...tf.get_variable()创建的变量名不受tf.name_scope的影响,即创建的变量的name没有name_scope定义的前缀。而且,在未指定共享变量时,如果重名会报... -
tf.name_scope()和tf.variable_scope()解析
2018-05-04 10:47:44摘要: tf.name_scope()和tf.variable_scope()是两个作用域,一般与两个创建/调用变量的函数tf.variable() 和tf.get_variable()搭配使用。它们搭配在一起的两个常见用途:1)变量共享,2)tensorboard画流程图时为了... -
tensorflow 中 variable_scope 与name_scope函数解析
2017-09-18 22:27:31知乎上面一个有意思的问答:tensorflow里面name_scope, variable_scope等如何理解?先引用知乎上答主的话: 主要是因为 变量共享 的需求。而这就不得不谈到tf. get_variable()了。因为如果使用Variable 的话每次... -
tensorflow: name_scope 和 variable_scope区别及理解
2018-04-23 08:39:22tensorflow中name_scope和variable scope的理解之所以会出现这两种类型的scope,主要是后者(variable scope)为了实现tensorflow中的变量共享机制:即为了使得在代码的任何部分可以使用某一个已经创建的变量,TF... -
tensorflow:上下文管理器 与 name_scope, variable_scope
2017-06-21 19:35:38old_name_scope=old_name_scope, dtype=dtype, use_resource=use_resource) as vs: yield vs tf.variable_scope() 都是用已这种方式终结的。然后我们来看一下, _pure_variable_scope() , 它里面做了... -
TensorFlow学习记录:variable_scope()和name_scope()函数的区别
2018-12-11 23:48:52首先,我们在相同的variable_scope内使用get_variable()函数创建一个name属性为a的变量a1 import tensorflow as tf with tf.variable_scope("one"): a1=tf.get_variable("a",[1... -
python编程之tf.name_scope()的用法
2020-03-23 17:23:42tf.name_scope() 主要是和tf.Variable()使用; tf.Variable_scope()主要和tf.get_variable()使用; 我们来看看对于tf.Variable(),如果命名重复了,会怎么样? 不会冲突,系统会自动改成_1的形式 对于t... -
tf.name_scope()详解【命名空间其实就是给几个变量包一层名字,方便变量管理】
2021-04-18 20:26:32函数是:tf.name_scope 另外,就像操作系统文件夹命名一样,不同的顶层文件夹下,可以有同名文件夹。这里,不同的命名空间下,可以有名字相同的变量。 tf.name_scope 主要结合 tf.Variable() 来使用,方便参数命名... -
(TensorFlow)——tf.variable_scope和tf.name_scope详解
2018-09-11 20:29:181、variable_scope和name_scope存在的价值: 和普通模型相比,深度学习模型的节点(参数)非常多,我们很难确定哪个变量属于哪层。为了解决此问题,所以引入了name_scope和variable_scope,两者分别承担着不同的... -
TensorFlow学习笔记(1) tf.name_scope(‘scope_name’)
2019-06-04 14:29:33TensorFlow中的name_scope函数的作用是创建一个参数名称空间,这个空间里包括许多参数,每个参数有不同的名字,这样可以更好的管理参数空间,防止变量命名时产生冲突。 # 这里的name_scope是创建了一个命名空间,... -
tf.variable_scope()和tf.name_scope()的正确用法
2020-10-18 11:33:13tf.variable_scope() tf官网对tf.variable_scope()的说明。 tf.variable_scope()和tf.get_variable()是配合使用的,主要用于变量共享。当reuse=False或None,重复创建变量会报错。当reuse=True而变量不存在,也会... -
命名空间:tf.variable_scope()函数和tf.name_scope()函数的使用及区别
2018-05-22 12:48:20先看代码:#命名空间函数tf.variable_scope()和tf.name_scope()函数区别于使用 import tensorflow as tf with tf.variable_scope("foo"): a = tf.get_variable("bar", [1]) print(a.name) #... -
充分理解 name_scope 和 variable_scope
2018-01-23 16:10:17之前写过一个例子了: TensorFlow入门(四) name / variable_scope 的使用 但是当时其实还对 name / variable_scope 不是非常理解。 * 起因:在运行 RNN LSTM 实例代码的时候出现 ValueError。 * 在 ... -
tf.name_scope()和tf.variable_scope()
2018-04-26 21:38:55tf.name_scope()和tf.variable_scope()是两个作用域,一般与两个创建/调用变量的函数tf.variable() 和tf.get_variable()搭配使用。tf.name_scope和 variable_scope也是个作为上下文管理器的角色,下文管理器:意思... -
tensorflow中的scope命名方法:name_scope, variavle_scope
2018-07-26 17:31:46转自:知乎回答:tensorflow里面name_scope, variable_scope等如何理解? 1 在训练深度网络时,为了减少需要训练参数的个数(比如具有simase结构的LSTM模型)、或是多机多卡并行化训练大数据大模型(比如数据... -
详解tf.variable_scope函数
2018-12-21 17:01:30tf.variable_scope函数 variable_scope类 用于定义创建变量(层)的操作的上下文管理器。 此上下文管理器验证(可选)values是否来自同一图形,确保图形是默认的图形,并推送名称范围和变量范围。 如果name_or_scope... -
tf.get_default_graph().get_name_scope()
2020-07-13 22:53:01tf.get_default_graph().get_name_scope() tf 1.15.0版本可能有这个 2.2.0没有这个函数