tensorflow中共享变量 tf.get_variable 和命名空间 tf.variable_scope
tensorflow中有很多需要变量共享的场合,比如在多个GPU上训练网络时网络参数和训练数据就需要共享。
tf通过 tf.get_variable() 可以建立或者获取一个共享的变量。 tf.get_variable函数的作用从tf的注释里就可以看出来-- ‘Gets an existing variable with this name or create a new one’。
与 tf.get_variable 函数相对的还有一个 tf.Variable 函数,两者的区别是:
- tf.Variable定义变量的时候会自动检测命名冲突并自行处理,例如已经定义了一个名称是 ‘wg_1’的变量,再使用tf.Variable定义名称是‘wg_1’的变量,会自动把后一个变量的名称更改为‘wg_1_0’,实际相当于创建了两个变量,tf.Variable不可以创建共享变量。
- tf.get_variable定义变量的时候不会自动处理命名冲突,如果遇到重名的变量并且创建该变量时没有设置为共享变量,tf会直接报错。
变量可以共享之后还有一个问题就是当模型很大很复杂的时候,变量和操作的数量也比较庞大,为了方便对这些变量进行管理,维护条理清晰的graph结构,tf建立了一套共享机制,通过 变量作用域(命名空间,variable_scope)实现对变量的共享和管理。例如,cnn的每一层中,均有weights和biases这两个变量,通过tf.variable_scope()为每一卷积层命名,就可以防止变量命名重复。
与 tf.variable_scope相对的还有一个 tf.name_scope 函数,两者的区别是:
- tf.name_scope 主要用于管理一个图(graph)里面的各种操作,返回的是一个以scope_name命名的context manager。一个graph会维护一个name_space的堆,每一个namespace下面可以定义各种op或者子namespace,实现一种层次化有条理的管理,避免各个op之间命名冲突。
- tf.variable_scope 一般与tf.name_scope()配合使用,用于管理一个图(graph)中变量的名字,避免变量之间的命名冲突,tf.variable_scope允许在一个variable_scope下面共享变量。
# coding: utf-8
import tensorflow as tf
# 定义的基本等价
v1 = tf.get_variable("v", shape=[1], initializer= tf.constant_initializer(1.0))
v2 = tf.Variable(tf.constant(1.0, shape=[1]), name="v")
with tf.variable_scope("abc"):
v3=tf.get_variable("v",[1],initializer=tf.constant_initializer(1.0))
# 在变量作用域内定义变量,不同变量作用域内的变量命名可以相同
with tf.variable_scope("xyz"):
v4=tf.get_variable("v",[1],initializer=tf.constant_initializer(1.0))
with tf.variable_scope("xyz", reuse=True):
v5 = tf.get_variable("v")
v6 = tf.get_variable("v",[1])
with tf.variable_scope("foo"):
v7 = tf.get_variable("v", [1])
# 通过 tf.get_variable_scope().reuse_variables() 设置以下的变量是共享变量;
# 如果不加,v8的定义会由于重名而报错
tf.get_variable_scope().reuse_variables()
v8 = tf.get_variable("v", [1])
assert v7 is v8
with tf.variable_scope("foo_1") as foo_scope:
v = tf.get_variable("v", [1])
with tf.variable_scope(foo_scope):
w = tf.get_variable("w", [1])
with tf.variable_scope(foo_scope, reuse=True):
v1 = tf.get_variable("v", [1])
w1 = tf.get_variable("w", [1])
assert v1 is v
assert w1 is w
with tf.variable_scope("foo1"):
with tf.name_scope("bar1"):
v_1 = tf.get_variable("v", [1])
x_1 = 1.0 + v_1
assert v_1.name == "foo1/v:0"
assert x_1.op.name == "foo1/bar1/add"
print v1==v2 # False
print v3==v4 # False 不同变量作用域中
print v3.name # abc/v:0
print v4==v5 # 输出为True
print v5==v6 # True
tensorflow中共享变量 tf.get_variable 和命名空间 tf.variable_scope的更多相关文章
- TensorFlow中的L2正则化函数:tf.nn.l2_loss()与tf.contrib.layers.l2_regularizerd()的用法与异同
tf.nn.l2_loss()与tf.contrib.layers.l2_regularizerd()都是TensorFlow中的L2正则化函数,tf.contrib.layers.l2_regula ...
- TensorFlow中的变量命名以及命名空间.
What: 在Tensorflow中, 为了区别不同的变量(例如TensorBoard显示中), 会需要命名空间对不同的变量进行命名. 其中常用的两个函数为: tf.variable_scope, t ...
- 【tf.keras】tf.keras使用tensorflow中定义的optimizer
Update:2019/09/21 使用 tf.keras 时,请使用 tf.keras.optimizers 里面的优化器,不要使用 tf.train 里面的优化器,不然学习率衰减会出现问题. 使用 ...
- Tensorflow中的name_scope和variable_scope
Tensorflow是一个编程模型,几乎成为了一种编程语言(里面有变量.有操作......). Tensorflow编程分为两个阶段:构图阶段+运行时. Tensorflow构图阶段其实就是在对图进行 ...
- 对tensorflow 中的attention encoder-decoder模型调试分析
#-*-coding:utf8-*- __author = "buyizhiyou" __date = "2017-11-21" import random, ...
- 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中get_variable共享变量调用
import tensorflow as tf with tf.variable_scope('v_scope',reuse=True) as scope1: Weights1 = tf.get_va ...
- TF之RNN:TF的RNN中的常用的两种定义scope的方式get_variable和Variable—Jason niu
# tensorflow中的两种定义scope(命名变量)的方式tf.get_variable和tf.Variable.Tensorflow当中有两种途径生成变量 variable import te ...
- tensorflow中 tf.add_to_collection、 tf.get_collection 和 tf.add_n函数
tf.add_to_collection(name, value) 用来把一个value放入名称是'name'的集合,组成一个列表; tf.get_collection(key, scope=Non ...
随机推荐
- 命令行下开启与关闭windows防火墙关端口(转)
sc config sharedaccess start= auto //设置防火墙服务为自动 net start sharedaccess //开启防火墙服务 关闭端口 netsh firewall ...
- centos 相关
运行locate httpd.conf,提示-bash: locate: command not found错误.则需要安装mlocate软件包: yum install mlocate 搜索,提示l ...
- MapReduce: map读取文件的过程
我们的输入文件 hello0, 内容如下: xiaowang 28 shanghai@_@zhangsan 38 beijing@_@someone 100 unknown 逻辑上有3条记录, 它们以 ...
- 根据源Excel文件,新建Excel文件
/** * 描述:根据源Excel文件,创建新的Excel文件 * @param excelFile * @throws CheckException */public static void cre ...
- 蜻蜓FM下载文件名还原
从蜻蜓FM手机版可以下载音频文件,目的是可以使用普通的播放器进行音频的播放(只是缓存,还用蜻蜓fm播放的请路过),但问题来了,下载下来的音频文件不是在界面中我们看到的文件名称了.于是,我们要进行一项非 ...
- ELF文件分析
(1)文件ELF.c (2)编译: gcc -c ELF_1.c -o ELF_1.o (3)显示文件类型: (4)查看大小: (5)转换为16进制 (6)显示各段信息 (7)分析
- 生成二维码Base64图片
这个写了,但是自己没有用,发现浏览器有的不兼容 代码: string str = System.Configuration.ConfigurationManager.AppSettings[" ...
- noip 2018 D1T3 赛道修建
noip 2018 D1T3 赛道修建 首先考虑二分答案,这时需要的就是对于一个长度求出能在树中选出来的最多的路径条数.考虑到一条路径是由一条向上的路径与一条向下的路径构成,或者仅仅是向上或向下的路径 ...
- 【p4tutorials】P4 v1.1 Simple Router
fork了p4tutorials,想从里面窥探一些门道. 本文相关的原文链接:ReadMe 说明: 下面的这个P4程序,是当下最著名的 simple_router 程序的一个版本,是根据P4的1.1版 ...
- ggplot2作图详解7(完):主题(theme)设置
凡是和数据无关的图形设置内容理论上都可以归为主题类,但考虑到一些内容(如坐标轴)的特殊性,可以允许例外的情况.主题的设置相当繁琐,很容易就占用了 大量的作图时间,应尽量把这些东西简化,把注意力主要放在 ...