variable scope变量范围

简介:

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是可以嵌套的:

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
#*********************
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

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

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
#************************
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15

观察和上个程序的不同

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
#************************
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15

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

代码1

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
# 这两个变量的名字是不一样的,所以不会产生冲突
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

代码2

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
# 两个变量的名字一样,会报错
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

共享变量

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

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])
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

其它

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

目录
相关文章
|
监控 安全 Linux
Linux日志(实用版)
Linux日志(实用版)
548 0
|
传感器 算法 物联网
《移动互联网技术》第三章 无线定位技术:掌握位置服务和室内定位的基本概念和工作原理
《移动互联网技术》第三章 无线定位技术:掌握位置服务和室内定位的基本概念和工作原理
945 0
|
人工智能 Linux Python
代码自动补全工具——Kite安装教程(以Pycharm为例)及Failed to install PyCharm at path.Plugin configuration directory的解决方案
代码自动补全工具——Kite安装教程(以Pycharm为例)及Failed to install PyCharm at path.Plugin configuration directory的解决方案
代码自动补全工具——Kite安装教程(以Pycharm为例)及Failed to install PyCharm at path.Plugin configuration directory的解决方案
|
编解码 Java 测试技术
『App自动化测试之Appium应用篇』| uiautomator + accessibility_id定位方法完全使用攻略
『App自动化测试之Appium应用篇』| uiautomator + accessibility_id定位方法完全使用攻略
1058 0
|
9月前
|
人工智能 自然语言处理 搜索推荐
2026最新:阿里云万小智AI建站系统测评及费用价格,15元AI建站免费送CN域名
2026年阿里云万小智AI建站系统测评:零代码建站,10分钟上线;新客享15元首月、年付180元,赠.CN域名+SSL证书;集成AI建站、配图、客服、内容创作四大能力,覆盖多行业模板,访问速度0.8秒,稳定性99.9%。
941 1
|
8月前
|
人工智能 自然语言处理 算法
大模型应用:大模型的token频率偏见:高频词与低频词的嵌入表示差异分析.46
本文深入解析大模型中的“Token频率偏见”:高频词嵌入精准稳定,低频词因拆分多、训练少而语义模糊。通过分词优化、LoRA微调、提示工程等方案,可有效提升医疗等专业场景的语义理解能力。
608 2
|
消息中间件 监控 数据可视化
实时看板数据分析的力量:推动高效决策的17个关键维度
实时看板数据分析通过可视化工具将企业运营数据实时整合展示,助力快速决策。它结合数据收集、处理与交互分析,广泛应用于制造、零售、金融等领域,提升响应速度与协作效率。技术上依赖流数据处理与可视化工具,如Power BI、Tableau等,同时关注数据安全与用户体验优化。
827 1
|
11月前
|
人工智能 Cloud Native 关系型数据库
云原生视角:AI数字人vs数字员工,玄晶引擎等工具的架构选型与落地实践
本文对比AI数字人与云原生AI数字员工的技术差异,揭示“交互组件”到“智能体”的进化路径。基于玄晶引擎与阿里云生态的实践案例,从功能闭环、架构设计、成本收益三维度解析,并提供开发者选型工具包,助力企业实现直播引流、智能下单、库存同步等全链路自动化,降低算力成本60%,提升业务转化率。
600 6
|
数据可视化 数据挖掘 关系型数据库
数据分析工具有哪些?
【5月更文挑战第19天】数据分析工具有哪些?
1788 7
|
网络协议 应用服务中间件 Linux
2023年实施工程师面试题(夺命10连问)HR不会技术的看
2023年实施工程师面试题(夺命10连问)HR不会技术的看
1051 2