tensorflow_scope作用
来源:互联网 发布:淘宝数据分析哪些内容 编辑:程序博客网 时间:2024/05/29 19:53
tensorflow中有两个关于variable的op,tf.Variable()
与tf.get_variable()
下面介绍这两个的区别
区别
- 使用
tf.Variable
时,如果检测到命名冲突,系统会自己处理。使用tf.get_variable()
时,系统不会处理冲突,而会报错
import tensorflow as tfw_1 = tf.Variable(3,name="w_1")w_2 = tf.Variable(1,name="w_1")print w_1.nameprint w_2.name#输出#w_1:0#w_1_1:
import tensorflow as tfw_1 = tf.get_variable(name="w_1",initializer=1)w_2 = tf.get_variable(name="w_1",initializer=2)#错误信息#ValueError: Variable w_1 already exists, disallowed. Did#you mean to set reuse=True in VarScope?
- 基于这两个函数的特性,当我们需要共享变量的时候,需要使用
tf.get_variable()
。在其他情况下,这两个的用法是一样的
get_variable()与Variable的实质区别
来看下面一段代码:
import tensorflow as tfwith tf.variable_scope("scope1"): w1 = tf.get_variable("w1", shape=[]) w2 = tf.Variable(0.0, name="w2")with tf.variable_scope("scope1", reuse=True): w1_p = tf.get_variable("w1", shape=[]) w2_p = tf.Variable(1.0, name="w2")print(w1 is w1_p, w2 is w2_p)#输出#True False
看到这,就可以明白官网上说的参数复用的真面目了。由于tf.Variable()
每次都在创建新对象,所有reuse=True
和它并没有什么关系。对于get_variable()
,来说,如果已经创建的变量对象,就把那个对象返回,如果没有创建变量对象的话,就创建一个新的。
实例:
import tensorflow as tf
#print(dir(tf))
#print(help(tf.Variable))
#with tf.name_scope("a_name_scope") as scope:
with tf.variable_scope("a_variable_scope") as scope:
var1 = tf.get_variable(name = 'var1', initializer=[[1.,2.],[3.,4.]], dtype = tf.float32)
#scope.reuse_variables()
#print(dir(scope))
with tf.variable_scope("a_variable_scope", reuse = True) as scope:
var2 = tf.get_variable(name='var1', initializer=[2])
with tf.variable_scope("a_variable_scope", reuse = None) as scope:
var6 = tf.get_variable(name='var2', initializer=[2])
var3 = tf.Variable(initial_value = [3], name='var3')
var4 = tf.Variable(initial_value = [4], name='var3')
var5 = tf.Variable(initial_value = [5], name='var3')
init = tf.global_variables_initializer()
with tf.Session() as sess:
sess.run(init)
print(var1.name)
print(sess.run(var1))
print(var2.name)
print(sess.run(var2))
print(var3.name)
print(sess.run(var3))
print(var4.name)
print(sess.run(var4))
print(var5.name)
print(sess.run(var5))
输出:
a_variable_scope/var1:0
[[ 1. 2.]
[ 3. 4.]]
a_variable_scope/var1:0
[[ 1. 2.]
[ 3. 4.]]
a_variable_scope_2/var3:0
[3]
a_variable_scope_2/var3_1:0
[4]
a_variable_scope_2/var3_2:0
[5]
- tensorflow_scope作用
- 作用
- 作用
- 作用
- __define_initcall 作用(subsys_initcall 作用)
- static作用const作用
- 作用域::的作用
- 作用域的作用
- IsPostBack作用
- encodeRedirectURL 作用?
- static 作用
- 数据结构作用
- xml作用
- ServletContextListener作用
- 作用域
- spring作用
- htmllabel作用
- serialVersionUID作用
- 自我整理:iOS端APP真机调试和打ipa包完整流程
- 史上最全最好用的自定义交易支付密码及弹框
- Linux for Ubuntu Clion配置OenGL(C/C++)开发环境
- PS中的小技巧——不定期更新
- bzoj4569: [Scoi2016]萌萌哒
- tensorflow_scope作用
- 各种全局主键生成策略对比
- 路由器认识与设置方法
- [python] 函数小结
- Android EditText 默认不弹出输入法
- “玲珑杯”线上赛 Round #15 河南专场 H -- 喵哈哈村的战斗魔法师丶坏坏い月 分块/线段树
- dwr消息推送---向指定用户推送
- C#+ XML 增删改查
- 数据结构-顺序表的基本操作