先看代码:

  1.  
    #命名空间函数tf.variable_scope()和tf.name_scope()函数区别于使用
  2.  
     
  3.  
    import tensorflow as tf
  4.  
     
  5.  
    with tf.variable_scope("foo"):
  6.  
    a = tf.get_variable("bar", [1])
  7.  
    print(a.name) #foo/bar:0
  8.  
    b = tf.Variable("b", [1])
  9.  
    print(b.name) #foo/Variable:0
  10.  
     
  11.  
    with tf.variable_scope("bar"):
  12.  
    a = tf.get_variable("bar", [1])
  13.  
    print(a.name) #bar/bar:0
  14.  
    b = tf.Variable("b", [1])
  15.  
    print(b.name) #bar/Variable:0
  16.  
     
  17.  
    with tf.name_scope("a"):
  18.  
    a = tf.Variable([1])
  19.  
    print(a.name) #a/Variable:0
  20.  
     
  21.  
    with tf.name_scope("b"):
  22.  
    b = tf.get_variable("b", [1])
  23.  
    print(b.name) #b_1:0
  24.  
     
  25.  
    with tf.name_scope("b"):
  26.  
    c = tf.get_variable("b", [1])
  27.  
    print(c.name) #出错,b已存在

可以看出,对于tf.Variable()函数,两者的使用情况都一样;而tf.get_variable()函数,它不受name_scope约束,已经声明过的变量就不能再声明了。

1. tf.name_scope('scope_name')或tf.name_scope(named_scope)

主要与tf.Variable搭配使用;

当传入字符串时,用以给变量名添加前缀,类似于目录,如case1所示;

当传入已存在的name_scope对象时,则其范围内变量的前缀只与当前传入的对象有关,与更上层的name_scope无关,如case2所示。

  1. import tensorflow as tf
  2. # case 1:
  3. with tf.name_scope('l1'):
  4. with tf.name_scope('l2'):
  5. wgt1 = tf.Variable([1,2,3], name='wgts')
  6. bias1 = tf.Variable([0.1], name='biases')
  7. print wgt1.name, bias1.name
  8. # >>> l1/l2/wgts:0 l1/l2/biases:0
  9. # case 2:
  10. with tf.name_scope('l1') as l1_scp:
  11. with tf.name_scope('l2'):
  12. wgt0 = tf.Variable([1,2,3], name='wgts')
  13. bias0 = tf.Variable([0.1], name='biases')
  14. with tf.name_scope(l1_scp):
  15. wgt1 = tf.Variable([1,2,3], name='wgts')
  16. bias1 = tf.Variable([0.1], name='biases')
  17. print wgt0.name, bias0.name, wgt1.name, bias1.name
  18. # >>> l1_1/l2/wgts:0 l1_1/l2/biases:0 l1_1/wgts:0 l1_1/biases:0

2. tf.variable_scope('scope_name', reuse=None)或

tf.variable_scope(named_scope)

与name_scope一样:当传入字符串时,用以给变量名添加前缀,类似于目录;

当传入已存在的variable_scope对象时,则其范围内变量的前缀只与当前传入的对象有关,与更上层的variable_scope无关。

常于get_variable搭配使用,多用于变量共享;其中 reuse 参数可设为 None、tf.AUTO_REUSE、True、False;

当 reuse=None(默认情况)时,与上层variable_scope的reuse参数一样。

  1. # case 1
  2. with tf.variable_scope('lv1'):
  3. with tf.variable_scope('lv2'):
  4. init = tf.constant_initializer(0.1)
  5. wgt1 = tf.get_variable('wgts', [2,2])
  6. bias1 = tf.get_variable('biases', [2,2])
  7. print wgt1.name, bias1.name
  8. # >>> lv1/lv2/wgts:0 lv1/lv2/biases:0

当 reuse=tf.AUTO_REUSE 时,自动复用,如果变量存在则复用,不存在则创建。这是最安全的用法。

  1. with tf.variable_scope('lv1'):
  2. with tf.variable_scope('lv2'):
  3. init = tf.constant_initializer(0.1)
  4. wgt1 = tf.get_variable('wgts', [2,2])
  5. bias1 = tf.get_variable('biases', [2,2])
  6. print wgt1.name, bias1.name
  7. # >>> lv1/lv2/wgts:0 lv1/lv2/biases:0
  8. with tf.variable_scope('lv1', reuse=tf.AUTO_REUSE):
  9. with tf.variable_scope('lv2'):
  10. init = tf.constant_initializer(0.1)
  11. wgt2 = tf.get_variable('wgts', [2,2])
  12. bias2 = tf.get_variable('biases', [2,2])
  13. print wgt2.name, bias2.name
  14. # >>> lv1/lv2/wgts:0 lv1/lv2/biases:0
  1. with tf.variable_scope('lv1', reuse=tf.AUTO_REUSE):
  2. with tf.variable_scope('lv2'):
  3. init = tf.constant_initializer(0.1)
  4. wgt2 = tf.get_variable('wgts', [2,2])
  5. bias2 = tf.get_variable('biases', [2,2])
  6. print wgt2.name, bias2.name
  7. # >>> lv1/lv2/wgts:0 lv1/lv2/biases:0

当 reuse=True 时,tf.get_variable会查找该命名变量,如果没有找到,则会报错;所以设置reuse=True之前,要保证该命名变量已存在。

  1. with tf.variable_scope('lv1', reuse=True):
  2. with tf.variable_scope('lv2'):
  3. init = tf.constant_initializer(0.1)
  4. wgt1 = tf.get_variable('wgts', [2,2])
  5. bias1 = tf.get_variable('biases', [2,2])
  6. print wgt1.name, bias1.name
  7. # >>> ValueError: Variable lv1/lv2/wgts does not exist,
  8. # or was not created with tf.get_variable(). Did you mean
  9. # to set reuse=tf.AUTO_REUSE in VarScope?

命名变量已存在:

  1. with tf.variable_scope('lv1'):
  2. with tf.variable_scope('lv2'):
  3. init = tf.constant_initializer(0.1)
  4. wgt1 = tf.get_variable('wgts', [2,2])
  5. bias1 = tf.get_variable('biases', [2,2])
  6. print wgt1.name, bias1.name
  7. # >>> lv1/lv2/wgts:0 lv1/lv2/biases:0
  8. # case 2
  9. with tf.variable_scope('lv1', reuse=True):
  10. with tf.variable_scope('lv2'):
  11. init = tf.constant_initializer(0.1)
  12. wgt1 = tf.get_variable('wgts', [2,2])
  13. bias1 = tf.get_variable('biases', [2,2])
  14. print wgt1.name, bias1.name
  15. # >>> lv1/lv2/wgts:0 lv1/lv2/biases:0

当 reuse=False 时,tf.get_variable会调用tf.Variable来创建变量,并检查创建的变量是否以存在,如果已存在,则报错;

  1. with tf.variable_scope('lv1'):
  2. with tf.variable_scope('lv2'):
  3. init = tf.constant_initializer(0.1)
  4. wgt1 = tf.get_variable('wgts', [2,2])
  5. bias1 = tf.get_variable('biases', [2,2])
  6. print wgt1.name, bias1.name
  7. # >>> lv1/lv2/wgts:0 lv1/lv2/biases:0
  8. # case 2
  9. with tf.variable_scope('lv1', reuse=False):
  10. with tf.variable_scope('lv2'):
  11. init = tf.constant_initializer(0.1)
  12. wgt1 = tf.get_variable('wgts', [2,2])
  13. bias1 = tf.get_variable('biases', [2,2])
  14. print wgt1.name, bias1.name
  15. # ValueError: Variable lv1/lv2/wgts already exists, disallowed.
  16. # Did you mean to set reuse=True or reuse=tf.AUTO_REUSE in VarScope?

variable_scope和name_scope差别的更多相关文章

  1. Tensorflow中的name_scope和variable_scope

    Tensorflow是一个编程模型,几乎成为了一种编程语言(里面有变量.有操作......). Tensorflow编程分为两个阶段:构图阶段+运行时. Tensorflow构图阶段其实就是在对图进行 ...

  2. tensorflow里面共享变量、name_scope, variable_scope等如何理解

    tensorflow里面共享变量.name_scope, variable_scope等如何理解 name_scope, variable_scope目的:1 减少训练参数的个数. 2 区别同名变量 ...

  3. tf.variable和tf.get_Variable以及tf.name_scope和tf.variable_scope的区别

    在训练深度网络时,为了减少需要训练参数的个数(比如具有simase结构的LSTM模型).或是多机多卡并行化训练大数据大模型(比如数据并行化)等情况时,往往需要共享变量.另外一方面是当一个深度学习模型变 ...

  4. tensorflow中的name_scope, variable_scope

    在训练深度网络时,为了减少需要训练参数的个数(比如LSTM模型),或者是多机多卡并行化训练大数据.大模型等情况时,往往就需要共享变量.另外一方面是当一个深度学习模型变得非常复杂的时候,往往存在大量的变 ...

  5. 通俗理解tf.name_scope()、tf.variable_scope()

    前言:最近做一个实验,遇到TensorFlow变量作用域问题,对tf.name_scope().tf.variable_scope()等进行了较为深刻的比较,记录相关笔记:tf.name_scope( ...

  6. Tensorflow 之 name/variable_scope 变量管理

    name/variable_scope 的作用 充分理解 name / variable_scope TensorFlow 入门笔记 当一个神经网络比较复杂.参数比较多时,就比较需要一个比较好的方式来 ...

  7. Tensorflow常用函数说明(一)

    首先最开始应该清楚一个知识,最外面的那个[ [ [ ]]]括号代表第一维,对应维度数字0,第二个对应1,多维时最后一个对应数字-1:因为后面有用到 1 矩阵变换 tf.shape(Tensor) 返回 ...

  8. [TensorBoard] Name & Variable scope

    TF有两个scope, 一个是name_scope一个是variable_scope 第一个程序: with tf.name_scope("hello") as name_scop ...

  9. 2.4scope

    name_scope variable_scope scope (name_scope/variable_scope) from __future__ import print_function im ...

随机推荐

  1. Xcode6 iOS7模拟器和Xcode7 iOS8模拟器离线下载

    Xcode6 只支持iOS7和iOS8的模拟器 Xcode7 只支持iOS9和iOS8的模拟器 Xcode 并不会识别 SDKs 目录下的模拟器,我经过一些尝试以后,发现要放在这个目录下: /Libr ...

  2. vuessr nuxt入门指南

    nuxt.js 官网地址:https://zh.nuxtjs.org/guide/installation 1.安装 vue init nuxt-community/starter-template ...

  3. 视频监控系统:C/S & B/S

    近期一直和视频监控打交道.也做了大量的工作.当中包括了监控系统的两种模式实现: 1.C/S架构 C/S架构,主要是Android系统下监控client设计,基于VLC实现,可直接观看IP摄像机RTSP ...

  4. WINUSB Descriptors

    Reading string descriptors: String (0x01): "Travis Robinson" String (0x02): "Benchmar ...

  5. WebClient 通过get和post请求api

    //get 请求        string url = string.Format("http://localhost:28450/api/values?str1=a&str2=b ...

  6. AutoMapper在MVC中的运用07-映射在订单场景的例子

    本文参考了Taswar Bhatti的博客,他写了<Instant AutoMapper>这本书.遗憾的是,这本电子版书在国内还买不到,也下载不到.也只能从他的有限几篇博文中来窥探一二了. ...

  7. Windows Phone本地数据库(SQLCE):13、更新数据(翻译)

    这是“windows phone mango本地数据库(sqlce)”系列短片文章的第十三篇. 为了让你开始在Windows Phone Mango中使用数据库,这一系列短片文章将覆盖所有你需要知道的 ...

  8. DELPHI新的变量的声明方法

    DELPHI新的变量的声明方法 从DELPHI 10.3.1开始支持新的变量声明方法: procedure TForm1.查询1Click(Sender: TObject); begin var ur ...

  9. iOS本地化应用程序

    因为使用的是xcode4,应用程序本地化的问题跟以前的版本还是有些不同,在网上找了些资料对于xcode4以上的版本资料还是相对较少,有些最后要通过手动创建文件,这样操作实在是太麻烦,所以经过一个下午的 ...

  10. Struts2 注解模式

    相信大家一定看到了两个class中定义了一样的action,不过看类的元数据,是不同的命名空间.这里比较重要(对我来说)的是 @Action(value = "/login", r ...