【TensorFlow-windows】name_scope与variable_scope
前言
探索一下variable_scope
和name_scope
相关的作用域,为下一章节tensorboard
的学习做准备
其实关于variable_scope
与get_variable
实现变量共享,在最开始的博客有介绍过:
【TensorFlow-windows】学习笔记二——低级API
当然还是国际惯例,参考博客:
tensorflow: name_scope 和 variable_scope区别及理解
tensorflow学习笔记(十七):name&variable scope
tensorflow
官方文档name_scope
tensorflow
官方文档get_varialbe
tensorflow
官方文档variable_scope
variable_scope相关
先引入对应包:
import tensorflow as tf
import numpy as np
Variable定义的变量是否共享
a1 = tf.Variable(1,name='aaa')
a2 = tf.Variable(2,name='aaa')
init=tf.initialize_all_variables()
with tf.Session() as sess:sess.run(init)print(a1.eval())#1print(a2.eval())#2
print(a1) #<tf.Variable 'aaa:0' shape=() dtype=int32_ref>
print(a2) #<tf.Variable 'aaa_1:0' shape=() dtype=int32_ref>
结论:Variable
定义的权重不共享,会自动给变量按照定义顺序加后缀_索引
,比如第2此定义的aaa
得到的名字是aaa_1
,所以他们是完全不同的两个变量,名称也不同。
Variable定义与get_variable定义有什么区别
#variable创建方式
tf.Variable(<initial-value>, name=<optional-name>)
#get_variable创建方式
tf.get_variable(name,shape=None,dtype=None,initializer=None,regularizer=None,trainable=True,collections=None,caching_device=None,partitioner=None,validate_shape=True,use_resource=None,custom_getter=None,constraint=None
)
结论:Variable
定义不实现共享,所以只需要初始值就行了,名字无所谓。get_variable
要通过名字实现共享,所以必须给变量一个名字,其余无所谓。
直接定义两个同名的get_variable
变量
b1 = tf.get_variable(name='b',initializer=10)
init = tf.initialize_all_variables()
with tf.Session() as sess:sess.run(init)print(b1.eval())#10
b2 = tf.get_variable(name='b',initializer=20)
'''
Variable b already exists, disallowed. Did you mean to set reuse=True or reuse=tf.AUTO_REUSE in VarScope? Originally defined at:
'''
结论:不能直接定义两个同名get_variable
变量,必须通过特定方法实现共享
第一种共享方法
在variable_scope
内部使用reuse_variables
函数:
with tf.variable_scope('dd') as scope:d1=tf.get_variable(name='d',initializer=10.0,dtype=tf.float32)scope.reuse_variables()d2=tf.get_variable(name='d',initializer=20.0,dtype=tf.float32)
上述在同一个变量空间中,定义两个同名变量,通过reuse_variable
实现共享,输出结果如下:
print(d1,d2)
init=tf.initialize_all_variables()
with tf.Session() as sess:sess.run(init)print(d1.eval(),d2.eval())
'''
<tf.Variable 'dd/d:0' shape=() dtype=float32_ref> <tf.Variable 'dd/d:0' shape=() dtype=float32_ref>
10.0 10.0
'''
结论:可以通过reuse_variable
实现共享,但是第二次初始化的值是无法覆盖第一次初始化的值的
第二种共享方法
在使用variable_scope
建立变量空间的时候,如果是复用一个已定义的变量空间中的变量,直接将reuse
设置为True
with tf.variable_scope('ff') as scope:f1=tf.get_variable(name='f',initializer=10.0,dtype=tf.float32)
with tf.variable_scope('ff',reuse=True) as scope:f2=tf.get_variable(name='f',initializer=20.0,dtype=tf.float32)
with tf.variable_scope('gg') as scope:g=tf.get_variable(name='f',initializer=20.0,dtype=tf.float32)
输出看看
print(f1,f2,g)
init=tf.initialize_all_variables()
with tf.Session() as sess:sess.run(init)print(f1.eval(),f2.eval(),g.eval())
'''
<tf.Variable 'ff/f:0' shape=() dtype=float32_ref> <tf.Variable 'ff/f:0' shape=() dtype=float32_ref> <tf.Variable 'gg/f:0' shape=() dtype=float32_ref>
10.0 10.0 20.0
'''
结论:可以通过函数参数reuse
实现变量共享
不同变量空间中的相同名称变量是否共享
结论:无法共享,比如上例中f1
和g
无法共享,虽然名称都是f
,但是所在变量空间不同。
第三种共享方法
调用函数的时候,自动检测是否需要reuse
with tf.variable_scope('ee',reuse=tf.AUTO_REUSE) as scope:e2=tf.get_variable(name='e',initializer=10.0,dtype=tf.float32)e3=tf.get_variable(name='e',initializer=20.0,dtype=tf.float32)
with tf.variable_scope('ee',reuse=tf.AUTO_REUSE) as scope:e4=tf.get_variable(name='e',initializer=30.0,dtype=tf.float32)
init = tf.initialize_all_variables()
with tf.Session() as sess:sess.run(init)print(e2,e3,e4)print(e2.eval(),e3.eval(),e4.eval())
'''
<tf.Variable 'ee/e:0' shape=() dtype=float32_ref> <tf.Variable 'ee/e:0' shape=() dtype=float32_ref> <tf.Variable 'ee/e:0' shape=() dtype=float32_ref>
10.0 10.0 10.0
'''
如果之前没创建过共享变量,不适用自动检测,而直接reuse
会报错
with tf.variable_scope('ee',reuse=True) as scope:e2=tf.get_variable(name='e',initializer=10.0,dtype=tf.float32)
'''Variable ee/e does not exist, or was not created with tf.get_variable(). Did you mean to set reuse=tf.AUTO_REUSE in VarScope?
'''
结论:如果不确定之前是否创建过共享变量,最好是使用AUTO_REUSE
自动检测
name_scope相关
直接用namescope是否能隔绝共享变量
with tf.name_scope('f') as nscope:f1 = tf.get_variable('f',initializer=10.0,dtype=tf.float32)
with tf.name_scope('g') as nscope:g = tf.get_variable('f',initializer=20.0,dtype=tf.float32)
'''
Variable f already exists, disallowed. Did you mean to set reuse=True or reuse=tf.AUTO_REUSE in VarScope? Originally defined at:
'''
结论:无法直接用name_scope
隔绝共享变量
在不同namesope中是否可以共享变量
创建两个命名空间,但是两个命名空间包含相同的变量空间,设置共享变量h
with tf.name_scope('test1') as scope:with tf.variable_scope('h',reuse=tf.AUTO_REUSE):h1 = tf.get_variable('hh',initializer=10.0,dtype=tf.float32)
with tf.name_scope('test2') as scope:with tf.variable_scope('h',reuse=tf.AUTO_REUSE):h2 = tf.get_variable('hh',initializer=10.0,dtype=tf.float32)
测试是否能实现变量共享
init = tf.initialize_all_variables()
with tf.Session() as sess:sess.run(init)print(h1,h2)print(h1.eval(),h2.eval())op=tf.assign(h1,30.0)sess.run(op)print(h1.eval(),h2.eval())
'''
<tf.Variable 'h/hh:0' shape=() dtype=float32_ref> <tf.Variable 'h/hh:0' shape=() dtype=float32_ref>
10.0 10.0
30.0 30.0
'''
结论:命名空间不能控制是否共享,但是变量空间可以控制变量共享。
总结
get_variable
与variable_scope
配合可以实现变量共享。
name_scope
无法实现变量共享,但是如果看过之前的博客可以发现,它的一个作用是将一系列操作封装在一起,这样画图的时候网络结构比较清晰。
其它作用以后遇到再补充,主要是为了下一章节学习tensorboard
做准备。
【TensorFlow-windows】name_scope与variable_scope相关推荐
- tensorflow中name_scope和variable_scope变量的使用
1. variable_scope的使用 首先,使用variable_scope可以很方便的管理get_varibale. 如何确定 get_variable 的 prefixed name? 1.1 ...
- Tensorflow name_scope和variable_scope的用法
Tensorflow name_scope和variable_scope 的用法 简介 当一个神经网络比较复杂.参数比较多时,就比较需要一个比较好的方式来传递和管理这些参数.而Tensorflow提供 ...
- TensorFlow学习笔记(1):variable与get_variable, name_scope()和variable_scope()
Variable tensorflow中有两个关于variable的op,tf.Variable()与tf.get_variable()下面介绍这两个的区别 使用tf.Variable时,如果检测到命 ...
- TensorFlow windows之Tensorboard使用
TensorBoard是什么 TensorBoard:Tensorflow自带的可视化工具,TensorBoard 来展现 TensorFlow 图像,绘制图像生成的定量指标图以及附加数据 关于如何在 ...
- tensorflow windows
conda create -n py35 python=3.5 activate py35 pip install --ignore-installed --upgrade https://stora ...
- TensorFlow基础笔记(13) tf.name_scope tf.variable_scope学习
转载http://blog.csdn.net/jerr__y/article/details/60877873 1. 首先看看比较简单的 tf.name_scope('scope_name'). tf ...
- Tensorflow中的name_scope和variable_scope
Tensorflow是一个编程模型,几乎成为了一种编程语言(里面有变量.有操作......). Tensorflow编程分为两个阶段:构图阶段+运行时. Tensorflow构图阶段其实就是在对图进行 ...
- tf.name_scope tf.variable_scope学习
1. 首先看看比较简单的 tf.name_scope('scope_name'). tf.name_scope 主要结合 tf.Variable() 来使用,方便参数命名管理. ''' Signatu ...
- tensorflow tf.name_scope() 命名空间(用于规定对象和操作属于哪个区域)
tf.name_scope()规定了对象和操作属于哪个区域 本质上name_scope只对对象的name属性进行圈定,并不会对其作用域产生任何影响 tf.name_scope('cgx_scope') ...
最新文章
- Python将两个图像合并成一个图像(横向合并)
- IntelliJ 中设置与Eclipse中 Ctrl+1 功能类似的快捷键
- Python列表解析式总结
- jquery设置表单元素只读_jquery设置元素readonly与disabled属性
- Spring Boot2.0 JPA 实现分页(简单查询分页、复杂查询分页)
- HTML单元格怎么加单元格,如何根据其值在HTML单元格中添加类
- 8个月打磨,一份送给程序员的「分布式系统」合集
- sklearn 线性回归
- 《一分钟经理人》学习笔记第五部分---一分钟表扬为什么有效
- SnapKit 是怎样炼成的 | 掘金技术征文
- [论文阅读] 激光点云分割-RPVNet
- 电影《寒战2》中的管理知识
- 计算机测配色原理,计算机测配色教学方法论文
- 调研报告之——可见光通信与可见光定位
- html5微课程制作,翟猛老师《微课开发及制作-基于H5课件制作模式》
- php程序员 一万小时定律,科学网—一万小时定律——阅读笔记 - 贾琳的博文
- 〖大前端 - 基础入门三大核心之CSS篇②〗- CSS选择器之标签选择器、id选择器、class选择器与原子类
- Android代码动态设置圆角,颜色,线条背景
- sketchup技巧分享-如何利用组件创建无缝图案峥
- 【若依(ruoyi)】template might not exist or might not be accessible by any of the configured Template Res
热门文章
- Honeycomb Gym - 102028F(bfs)
- B - Greg's Workout CodeForces - 255A(思维)
- ROS2学习(十一).ROS概念 - 命令行工具的使用
- linux centos密码忘记,CentOS忘记root密码的解决办法
- php长传文件到数据库,php上传文件并存储到mysql数据库的简单示例
- 如果让我重做一次研究生--王泛森院士
- linux中 tar 报参数列表过长,四种解决”Argument list too long”参数列表过长的办法...
- 抖音只能上下滑动吗_抖音:如何靠评论,轻松涨粉十万
- 数据库SQL语言从入门到精通--Part 4--SQL语言中的模式、基本表、视图
- java kafka 多线程消费