tensorflow: name_scope 和 variable_scope的差别

Variable sharing 简介

因为我目前对variable_scope 的理解,这个功能主要是是针对Variable sharing 来做的,所以先介绍一下variable sharing:
主要有两种方法实现
* 直接在各个ops,function之间传递variable reference.
* 把variable 封装在variable_scope/name_scope 中

variable_scop/name_scope简介

这两种scope最直接的影响是: 所有在scope下面创建的variable都会把这个scope的名字作为前缀。比如如下代码
with tf.name_scope("my_scope"):
    v1 = tf.get_variable("var1", [1], dtype=tf.float32)
    v2 = tf.Variable(1, name="var2", dtype=tf.float32)

with tf.variable_scope("my_scope"):
    v3 = tf.get_variable("var1", [1], dtype=tf.float32)
    v4 = tf.Variable(1, name="var2", dtype=tf.float32)
print("v1:",v1)
print("v2:",v2)
print("v3:",v3)
print("v4:",v4)

输出结果如下

v1: <tf.Variable 'var1:0' shape=(1,) dtype=float32_ref>
v2: <tf.Variable 'my_scope/var2:0' shape=() dtype=float32_ref>
v3: <tf.Variable 'my_scope/var1:0' shape=(1,) dtype=float32_ref>
v4: <tf.Variable 'my_scope_1/var2:0' shape=() dtype=float32_ref>

name_scope 与 variable_scope的差别

细心的同学可能已经主要到了,在上面的输出中,v1 没有my_scope的前缀。这个就是name_scope 和variable_scope的差别:
tf.get_variable 会忽略 name_scope. name_scope 会对operation 起作用。

所以这就允许我们使用name_scope给operation分类。同时使用variable_scope来区分variable.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值