TensorFlow中,variable_scope和name_scope的不同之处
来源:互联网 发布:mysql修复表命令 编辑:程序博客网 时间:2024/06/08 00:35
之前一直很困惑,tf.variable_scope和tf.name_scope都是管理上下文环境的,它们有什么不同?
查阅资料时,发现了一段有意思的测试代码
import tensorflow as tfdef scoping(fn, scope1, scope2, vals): with fn(scope1): a = tf.Variable(vals[0], name='a') # 此处注意 b是get_variable b = tf.get_variable('b', initializer=vals[1]) c = tf.constant(vals[2], name='c') with fn(scope2): d = tf.add(a * b, c, name='res') print('\n '.join([scope1, a.name, b.name, c.name, d.name]), '\n') return dd1 = scoping(tf.variable_scope, 'scope_vars', 'res', [1, 2, 3])d2 = scoping(tf.name_scope, 'scope_name', 'res', [1, 2, 3])# 如果加上这一行,就会报错,因为d3的变量b会和d2的变量b冲突# d3 = scoping(tf.name_scope, 'scope_name2', 'res', [1, 2, 3])# 但这一行就不会冲突,因为d3和d1的变量b各自有作用域# d3 = scoping(tf.variable_scope, 'scope_vars2', 'res', [1, 2, 3])with tf.Session() as sess: writer = tf.summary.FileWriter('logs', sess.graph) sess.run(tf.global_variables_initializer()) print(sess.run([d1, d2])) writer.close()
运行后,得到如下输出
scope_vars scope_vars/a:0 scope_vars/b:0 scope_vars/c:0 scope_vars/res/res:0 scope_name scope_name/a:0 b:0 scope_name/c:0 scope_name/res/res:0
TensorBoard显示
总而言之,tf.name_scope仅为非tf.get_variable创建的tensor添加前缀;而tf.variable_scope为所有tensor添加前缀
阅读全文
0 0
- TensorFlow中,variable_scope和name_scope的不同之处
- tensorflow 中 name_scope和variable_scope
- tensorflow 中 name_scope 及 variable_scope 的异同
- Tensorflow name_scope和variable_scope的区别
- tensorflow: name_scope 和 variable_scope的差别
- tensorflow 中 variable_scope 与name_scope函数解析
- TensorFlow:作用域name_scope和variable_scope
- Tensorflow-variable_scope,name_scope
- tensorflow name_scope variable_scope
- tensorflow variable_scope\name_scope
- tensorflow name_scope && variable_scope
- tf.variable_scope和tf.name_scope的用法
- tensorflow 中的 name_scope 与 variable_scope
- TensorFlow基础name_scope与variable_scope
- tf.variable_scope与tf.name_scope不同之处
- tensorflow:上下文管理器 与 name_scope, variable_scope
- Tensorflow中tf.get_variable和tf.variable_scope的使用
- tensorflow的共享变量,tf.Variable(),tf.get_variable(),tf.Variable_scope(),tf.name_scope()联系与区别:
- C++写时拷贝(测试用例String类)
- No adapter for handler [public boolean com.fuying.assure.user.login.action.UserManageAction.validIdN
- java.lang.IllegalStateException: Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 2 path $
- 关于descriptionWithLocale和NSLog数组/字典时中文显示unicode
- 总结几种排序算法(一)---冒泡排序
- TensorFlow中,variable_scope和name_scope的不同之处
- iOS UserDefaults存储自定义对象
- 联合体(union)的使用方法及其本质
- ZK框架的分析与应用
- mysql游标
- 算法系列——Copy List with Random Pointer
- 第八届福建省大学生程序设计竞赛 FZU 2277 Change (dfs序+树状数组)
- 父类指针强制转换为子类指针
- 同步与异步