tensorflow中命名空间、变量命名的问题
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中命名空间、变量命名的问题的更多相关文章
- TensorFlow中的变量命名以及命名空间.
What: 在Tensorflow中, 为了区别不同的变量(例如TensorBoard显示中), 会需要命名空间对不同的变量进行命名. 其中常用的两个函数为: tf.variable_scope, t ...
- 83、Tensorflow中的变量管理
''' Created on Apr 21, 2017 @author: P0079482 ''' #如何通过tf.variable_scope函数来控制tf.ger_variable函数获取已经创建 ...
- TensorFlow中的变量和常量
1.TensorFlow中的变量和常量介绍 TensorFlow中的变量: import tensorflow as tf state = tf.Variable(0,name='counter') ...
- 2、Tensorflow中的变量
2.Tensorflow中的变量注意:tf中使用 变量必须先初始化下面是一个使用变量的TF代码(含注释): # __author__ = "WSX" import tensorfl ...
- JavaScript 中的变量命名方法
三种命名方法 在程序语言中,通常使用的变量命名方法有三种:骆驼命名法(CamelCase),帕斯卡命名法(PascalCase)和匈牙利命名法. 依靠单词的大小写拼写复合词的做法,叫做"骆驼 ...
- 深度学习原理与框架-Tensorflow基本操作-Tensorflow中的变量
1.tf.Variable([[1, 2]]) # 创建一个变量 参数说明:[[1, 2]] 表示输入的数据,为一行二列的数据 2.tf.global_variables_initializer() ...
- Tensorflow中的变量
从初识tf开始,变量这个名词就一直都很重要,因为深度模型往往所要获得的就是通过参数和函数对某一或某些具体事物的抽象表达.而那些未知的数据需要通过学习而获得,在学习的过程中它们不断变化着,最终收敛达到较 ...
- tensorflow中使用变量作用域及tf.variable(),tf,getvariable()与tf.variable_scope()的用法
一 .tf.variable() 在模型中每次调用都会重建变量,使其存储相同变量而消耗内存,如: def repeat_value(): weight=tf.variable(tf.random_no ...
- tensorflow中常量(constant)、变量(Variable)、占位符(placeholder)和张量类型转换reshape()
常量 constant tf.constant()函数定义: def constant(value, dtype=None, shape=None, name="Const", v ...
随机推荐
- 海思编解码芯片添加64M nor flash
uboot和内核都必须修改. struct spi_info hisfc350_spi_info_table[] : 在结构体里面添加自己的flash节点,我这里用的是MX66LS51235E { & ...
- C#基础加强(3)之值、引用类型及结构体
值.引用类型 介绍 引用类型派生自 System.Object ,而值类型均隐式派生自 System.ValueType . 其实 System.ValueType 也是继承自 System.Obje ...
- C#基础加强(8)之委托和事件
委托 简介 委托是一种可以声明出指向方法的变量的数据类型. 声明委托的方式 格式: delegate <返回值类型> 委托类型名(参数) ,例如: delegate void MyDel( ...
- go语言的安装与开发环境
安装golang编译器: https://studygolang.com/dl 之后设置环境变量GOPATH(项目目录) GOROOT(默认已经设置好) 安装编辑器:IDEA安装和破解 https: ...
- nginx rewrite 指令
ginx通过ngx_http_rewrite_module模块支持url重写.支持if条件判断,但不支持else. 该模块需要PCRE支持,应在编译nginx时指定PCRE源码目录, nginx安装方 ...
- Nginx技术研究系列7-Azure环境中Nginx高可用性和部署架构设计
前几篇文章介绍了Nginx的应用.动态路由.配置.在实际生产环境部署时,我们需要同时考虑Nginx的高可用性和部署架构. Nginx自身不支持集群以保证自身的高可用性,商业版本的Nginx+推荐: T ...
- samba服务器笔记 (一)
Samba安装 samba:主服务包:samba-client:客户端:samba-common:通用工具:samba4-libs:库:samba-winbind:windows域映射:samba-w ...
- wget下载阿里云RDS备份集
[root@localhost tmp]# more wget.sh #!/bin/bash download_url=`python /tmp/geturl.py` echo $download_u ...
- tp未验证内容-9
在tp的数据库配置中, convention.php中所有的选项都没有设置,要自己在Home/conf/config.php中自己设置, 注意几个地方,一是数据库的名字是: db_name,不是db_ ...
- Learning-MySQL【4】:表的操作管理和 MySQL 的约束控制
一.表的操作 1.表的基本概念 数据库与表之间的关系:数据库是由各种数据表组成的,数据表是数据库中最重要的对象,用来存储和操作数据的逻辑结构. 表由列和行组成,列是表数据的描述,行是表数据的实例. 表 ...