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在命名冲突时自动处理冲突问题
1 import tensorflow as tf 2 a1 = tf.Variable(tf.constant(1.0, shape=[1]),name="a") 3 a2 = tf.Variable(tf.constant(1.0, shape=[1]),name="a") 4 print(a1) 5 print(a2) 6 print(a1==a2) 7 8 9 ### 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的情况下变量命名冲突时报错
1 import tensorflow as tf 2 a3 = tf.get_variable("a", shape=[1], initializer=tf.constant_initializer(1.0)) 3 a4 = tf.get_variable("a", shape=[1], initializer=tf.constant_initializer(1.0)) 4 5 6 ### 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命名受它影响
1 import tensorflow as tf 2 a = tf.Variable(tf.constant(1.0, shape=[1]),name="a") 3 with tf.name_scope('layer2'): 4 a1 = tf.Variable(tf.constant(1.0, shape=[1]),name="a") 5 a2 = tf.Variable(tf.constant(1.0, shape=[1]),name="a") 6 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)) 该句会报错 8 print(a) 9 print(a1) 10 print(a2) 11 print(a3) 12 print(a1==a2) 13 14 15 ### 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在变量已存在则自动复用,不存在则创建
1 import tensorflow as tf 2 with tf.variable_scope('layer1',reuse=tf.AUTO_REUSE): 3 a1 = tf.Variable(tf.constant(1.0, shape=[1]),name="a") 4 a2 = tf.Variable(tf.constant(1.0, shape=[1]),name="a") 5 a3 = tf.get_variable("a", shape=[1], initializer=tf.constant_initializer(1.0)) 6 a4 = tf.get_variable("a", shape=[1], initializer=tf.constant_initializer(1.0)) 7 print(a1) 8 print(a2) 9 print(a1==a2) 10 print(a3) 11 print(a4) 12 print(a3==a4) 13 14 15 ### 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
!!!
转载于:https://www.cnblogs.com/jfl-xx/p/9885662.html
tensorflow中命名空间、变量命名的问题相关推荐
- tensorflow中的变量管理
import tensorflow as tf# variable_scope()示例 """ tensorflow中通过变量名称获取变量的机制主要是通过tf.get_v ...
- python中的变量命名规则
There are only two hard things in Computer Science: cache invalidation and naming things. 在计算机科学领域只有 ...
- 《TensorFlow:实战Google深度学习框架》——5.3 TensorFlow中的变量初始化函数
- tensorflow中张量、常量、变量、占位符
引言 从实例出发 #先导入TensorFlow import tensorflow as tf# Create TensorFlow object called hello_constant hell ...
- @value 静态变量_Java中的变量——通过示例学习Java编程(4)
作者:CHAITANYA SINGH 来源: 通过示例学习Java编程(4):Java中的变量-方家话题www.koofun.com 变量是用来存放可以更改的值的容器.例如,当我写int i = 1 ...
- android r中的变量_R中的变量
android r中的变量 Variables in R are the same as the notion of variables in any other programming langua ...
- tensorflow中name_scope和variable_scope变量的使用
1. variable_scope的使用 首先,使用variable_scope可以很方便的管理get_varibale. 如何确定 get_variable 的 prefixed name? 1.1 ...
- Tensorflow中scope命名方法
两篇文章掌握Tensorflow中scope用法: [1]Tensorflow中scope命名方法(本文) [2]Tensorflow中tf.name_scope() 和 tf.variable_sc ...
- 编程参考 - 编程中给变量起名时如何选择前缀,以及匈牙利命名法等
我最开始当程序员用C语言写代码,公司里推行编码规范,变量的前缀都是有规定的. 比如整型变量,前面都是 u8Name, i8Name, u16Name, i16Name之类的. 尤其是嵌入式编程,涉及到 ...
- python重命名文件pycharm_Python中批量修改变量名太费劲?Pycharm中使用重命名一次搞定...
标签:rename current 变量 阅读 tor 小伙伴 search 其他 就是如果程序中有一个变量被用得比较多,但名字起得不是很好,导致其他阅读程序的人搞不清 ...
最新文章
- Facebook工程师教你什么是随机森林,就算零基础也可以看懂 | 干货
- 如何系统的自学python 知乎-如何系统地自学 Python?
- 搜索Maven的仓库
- Struts2开发基本步骤
- HDU1010:Tempter of the Bone(dfs+剪枝)
- ci框架的session类,怎么使用ci的session类
- 用pfx证书java双向认证_把CA证书生成的crt的证书和pem的私钥转换成java能够使用的keystore和pcks12的证书,实现https双向认证...
- mysql之 OPTIMIZE TABLE整理碎片
- java的封装和this关键字知识整理
- rails提供的validators
- 终章-剑之魂【模拟】【贪心】
- pycharm多行代码同时注释、去除注释
- linux 系统调试工具,Linux 系统调试...
- 卷积神经网络的几种模型
- msyql数据库[云图智联]
- 离散作业--求左陪集
- 公司规章制度制度(受不了)
- linux 下载百度网盘资源 centos安装aria2
- Linux三剑客练习
- IDEA try catch快捷键、 快速生成get set 方法快捷键、快速生成有参和无参构造方法快捷键
热门文章
- liunx中常用命令 -大数据
- mysql odbc 驱动程序不支持所需的属性_ODBC 驱动程序不支持所需的属性。
- Spring Framework 常用工具类一
- ProcessBuilder 创建操作系统进程
- 阶段1 语言基础+高级_1-3-Java语言高级_06-File类与IO流_01 File类_7_File类创建删除功能的方法...
- System Operations on AWS - Lab 7 - CloudFormation
- PL/SQL developer连接远程ORACLE
- UVa 1658,Admiral (拆点+限制最小费用流)
- [Noi2015]寿司晚宴
- java利用Google Zxing实现 二维码生成与解析