1.简介

对比分析tf.Variable / tf.get_variable | tf.name_scope / tf.variable_scope的异同

2.说明

  • tf.Variable创建变量;tf.get_variable创建与获取变量
  • tf.Variable自动检测命名冲突并且处理;tf.get_variable在没有设置reuse时会报错
  • tf.name_scope没有reuse功能,tf.get_variable在变量冲突时报错;tf.variable_scope有reuse功能,可配合tf.get_variable实现变量共享
  • tf.get_variable变量命名不受tf.name_scope的影响;tf.Variable受两者的影响

3.代码示例

3.1 tf.Variable

tf.Variable在命名冲突时自动处理冲突问题

 import tensorflow as tf
a1 = tf.Variable(tf.constant(1.0, shape=[1]),name="a")
a2 = tf.Variable(tf.constant(1.0, shape=[1]),name="a")
print(a1)
print(a2)
print(a1==a2) ###
10 <tf.Variable 'a:0' shape=(1,) dtype=float32_ref>
11 <tf.Variable 'a_1:0' shape=(1,) dtype=float32_ref>
12 False

3.2 tf.get_variable

tf.get_variable在没有设置命名空间reuse的情况下变量命名冲突时报错

 import tensorflow as tf
a3 = tf.get_variable("a", shape=[1], initializer=tf.constant_initializer(1.0))
a4 = tf.get_variable("a", shape=[1], initializer=tf.constant_initializer(1.0)) ###
7 ValueError: Variable a already exists, disallowed.
8 Did you mean to set reuse=True or reuse=tf.AUTO_REUSE in VarScope?

3.3 tf.name_scope

tf.name_scope没有reuse功能,tf.get_variable命名不受它影响,并且命名冲突时报错;tf.Variable命名受它影响

 import tensorflow as tf
a = tf.Variable(tf.constant(1.0, shape=[1]),name="a")
with tf.name_scope('layer2'):
a1 = tf.Variable(tf.constant(1.0, shape=[1]),name="a")
a2 = tf.Variable(tf.constant(1.0, shape=[1]),name="a")
a3 = tf.get_variable("a", shape=[1], initializer=tf.constant_initializer(1.0))
7 # a4 = tf.get_variable("a", shape=[1], initializer=tf.constant_initializer(1.0)) 该句会报错
print(a)
print(a1)
print(a2)
print(a3)
print(a1==a2) ###
16 <tf.Variable 'a:0' shape=(1,) dtype=float32_ref>
17 <tf.Variable 'layer2/a:0' shape=(1,) dtype=float32_ref>
18 <tf.Variable 'layer2/a_1:0' shape=(1,) dtype=float32_ref>
19 <tf.Variable 'a_1:0' shape=(1,) dtype=float32_ref>
20 False

3.4 tf.variable_scope

tf.variable_scope可以配tf.get_variable实现变量共享;reuse默认为None,有False/True/tf.AUTO_REUSE可选:

  • 设置reuse = None/False时tf.get_variable创建新变量,变量存在则报错
  • 设置reuse = True时tf.get_variable只讲获取已存在的变量,变量不存在时报错
  • 设置reuse = tf.AUTO_REUSE时tf.get_variable在变量已存在则自动复用,不存在则创建
 import tensorflow as tf
with tf.variable_scope('layer1',reuse=tf.AUTO_REUSE):
a1 = tf.Variable(tf.constant(1.0, shape=[1]),name="a")
a2 = tf.Variable(tf.constant(1.0, shape=[1]),name="a")
a3 = tf.get_variable("a", shape=[1], initializer=tf.constant_initializer(1.0))
a4 = tf.get_variable("a", shape=[1], initializer=tf.constant_initializer(1.0))
print(a1)
print(a2)
print(a1==a2)
print(a3)
print(a4)
print(a3==a4) ###
16 <tf.Variable 'layer1_1/a:0' shape=(1,) dtype=float32_ref>
17 <tf.Variable 'layer1_1/a_1:0' shape=(1,) dtype=float32_ref>
18 False
19 <tf.Variable 'layer1/a_2:0' shape=(1,) dtype=float32_ref>
20 <tf.Variable 'layer1/a_2:0' shape=(1,) dtype=float32_ref>
21 True

!!!

tensorflow中命名空间、变量命名的问题的更多相关文章

  1. TensorFlow中的变量命名以及命名空间.

    What: 在Tensorflow中, 为了区别不同的变量(例如TensorBoard显示中), 会需要命名空间对不同的变量进行命名. 其中常用的两个函数为: tf.variable_scope, t ...

  2. 83、Tensorflow中的变量管理

    ''' Created on Apr 21, 2017 @author: P0079482 ''' #如何通过tf.variable_scope函数来控制tf.ger_variable函数获取已经创建 ...

  3. TensorFlow中的变量和常量

    1.TensorFlow中的变量和常量介绍 TensorFlow中的变量: import tensorflow as tf state = tf.Variable(0,name='counter') ...

  4. 2、Tensorflow中的变量

    2.Tensorflow中的变量注意:tf中使用 变量必须先初始化下面是一个使用变量的TF代码(含注释): # __author__ = "WSX" import tensorfl ...

  5. JavaScript 中的变量命名方法

    三种命名方法 在程序语言中,通常使用的变量命名方法有三种:骆驼命名法(CamelCase),帕斯卡命名法(PascalCase)和匈牙利命名法. 依靠单词的大小写拼写复合词的做法,叫做"骆驼 ...

  6. 深度学习原理与框架-Tensorflow基本操作-Tensorflow中的变量

    1.tf.Variable([[1, 2]])  # 创建一个变量 参数说明:[[1, 2]] 表示输入的数据,为一行二列的数据 2.tf.global_variables_initializer() ...

  7. Tensorflow中的变量

    从初识tf开始,变量这个名词就一直都很重要,因为深度模型往往所要获得的就是通过参数和函数对某一或某些具体事物的抽象表达.而那些未知的数据需要通过学习而获得,在学习的过程中它们不断变化着,最终收敛达到较 ...

  8. tensorflow中使用变量作用域及tf.variable(),tf,getvariable()与tf.variable_scope()的用法

    一 .tf.variable() 在模型中每次调用都会重建变量,使其存储相同变量而消耗内存,如: def repeat_value(): weight=tf.variable(tf.random_no ...

  9. tensorflow中常量(constant)、变量(Variable)、占位符(placeholder)和张量类型转换reshape()

    常量 constant tf.constant()函数定义: def constant(value, dtype=None, shape=None, name="Const", v ...

随机推荐

  1. 详细解读html中的Map,area标签

    一.定义 定义一个客户端图像映射.图像映射(image-map)指带有可点击区域的一幅图像. 二.使用 <!--定义一个图像 他的边框为0(border) usemap(指定该图像被用作图像地图 ...

  2. python 判断两个ip是不是处于同一网段

    a_ip:10.10.15.100b_ip:10.10.15.101c_ip:10.10.10.100netmask:255.255.255.0 def numtobinary(num): binar ...

  3. Dvelopment descriptor

    部署描述符是JavaEE程序常见的一部分,但是之前都没有较为全面的学习过,这里就较为全面的记录一下部署描述符中的元素.部署一个Servlet 3 或以上应用程序是一件轻而易举的事.通过Servlet注 ...

  4. nodejs笔记之文件操作

    文件操作包含: 读取文件 写入文件 删除文件 创建目录 删除目录 读取文件: // 异步操作 var fs = require("fs"); fs.readFile(". ...

  5. win的使用

    1.net use 挂在远端文件系统,方便本机软件操作! 添加挂载文件   net use  h:   //192.168.1.1/file 删除挂载文件   net use /delete  h: ...

  6. PHP保留两位小数并且四舍五入及不四舍五入的方法

    php保留两位小数并且四舍五入 $num = 123213.666666; echo sprintf("%.2f", $num); php保留两位小数并且不四舍五入 $num = ...

  7. Springboot 上传excel并解析文件内容

    最近在做一个物业的系统,需要通过excel上传业主的信息,解析并入库. 参考:https://www.cnblogs.com/jyyjava/p/8074322.html 话不多说,直接上核心代码 i ...

  8. JDK1.7安装和配置及注意事项

    要求 必备知识 windows 7 基本操作. 运行环境 windows 7 下载地址 环境下载 下载JDK 下载地址:http://www.oracle.com/technetwork/java/j ...

  9. 新建Maven项目建成后本应该有的src/main/java和src/test/java目录并没有出现:

    转自:http://www.cnblogs.com/dong-dong-dong/p/9565466.html 新建Maven项目建成后本应该有的src/main/java和src/test/java ...

  10. G711 G723 G729线路占多少带宽问题

    G.711   G.711   也称为PCM(脉冲编码调制),是国际电信联盟订定出来的一套语音压缩标准,主要用于电话.它主要用脉冲编码调制对音频采样,采样率为8k每秒.它利用一个 64Kbps 未压缩 ...