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中的变量命名以及命名空间.的更多相关文章

  1. 83、Tensorflow中的变量管理

    ''' Created on Apr 21, 2017 @author: P0079482 ''' #如何通过tf.variable_scope函数来控制tf.ger_variable函数获取已经创建 ...

  2. TensorFlow中的变量和常量

    1.TensorFlow中的变量和常量介绍 TensorFlow中的变量: import tensorflow as tf state = tf.Variable(0,name='counter') ...

  3. 2、Tensorflow中的变量

    2.Tensorflow中的变量注意:tf中使用 变量必须先初始化下面是一个使用变量的TF代码(含注释): # __author__ = "WSX" import tensorfl ...

  4. tensorflow中共享变量 tf.get_variable 和命名空间 tf.variable_scope

    tensorflow中有很多需要变量共享的场合,比如在多个GPU上训练网络时网络参数和训练数据就需要共享. tf通过 tf.get_variable() 可以建立或者获取一个共享的变量. tf.get ...

  5. JavaScript 中的变量命名方法

    三种命名方法 在程序语言中,通常使用的变量命名方法有三种:骆驼命名法(CamelCase),帕斯卡命名法(PascalCase)和匈牙利命名法. 依靠单词的大小写拼写复合词的做法,叫做"骆驼 ...

  6. 深度学习原理与框架-Tensorflow基本操作-Tensorflow中的变量

    1.tf.Variable([[1, 2]])  # 创建一个变量 参数说明:[[1, 2]] 表示输入的数据,为一行二列的数据 2.tf.global_variables_initializer() ...

  7. Tensorflow中的变量

    从初识tf开始,变量这个名词就一直都很重要,因为深度模型往往所要获得的就是通过参数和函数对某一或某些具体事物的抽象表达.而那些未知的数据需要通过学习而获得,在学习的过程中它们不断变化着,最终收敛达到较 ...

  8. tensorflow中使用变量作用域及tf.variable(),tf,getvariable()与tf.variable_scope()的用法

    一 .tf.variable() 在模型中每次调用都会重建变量,使其存储相同变量而消耗内存,如: def repeat_value(): weight=tf.variable(tf.random_no ...

  9. tensorflow中常量(constant)、变量(Variable)、占位符(placeholder)和张量类型转换reshape()

    常量 constant tf.constant()函数定义: def constant(value, dtype=None, shape=None, name="Const", v ...

随机推荐

  1. ps软件使用的问题解决记录

    1.PS的字体颜色改变不了的解决方法,添加字体的时颜色无论怎么选都只能有[黑.白.灰]三种颜色,     问题的原因:图像的模式选择了灰度(G)     解决方法:图像-->模式-->RG ...

  2. Android Google Maps API 网络服务用于网络定位、计算路线、获取经纬度、获取详细地址等

    extends:http://blog.csdn.net/h7870181/article/details/12505883 Google Maps API 网络服务 官网地址 : https://d ...

  3. iOS - 引用计数探讨

    <Objective-C 高级编程> 这本书有三个章节,我针对每一章节进行总结并加上适当的扩展分享给大家.可以从下面这张图来看一下这三篇的整体结构: 注意,这个结构并不和书中的结构一致,而 ...

  4. vue--循环列表

    <template> <div id="app"> <p v-for="x in list">{{x}}</p> ...

  5. css 多行文字,超出部分隐藏,...代替

    css虽然简单,但其实也是记得常用的那些,不常用的还是要搜一搜再写

  6. Redis+Keepalived实现高可用

    使用redis哨兵可以在主服务器出现故障的时候自动切换主从,但是从服务器的IP不同于原主服务器的IP还需要在客户端手动修改IP才能生效 下面使用keepalived实现VIP自动漂移 keepaliv ...

  7. HDU-1011 Starship Troopers(树形dp)

    Starship Troopers Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...

  8. Oracle体系结构之参数文件管理

    参数文件作用:主要用来记录数据库配置信息,数据库在启动时,需要读取参数文件中关于控制文件的信息,分配内存,打开进程,会话等.数据库启动时第一个读取参数文件. 参数文件分类: 1)pfile:文本文件, ...

  9. stress test - volume test

    D:\wamp64\bin\mysql\mysql5.7.11\bin>mysqlslap --delimiter=";" --query=" INSERT I N ...

  10. 6.2.3 Property Access Errors

    JavaScript: The Definitive Guide, Sixth Edition by David Flanagan   Property access expressions do n ...