tensorflow中的name_scope, variable_scope
在训练深度网络时,为了减少需要训练参数的个数(比如LSTM模型),或者是多机多卡并行化训练大数据、大模型等情况时,往往就需要共享变量。另外一方面是当一个深度学习模型变得非常复杂的时候,往往存在大量的变量和操作,如何避免这些变量名和操作名的唯一不重复,同时维护一个条理清晰的graph非常重要。因此,tensorflow中用tf.Variable(), tf.get_variable, tf.Variable_scope(), tf.name_scope() 几个函数来实现:
tf.Variable() 与 tf.get_variable() 的作用与区别:
1)tf.Variable() 会自动监测命名冲突并自行处理,但是tf.get_variable() 遇到重名的变量创建且没有设置为共享变量时,则会报错。
import tensorflow as tf; a1 = tf.Variable(tf.random_normal(shape=[2, 3], mean=0, stddev=1), name='a2') a2 = tf.Variable(tf.random_normal(shape=[2, 3], mean=0, stddev=1), name='a2') with tf.Session() as sess:
sess.run(tf.initialize_all_variables())
print(a1.name)
print(a2.name) # 输出
a2:0
a2_1:0
import tensorflow as tf; a1 = tf.get_variable(name='a1', shape=[1], initializer=tf.constant_initializer(1))
a3 = tf.get_variable(name='a1', shape=[1], initializer=tf.constant_initializer(1)) with tf.Session() as sess:
sess.run(tf.initialize_all_variables())
print(a1.name)
print(a3.name) # 输出
ValueError: Variable a1 already exists, disallowed. Did you mean to set reuse=True or reuse=tf.AUTO_REUSE in VarScope? Originally defined at:
2) tf.Variable() 和 tf.get_variable() 都是用于在一个name_scope下面获取或创建一个变量的两种方式,区别在于: tf.Variable()用于创建一个新变量,在同一个name_scope下面,可以创建相同名字的变量,底层实现会自动引入别名机制,两次调用产生了其实是两个不同的变量。tf.get_variable(<variable_name>)用于获取一个变量,并且不受name_scope的约束。当这个变量已经存在时,则自动获取;如果不存在,则自动创建一个变量。
import tensorflow as tf;
import numpy as np; with tf.name_scope('V1'): a1 = tf.Variable(tf.random_normal(shape=[2,3], mean=0, stddev=1), name='a2')
with tf.name_scope('V2'):
a2 = tf.Variable(tf.random_normal(shape=[2,3], mean=0, stddev=1), name='a2') with tf.Session() as sess:
sess.run(tf.initialize_all_variables())
print (a1.name)
print (a2.name) # 输出
V1/a2:0
V2/a2:0
import tensorflow as tf; with tf.name_scope('V1'):
a1 = tf.get_variable(name='a1', shape=[1], initializer=tf.constant_initializer(1))
with tf.name_scope('V2'):
a2 = tf.get_variable(name='a1', shape=[1], initializer=tf.constant_initializer(1)) with tf.Session() as sess:
sess.run(tf.initialize_all_variables())
print (a1.name)
print (a2.name) # 输出
Variable a1 already exists, disallowed. Did you mean to set reuse=True in VarScope? Originally defined at:
3)tf.name_scope() 与 tf.variable_scope(): tf.name_scope():主要用于管理一个图里面的各种op,返回的是一个以scope_name命名的context manager。一个graph会维护一个name_space的 堆,每一个namespace下面可以定义各种op或者子namespace,实现一种层次化有条理的管理,避免各个op之间命名冲突。 tf.variable_scope() 一般与tf.get_variable()配合使用,用于管理一个graph中变量的名字,避免变量之间的命名冲突。
import tensorflow as tf;
import numpy as np; with tf.variable_scope('V1'):
a1 = tf.get_variable(name='a1', shape=[1], initializer=tf.constant_initializer(1))
a2 = tf.Variable(tf.random_normal(shape=[2,3], mean=0, stddev=1), name='a2')
with tf.variable_scope('V2'):
a3 = tf.get_variable(name='a1', shape=[1], initializer=tf.constant_initializer(1))
a4 = tf.Variable(tf.random_normal(shape=[2,3], mean=0, stddev=1), name='a2') with tf.Session() as sess:
sess.run(tf.initialize_all_variables())
print (a1.name)
print (a2.name)
print (a3.name)
print (a4.name) # 输出
V1/a1:0
V1/a2:0
V2/a1:0
V2/a2:0
4)当要重复使用变量共享时,可以用tf.variable_scope() 和 tf.get_variable()来实现
import tensorflow as tf with tf.variable_scope('V1', reuse=None):
a1 = tf.get_variable(name='a1', shape=[1], initializer=tf.constant_initializer(1)) with tf.variable_scope('V1', reuse=True):
a2 = tf.get_variable(name='a1', shape=[1], initializer=tf.constant_initializer(1)) with tf.Session() as sess:
sess.run(tf.initialize_all_variables())
print(a1.name)
print(a2.name) #输出
V1/a1:0
V1/a1:0
上面的代码在第一个variable_scope中的reuse=None,在之后的variable_scope中若是要共享变量,就要将reuse=True。
tensorflow中的name_scope, variable_scope的更多相关文章
- tensorflow中使用tf.variable_scope和tf.get_variable的ValueError
ValueError: Variable conv1/weights1 already exists, disallowed. Did you mean to set reuse=True in Va ...
- Tensorflow中的name_scope和variable_scope
Tensorflow是一个编程模型,几乎成为了一种编程语言(里面有变量.有操作......). Tensorflow编程分为两个阶段:构图阶段+运行时. Tensorflow构图阶段其实就是在对图进行 ...
- tensorflow里面共享变量、name_scope, variable_scope等如何理解
tensorflow里面共享变量.name_scope, variable_scope等如何理解 name_scope, variable_scope目的:1 减少训练参数的个数. 2 区别同名变量 ...
- [翻译] Tensorflow中name scope和variable scope的区别是什么
翻译自:https://stackoverflow.com/questions/35919020/whats-the-difference-of-name-scope-and-a-variable-s ...
- TensorFlow中的变量命名以及命名空间.
What: 在Tensorflow中, 为了区别不同的变量(例如TensorBoard显示中), 会需要命名空间对不同的变量进行命名. 其中常用的两个函数为: tf.variable_scope, t ...
- tensorflow中slim模块api介绍
tensorflow中slim模块api介绍 翻译 2017年08月29日 20:13:35 http://blog.csdn.net/guvcolie/article/details/77686 ...
- 第二十二节,TensorFlow中的图片分类模型库slim的使用、数据集处理
Google在TensorFlow1.0,之后推出了一个叫slim的库,TF-slim是TensorFlow的一个新的轻量级的高级API接口.这个模块是在16年新推出的,其主要目的是来做所谓的“代码瘦 ...
- 第二十二节,TensorFlow中RNN实现一些其它知识补充
一 初始化RNN 上一节中介绍了 通过cell类构建RNN的函数,其中有一个参数initial_state,即cell初始状态参数,TensorFlow中封装了对其初始化的方法. 1.初始化为0 对于 ...
- 第十八节,TensorFlow中使用批量归一化(BN)
在深度学习章节里,已经介绍了批量归一化的概念,详情请点击这里:第九节,改善深层神经网络:超参数调试.正则化以优化(下) 神经网络在进行训练时,主要是用来学习数据的分布规律,如果数据的训练部分和测试部分 ...
随机推荐
- Java IO(2)阻塞式输入输出(BIO)
在上文中<Java IO(1)基础知识——字节与字符>了解到了什么是字节和字符,主要是为了对Java IO中有关字节流和字符流有一个更好的了解. 本文所述的输出输出指的是Java中传统的I ...
- Reinforcement Learning: An Introduction读书笔记(4)--动态规划
> 目 录 < Dynamic programming Policy Evaluation (Prediction) Policy Improvement Policy Iterat ...
- laravel5.5通过Migrations修改表 的artisan命令
1,不同表的修改都需要通过命令创建一个文件 2,首先通过artisan创建对应表的一个文件 php artisan make:module:migration abtinvitcard(模块名) al ...
- 小tips:JS之浅拷贝与深拷贝
浅拷贝: function extendCopy(p) { var c = {}; for (var i in p) { c[i] = p[i]; } return c; } 深拷贝: functio ...
- LVS + nginx实现高性能精准负载均衡
- 小程序实践(六):view内部组件排版
涉及知识点: 1.垂直排列,水平排列 2.居中对齐 示例: 1.默认排版 , 一个父组件里面两个子view 显示效果: 2.先给父view设置一个高度和颜色值,用于看效果 3.实现水平排列和垂直排列的 ...
- 深圳市共创力咨询为某大型上市企业提供两天的UCD内训与辅导服务!
2017年5月23和24日两天,深圳市共创力咨询为国内某大型上市企业提供了为期两天的内训与辅导服务.本次执行培训与辅导任务的是UCD(基于用户体验的设计)资深顾问蔷薇女士.蔷薇老师分别从UCD理论.U ...
- myBase7.0破解
永久性破解 Mybase7Pro破解版下载:链接:https://pan.baidu.com/s/1ezb8bSqI0OPZnVgx-snA_Q 提取码:n72t 1.下载后安装 myBase-De ...
- linux 命令之netstat
转自:http://www.maomao365.com/?p=699 linux 命令之netstat 在linux中netstat命令的作用是查看TCP/IP网络当前所开放端口,所对应的本地和外地端 ...
- spring applicationContext.xml最全约束
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.sp ...