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. Unity3D笔记 GUI 一

    要实现的功能: 1.个性化Windows界面 2.减少个性化的背景图片尺寸 3.个性化样式ExitButton和TabButton 4.实现三个选项卡窗口 一.个性化Windows界面 1.1.创建一 ...

  2. iOS SwiftMonkey 随机暴力测试

    参考源文章 https://github.com/zalando/SwiftMonkey https://kemchenj.github.io/2017/03/16/2017-03-16/ 简介 这个 ...

  3. opencv学习之PyImageSearch

    Practical Python and OpenCV+ Case Studies 是一本opencv的入门书籍 强烈推荐一个老外的网站,pyimagesearch 网址:https://www.py ...

  4. SPOJ IM - Intergalactic Map - [拆点最大流]

    题目链接:http://www.spoj.com/problems/IM/en/ Time limit:491 ms Memory limit:1572864 kB Code length Limit ...

  5. 浅谈jvm中的垃圾回收策略

    下面小编就为大家带来一篇浅谈jvm中的垃圾回收策略.小编觉得挺不错的,现在就分享给大家,也给大家做个参考.一起跟随小编过来看看吧   java和C#中的内存的分配和释放都是由虚拟机自动管理的,此前我已 ...

  6. iOS钱包卡券开发(往钱包里面加自己的卡券)

    参考文章 https://blog.csdn.net/sz_vcp2007/article/details/60762349 https://blog.csdn.net/eqera/article/d ...

  7. 前端开发组件化设计vue,react,angular原则漫谈

    前端开发组件化设计vue,react,angular原则漫谈 https://www.toutiao.com/a6346443500179505410/?tt_from=weixin&utm_ ...

  8. spring boot继承web和mybatis时,调用接口删除记录出现的空指针以及解决办法

    前两天在学spring boot的时候,出现了一个很奇怪的错误,因为是第一次使用spring boot,所以没想到会遇到这种莫名其妙的bug,即调用接口删除数据库中一条记录的时候,数据库中记录事实上以 ...

  9. jquery报错:“ReferenceError: jQuery is not defined”

    这明显是没有引到jquery,原因就是jquery没有放在最前面,jquery应该最先引入.

  10. 【JS】自学

    JS自学网址: http://www.runoob.com/js/js-tutorial.html