TensorFlow中的变量命名以及命名空间.
What:
在Tensorflow中, 为了区别不同的变量(例如TensorBoard显示中), 会需要命名空间对不同的变量进行命名. 其中常用的两个函数为: tf.variable_scope, tf.name_scope.
Why:
在自己的编写代码过程中, 用如下代码进行变量生成并进行卷积操作:
import tensorflow as tf
import numpy as np def my_conv2d(data, name, kh, kw, sh, sw, n_out):
n_in = np.shape(data)[-1]
with tf.name_scope(name):
kernel = tf.get_variable(name="W", shape=[kh, kw, n_in, n_out], dtype=tf.float32, initializer=tf.contrib.layers.xavier_initializer())
bias = tf.Variable(tf.constant(0.0, shape=[n_out], dtype=tf.float32), name="b")
conv = tf.nn.conv2d(data, kernel, stride=[1, sh, sw, 1], padding='SAME', name="Conv")
result = tf.nn.relu(tf.nn.bias_add(conv, bias), name="Act")
return result.
运行时会报错:
ValueError: Variable bar already exists, disallowed. Did you mean to set reuse=True in VarScope? ...
How:
中国工信出版社的<TensorFlow 实战Google深度学习学习框架>P231, 对tf.name_scope和tf.variable_scope做了一个详细的解释, 转载在下面:
这两个函数在大部分情况下是等价的, 唯一的区别是在使用tf.get_variable函数时.
import tensorflow as tf with tf.variable_scope("foo"):
a = tf.get_variable("bar", [1])
print a.name # 输出 foo/bar: 0 with tf.variable_scope("bar"):
b = tf.get_variable("bar", [1])
print b.name # 输出 bar/bar: 0 with tf.name_scope("a"):
a = tf.Variable([1])
print a.name # 输出 a/Variable: 0 a = tf.Variable("b", [1]):
print a.name # 输出 b: 0 with tf.name_scope("b"):
tf.get_variable("b", [1]) # Error
从上面的输出看出. 如果在使用tf.get_variable()生成变量, 这时的命名是不受tf.name_scope的影响, 而会收到tf.variable_scope的影响, 因此对于我自己的情况, 问题在于my_conv2d在多次调用时, 变量命名重复, 由此可见修改方案可以考虑改为用tf.variable_scope. 我在下面贴出另一种解决方案(每次调用都给予不同的变量名):
def my_conv2d(data, name, kh, kw, sh, sw, n_out):
n_in = np.shape(data)[-1]
with tf.name_scope(name) as scope:
kernel = tf.get_variable(name=scope+"W", shape=[kh, kw, n_in, n_out], dtype=tf.float32, initializer=tf.contrib.layers.xavier_initializer())
bias = tf.Variable(tf.constant(0.0, shape=[n_out], dtype=tf.float32), name=scope+"b")
conv = tf.nn.conv2d(data, kernel, stride=[1, sh, sw, 1], padding='SAME', name=scope+"Conv")
result = tf.nn.relu(tf.nn.bias_add(conv, bias), name=Scope+"Act")
return result.
TensorFlow中的变量命名以及命名空间.的更多相关文章
- 83、Tensorflow中的变量管理
''' Created on Apr 21, 2017 @author: P0079482 ''' #如何通过tf.variable_scope函数来控制tf.ger_variable函数获取已经创建 ...
- TensorFlow中的变量和常量
1.TensorFlow中的变量和常量介绍 TensorFlow中的变量: import tensorflow as tf state = tf.Variable(0,name='counter') ...
- 2、Tensorflow中的变量
2.Tensorflow中的变量注意:tf中使用 变量必须先初始化下面是一个使用变量的TF代码(含注释): # __author__ = "WSX" import tensorfl ...
- tensorflow中共享变量 tf.get_variable 和命名空间 tf.variable_scope
tensorflow中有很多需要变量共享的场合,比如在多个GPU上训练网络时网络参数和训练数据就需要共享. tf通过 tf.get_variable() 可以建立或者获取一个共享的变量. tf.get ...
- JavaScript 中的变量命名方法
三种命名方法 在程序语言中,通常使用的变量命名方法有三种:骆驼命名法(CamelCase),帕斯卡命名法(PascalCase)和匈牙利命名法. 依靠单词的大小写拼写复合词的做法,叫做"骆驼 ...
- 深度学习原理与框架-Tensorflow基本操作-Tensorflow中的变量
1.tf.Variable([[1, 2]]) # 创建一个变量 参数说明:[[1, 2]] 表示输入的数据,为一行二列的数据 2.tf.global_variables_initializer() ...
- Tensorflow中的变量
从初识tf开始,变量这个名词就一直都很重要,因为深度模型往往所要获得的就是通过参数和函数对某一或某些具体事物的抽象表达.而那些未知的数据需要通过学习而获得,在学习的过程中它们不断变化着,最终收敛达到较 ...
- tensorflow中使用变量作用域及tf.variable(),tf,getvariable()与tf.variable_scope()的用法
一 .tf.variable() 在模型中每次调用都会重建变量,使其存储相同变量而消耗内存,如: def repeat_value(): weight=tf.variable(tf.random_no ...
- tensorflow中常量(constant)、变量(Variable)、占位符(placeholder)和张量类型转换reshape()
常量 constant tf.constant()函数定义: def constant(value, dtype=None, shape=None, name="Const", v ...
随机推荐
- 调用office COM出现不会退出的问题
症状 在使用.net调用 Microsoft Office 应用程序时,Office 应用程序在调用Quit方法时不会退出. 原因 Visual Studio.NET 从托管代码调用 COM 对象时, ...
- 【CF662A】Gambling Nim 线性基
[CF662A]Gambling Nim 题意:n长卡牌,第i张卡牌正面的数字是$a_i$,反面的数字是$b_i$,每张卡牌等概率为正面朝上或反面朝上.现在Alice和Bob要用每张卡牌朝上的数字玩N ...
- linux shell中curl 发送post请求json格式问题
今天在linux中使用curl发送一个post请求时,带有json的数据,在发送时发现json中的变量没有解析出来 如下 curl -i -X POST -H 'Content-type':'appl ...
- Android 获取View中的组件
可以把这个view强转成ViewGroup对象,再通过getChildAt(0),getChildAt(1) 获取之后AddView可能会报错:IllegalStateException: The s ...
- Python 装饰器使用指南
装饰器是可调用的对象,其参数是另一个函数(被装饰的函数). 1 装饰器基础知识 首先看一下这段代码 def deco(fn): print "I am %s!" % fn.__na ...
- Java除法和js
java 除 向下取整 js 保留小数
- Python之时间模块
1,怎么打印时间戳 2,怎么打印日期 3,怎么把字符串转换成python认识的日期 把日期转换成字符串 字符串转换成日期格式 time.strptime("2017-5-16",& ...
- java基础解析系列(一)---String、StringBuffer、StringBuilder
java基础解析系列(一)---String.StringBuffer.StringBuilder 前言:本系列的主题是平时容易疏忽的知识点,只有基础扎实,在编码的时候才能更注重规范和性能,在出现bu ...
- 《机器学习实战》2.2.2分析数据:使用matplotlib创建散点图
#输出散点图 def f(): datingDataMat,datingLabels = file2matrix("datingTestSet3.txt") fig = plt.f ...
- NEFU 117 - 素数个数的位数 - [简单数学题]
题目链接:http://acm.nefu.edu.cn/JudgeOnline/problemShow.php?problem_id=117 Time Limit:1000ms Memory Limi ...