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 ...
随机推荐
- ThreadLocal的实现和使用场景
ThreadLocal 内部实现.应用场景和内存泄漏 深入理解线程局部变量:ThreadLocal <Java源码分析>:ThreadLocal /ThreadLocalMap Threa ...
- JS如何遍历Object中的所有属性?
JS如何遍历Object中的所有属性? var params = ""; for(var i in baseParams){ params += "&" ...
- GPU对数据的操作不可累加
我想当然的认为GPU处理数据时可以共同访问内存,所以对数据的操作是累加的. 事实证明:虽然GPU多个核可以访问同一块内存,但彼此之间没有依赖关系,它们对这块内存的作用无法累加. 先看代码: #incl ...
- vue--环境搭建(创建运行项目)
如何搭建vue环境: 1.安装之前必须要安装 node.js 2.搭建Vue环境,安装vue的脚手架工具 npm install --global vue-cli / cnpm install --g ...
- 基于pandas python sklearn 的美团某商家的评论分类(文本分类)
美团店铺评价语言处理以及分类(NLP) 第一篇 数据分析部分 第二篇 可视化部分, 本文是该系列第三篇,文本分类 主要用到的包有jieba,sklearn,pandas,本篇博文主要先用的是词袋模型( ...
- tomcat启动报错“Error: Exception thrown by the agent : java.net.MalformedURLException: Local host name unknown: java.net.UnknownHostException: iZ25fsk1ifk: iZ25fsk1ifk”
在启动了Tomcat的时候出现下面的错误,导致启动不了,卡在读日志的状态 Error: Exception thrown by the agent : java.net.MalformedURLExc ...
- SHU 第15届上海大学程序设计联赛夏季赛[热身赛] 第三题(G题) - 英语成绩
看完题目就觉得是个图论题…… 每个人的成绩就是vertice,两个人的分数差就是edge,那么肯定类似于一种relax的方式,不断将每个人的成绩的min往上提, 当然,单纯的遍历一遍G.E肯定不可能就 ...
- A Simple Chess---hdu5794(容斥+Lucas)
题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=5794 题意:给你一个n*m的网格,问从(1, 1)走到(n, m)的方案数是多少,其中有r ...
- SpringMVC 问题 org.springframework.beans.factory.BeanDefinitionStoreException
HTTP Status 500 – Internal Server Error Type Exception Report Message Servlet.init() for servlet [sp ...
- eclipse copy web project后修改context root
1.项目-->右键-->properties