Tensorflow 之 name/variable_scope 变量管理
name/variable_scope 的作用
充分理解 name / variable_scope
TensorFlow 入门笔记
- 当一个神经网络比较复杂、参数比较多时,就比较需要一个比较好的方式来传递和管理这些参数。而Tensorflow提供了通过变量名称来创建或者获取变量的机制。通过这个机制,可以在不同的函数中直接通过变量的名称来使用变量,而不需要将变量通过参数进行传递。
* name_scope: * 为了更好地管理变量的命名空间而提出的。比如在 tensorboard 中,因为引入了 name_scope, 我们的 Graph 看起来才井然有序。
* variable_scope: * 大大大部分情况下,跟 tf.get_variable() 配合使用,实现变量共享的功能。
2.三种方式创建变量: tf.placeholder, tf.Variable, tf.get_variable
从上面的实验结果来看,这三种方式所定义的变量具有相同的类型。而且只有 tf.get_variable() 创建的变量之间会发生命名冲突。在实际使用中,三种创建变量方式的用途也是分工非常明确的。其中
- tf.placeholder() 占位符。* trainable==False *
- tf.Variable() 一般变量用这种方式定义。 * 可以选择 trainable 类型 *
- tf.get_variable() 一般都是和 tf.variable_scope() 配合使用,从而实现变量共享的功能。 * 可以选择 trainable 类型 *
- tf.get_variable的作用不仅在于创建变量,它还可以通过变量名称获取变量,但在创建变量时,如果创建失败(变量已经存在),那么就会报错。因此,就需要一个上下文管理器tf.variable_scope,可以更好的管理变量,并且可以增加程序的可读性。
- with tf.variable_scope('foo',reuse=None):参数reuse=True时,通过tf.get_variable只能获取已经存在的变量名。上下文管理器在嵌套的时候,变量名称的前面会加上上下文管理器的名称。
3. 探索 name_scope 和 variable_scope
tf.name_scope() 并不会对 tf.get_variable() 创建的变量有任何影响。
tf.name_scope() 主要是用来管理命名空间的,这样子让我们的整个模型更加有条理。而 tf.variable_scope() 的作用是为了实现变量共享,它和 tf.get_variable() 来完成变量共享的功能。
- 首先我们要确立一种 Graph 的思想。在 TensorFlow 中,我们定义一个变量,相当于往 Graph 中添加了一个节点。和普通的 python 函数不一样,在一般的函数中,我们对输入进行处理,然后返回一个结果,而函数里边定义的一些局部变量我们就不管了。但是在 TensorFlow 中,我们在函数里边创建了一个变量,就是往 Graph 中添加了一个节点。出了这个函数后,这个节点还是存在于 Graph 中的。
name_scope
返回的是 string, 而variable_scope
返回的是对象. 这也可以感觉到,variable_scope
能干的事情比name_scope
要多.- name_scope对 get_variable()创建的变量 的名字不会有任何影响,而创建的
op
会被加上前缀. - tf.get_variable_scope() 返回的只是 variable_scope,不管 name_scope. 所以以后我们在使用tf.get_variable_scope().reuse_variables() 时可以无视name_scope
总结简单来看
1. 使用tf.Variable()
的时候,tf.name_scope()
和tf.variable_scope()
都会给 Variable
和 op
的 name
属性加上前缀。
2. 使用tf.get_variable()
的时候,tf.name_scope()
就不会给 tf.get_variable()
创建出来的Variable
加前缀。但是 tf.Variable()
创建出来的就会受到 name_scope
的影响.
name_scope可以用来干什么
典型的 TensorFlow 可以有数以千计的节点,如此多而难以一下全部看到,甚至无法使用标准图表工具来展示。为简单起见,我们为op/tensor
名划定范围,并且可视化把该信息用于在图表中的节点上定义一个层级。默认情况下, 只有顶层节点会显示。下面这个例子使用tf.name_scope在hidden命名域下定义了三个操作:
import tensorflow as tf with tf.name_scope('hidden') as scope:
a = tf.constant(5, name='alpha')
W = tf.Variable(tf.random_uniform([1, 2], -1.0, 1.0), name='weights')
b = tf.Variable(tf.zeros([1]), name='biases')
print a.name
print W.name
print b.name
结果是得到了下面三个操作名:
hidden/alpha
hidden/weights
hidden/biases
name_scope 是给op_name加前缀, variable_scope是给get_variable()创建的变量的名字加前缀。
tf.variable_scope有时也会处理命名冲突
import tensorflow as tf
def test(name=None):
with tf.variable_scope(name, default_name="scope") as scope:
w = tf.get_variable("w", shape=[2, 10])
test()
test()
ws = tf.trainable_variables()
for w in ws:
print(w.name)
#scope/w:0
#scope_1/w:0
#可以看出,如果只是使用default_name这个属性来创建variable_scope
#的时候,会处理命名冲突
共享变量
TensorFlow 支持两种共享变量的方式:
- 显式传递
tf.Variable
对象。 - 在
tf.variable_scope
对象内隐式包装tf.Variable
对象。
虽然显式传递变量的代码非常清晰,但有时编写 TensorFlow 函数(在实现中隐式使用变量)非常方便。tf.layer
中的大多数功能层以及所有 tf.metrics
和部分其他库工具都使用这种方法。
变量作用域允许您在调用隐式创建和使用变量的函数时控制变量重用。作用域还允许您以分层和可理解的方式命名变量。
例如,假设我们编写一个函数来创建一个卷积/relu 层:
def conv_relu(input, kernel_shape, bias_shape):
# Create variable named "weights".
weights = tf.get_variable("weights", kernel_shape,
initializer=tf.random_normal_initializer())
# Create variable named "biases".
biases = tf.get_variable("biases", bias_shape,
initializer=tf.constant_initializer(0.0))
conv = tf.nn.conv2d(input, weights,
strides=[1, 1, 1, 1], padding='SAME')
return tf.nn.relu(conv + biases)
此函数使用短名称 weights
和 biases
,这有利于清晰区分二者。然而,在真实模型中,我们需要很多此类卷积层,而且重复调用此函数将不起作用:
input1 = tf.random_normal([1,10,10,32])
input2 = tf.random_normal([1,20,20,32])
x = conv_relu(input1, kernel_shape=[5, 5, 32, 32], bias_shape=[32])
x = conv_relu(x, kernel_shape=[5, 5, 32, 32], bias_shape = [32]) # This fails.
由于期望的操作不清楚(创建新变量还是重新使用现有变量?),因此 TensorFlow 将会失败。不过,在不同作用域内调用 conv_relu
可表明我们想要创建新变量:
def my_image_filter(input_images):
with tf.variable_scope("conv1"):
# Variables created here will be named "conv1/weights", "conv1/biases".
relu1 = conv_relu(input_images, [5, 5, 32, 32], [32])
with tf.variable_scope("conv2"):
# Variables created here will be named "conv2/weights", "conv2/biases".
return conv_relu(relu1, [5, 5, 32, 32], [32])
如果您想要共享变量,有两种方法可供选择。首先,您可以使用 variable_scope
:reuse=True
创建具有相同名称的作用域:
with tf.variable_scope("model"):
output1 = my_image_filter(input1)
with tf.variable_scope("model", reuse=True):
output2 = my_image_filter(input2)
您也可以调用 scope.reuse_variables()
以触发重用:
with tf.variable_scope("model") as scope:
output1 = my_image_filter(input1)
scope.reuse_variables()
output2 = my_image_filter(input2)
由于根据作用域的具体字符串名称初始化变量作用域可能比较危险,因此也可以根据另一作用域进行初始化:
with tf.variable_scope("model") as scope:
output1 = my_image_filter(input1)
with tf.variable_scope(scope, reuse=True):
output2 = my_image_filter(input2)
Tensorflow 之 name/variable_scope 变量管理的更多相关文章
- 83、Tensorflow中的变量管理
''' Created on Apr 21, 2017 @author: P0079482 ''' #如何通过tf.variable_scope函数来控制tf.ger_variable函数获取已经创建 ...
- Tensorflow函数——tf.variable_scope()
Tensorflow函数——tf.variable_scope()详解 https://blog.csdn.net/yuan0061/article/details/80576703 2018年06月 ...
- Tesnsorflow命名空间与变量管理参数reuse
一.TensorFlow中变量管理reuse参数的使用 1.TensorFlow用于变量管理的函数主要有两个: (1)tf.get_variable:用于创建或获取变量的值 (2)tf.varia ...
- 集成direnv 与docker-compose 进行环境变量管理
direnv 是一个不错的换将变量管理工具,同时日常的开发测试中我们使用docker-compose 会比较多,一般我们的玩法是 可以再docker-compose 中指定环境变量,可以通过envir ...
- direnv 一个强大的环境变量管理工具
direnv 是一个基于golang 编写的强大的环境变量管理工具,可以帮助我们简化环境变量管理,而且 支持的平台比较多. 基本使用 下载二进制软件包 https://github.com/dir ...
- Ansible_变量管理与设置
一.Ansible变量管理 1.变量概述 Ansible支持利用变量来存储值,并在Ansible项目的所有文件中重复使用这些值.这可以简化项目的创建和维护,并减少错误的数量 通过变量,可以轻松地在An ...
- TensorFlow解析常量、变量和占位符
TensorFlow解析常量.变量和占位符 最基本的 TensorFlow 提供了一个库来定义和执行对张量的各种数学运算.张量,可理解为一个 n 维矩阵,所有类型的数据,包括标量.矢量和矩阵等都是特殊 ...
- 吴裕雄 python 神经网络——TensorFlow 变量管理
import tensorflow as tf with tf.variable_scope("foo"): v = tf.get_variable("v", ...
- TensorFlow学习笔记3——变量共享
因为最近在研究生成对抗网络GAN,在读别人的代码时发现了 with tf.variable_scope(self.name_scope_conv, reuse = reuse): 这样一条语句,查阅官 ...
随机推荐
- 使用 Java 查找字符串中出现次数最多的字符以及出现的次数?
使用 Java 查找字符串中出现次数最多的字符以及出现的次数? import java.util.HashMap; import java.util.Map; public class TestStr ...
- 【笔试题】Java 中如何递归显示一个目录下面的所有目录和文件?
笔试题 Java 中如何递归显示一个目录下面的所有目录和文件? import java.io.File; public class Test { private static void showDir ...
- 【C#】利用反射构建实体
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.D ...
- Typora ---一款简洁的Markdown编辑器
Typora BB in front 如果你是一个佛(lan)系(duo),内心文艺的程序员,并且你对其他Markdown编辑器的使用效果感觉不是很好的话,可以来了解一下该软件Typora. What ...
- 【Java虚拟机】JVM学习笔记之GC
JVM学习笔记二之GC GC即垃圾回收,在C++中垃圾回收由程序员自己来做,例如可以用free和delete来回收对象.而在Java中,JVM替程序员来执行垃圾回收的工作,下面看看GC的详细原理和执行 ...
- String 字符串补0
method1: 前提是你的长度已经确定!比如规定现实10位! - 优点: 不需要都是数字类型 String str_m = "123X"; String str =&q ...
- Linux-数据库3
外键约束 如果表A的主关键字是表B中的字段,则该字段称为表B的外键,表A称为主表,表B称为从表. 外键是用来实现参照完整性的,不同的外键约束方式将可以使两张表紧密的结合起来,特别是修改或者删除的级联操 ...
- django添加REST_FRAMEWORK 接口浏览
1.安装rest_framework pip install djangorestframework 2.配置rest_framework ## 将rest_framework加入项目app列表 I ...
- 【BZOJ 2054】 2054: 疯狂的馒头 (并查集特技)
Input 第一行四个正整数N,M,p,q Output 一共输出N行,第i行表示第i个馒头的最终颜色(如果最终颜色是白色就输出0). Sample Input 4 3 2 4 Sample Outp ...
- Loj10172 涂抹果酱
题目描述 Tyvj 两周年庆典要到了,Sam 想为 Tyvj 做一个大蛋糕.蛋糕俯视图是一个 N×M 的矩形,它被划分成 N×M 个边长为 1×1 的小正方形区域(可以把蛋糕当成 NNN 行 MMM列 ...