前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >tensorflow学习笔记(十五): variable scope

tensorflow学习笔记(十五): variable scope

作者头像
ke1th
发布2019-05-26 12:29:42
5920
发布2019-05-26 12:29:42
举报
文章被收录于专栏:漫漫深度学习路

variable scope

tensorflow 为了更好的管理变量,提供了variable scope机制 官方解释: Variable scope object to carry defaults to provide to get_variable.

Many of the arguments we need for get_variable in a variable store are most easily handled with a context. This object is used for the defaults.

Attributes:

  • name: name of the current scope, used as prefix in get_variable.
  • initializer: 传给get_variable的默认initializer.如果get_variable的时候指定了initializer,那么将覆盖这个默认的initializer.
  • regularizer: 传给get_variable的默认regulizer.
  • reuse: Boolean or None, setting the reuse in get_variable.
  • caching_device: string, callable, or None: the caching device passed to get_variable.
  • partitioner: callable or None: the partitioner passed to get_variable.
  • custom_getter: default custom getter passed to get_variable.
  • name_scope: The name passed to tf.name_scope.
  • dtype: default type passed to get_variable (defaults to DT_FLOAT).

regularizer参数的作用是给在本variable_scope下创建的weights加上正则项.这样我们就可以不同variable_scope下的参数加不同的正则项了.

可以看出,用variable scope管理get_varibale是很方便的

如何确定 get_variable 的 prefixed name

首先, variable scope是可以嵌套的:

代码语言:javascript
复制
with variable_scope.variable_scope("tet1"):
    var3 = tf.get_variable("var3",shape=[2],dtype=tf.float32)
    print var3.name
    with variable_scope.variable_scope("tet2"):
        var4 = tf.get_variable("var4",shape=[2],dtype=tf.float32)
        print var4.name
#输出为****************
#tet1/var3:0
#tet1/tet2/var4:0
#*********************

get_varibale.name 以创建变量的 scope 作为名字的prefix

代码语言:javascript
复制
def te2():
    with variable_scope.variable_scope("te2"):
        var2 = tf.get_variable("var2",shape=[2], dtype=tf.float32)
        print var2.name
        def te1():
            with variable_scope.variable_scope("te1"):
                var1 = tf.get_variable("var1", shape=[2], dtype=tf.float32)
            return var1
        return te1() #在scope te2 内调用的
res = te2()
print res.name
#输出*********************
#te2/var2:0
#te2/te1/var1:0
#************************

观察和上个程序的不同

代码语言:javascript
复制
def te2():
    with variable_scope.variable_scope("te2"):
        var2 = tf.get_variable("var2",shape=[2], dtype=tf.float32)
        print var2.name
        def te1():
            with variable_scope.variable_scope("te1"):
                var1 = tf.get_variable("var1", shape=[2], dtype=tf.float32)
            return var1
    return te1()  #在scope te2外面调用的
res = te2()
print res.name
#输出*********************
#te2/var2:0
#te1/var1:0
#************************

还有需要注意一点的是tf.variable_scope("name")tf.variable_scope(scope)的区别,看下面代码

代码1

代码语言:javascript
复制
import tensorflow as tf
with tf.variable_scope("scope"):
    tf.get_variable("w",shape=[1])#这个变量的name是 scope/w
    with tf.variable_scope("scope"):
        tf.get_variable("w", shape=[1]) #这个变量的name是 scope/scope/w
# 这两个变量的名字是不一样的,所以不会产生冲突

代码2

代码语言:javascript
复制
import tensorflow as tf
with tf.variable_scope("yin"):
    tf.get_variable("w",shape=[1])
    scope = tf.get_variable_scope()#这个变量的name是 scope/w
    with tf.variable_scope(scope):#这种方式设置的scope,是用的外部的scope
        tf.get_variable("w", shape=[1])#这个变量的name也是 scope/w
# 两个变量的名字一样,会报错

共享变量

共享变量的前提是,变量的名字是一样的,变量的名字是由变量名和其scope前缀一起构成, tf.get_variable_scope().reuse_variables() 是允许共享当前scope下的所有变量。reused variables可以看同一个节点

代码语言:javascript
复制
with tf.variable_scope("level1"):
    tf.get_variable("w",shape=[1])
    scope = tf.get_variable_scope()
    with tf.variable_scope("level2"):
        tf.get_variable("w", shape=[1])

with tf.variable_scope("level1", reuse=True): #即使嵌套的variable_scope也会被reuse
    tf.get_variable("w",shape=[1])
    scope = tf.get_variable_scope()
    with tf.variable_scope("level2"):
        tf.get_variable("w", shape=[1])

其它

tf.get_variable_scope() :获取当前scope tf.get_variable_scope().reuse_variables() 共享变量

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2016年11月03日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • variable scope
    • 如何确定 get_variable 的 prefixed name
      • 共享变量
        • 其它
        领券
        问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档