转载http://blog.csdn.net/jerr__y/article/details/60877873

1. 首先看看比较简单的 tf.name_scope(‘scope_name’).

tf.name_scope 主要结合 tf.Variable() 来使用,方便参数命名管理。

'''
Signature: tf.name_scope(*args, **kwds)
Docstring:
Returns a context manager for use when defining a Python op.
'''
# 也就是说,它的主要目的是为了更加方便地管理参数命名。
# 与 tf.Variable() 结合使用。简化了命名
with tf.name_scope('conv1') as scope: weights1 = tf.Variable([1.0, 2.0], name='weights') bias1 = tf.Variable([0.3], name='bias') # 下面是在另外一个命名空间来定义变量的 with tf.name_scope('conv2') as scope: weights2 = tf.Variable([4.0, 2.0], name='weights') bias2 = tf.Variable([0.33], name='bias') # 所以,实际上weights1 和 weights2 这两个引用名指向了不同的空间,不会冲突 print weights1.name print weights2.name
conv1/weights:0
conv2/weights:0
# 注意,这里的 with 和 python 中其他的 with 是不一样的
# 执行完 with 里边的语句之后,这个 conv1/ 和 conv2/ 空间还是在内存中的。这时候如果再次执行上面的代码
# 就会再生成其他命名空间
with tf.name_scope('conv1') as scope: weights1 = tf.Variable([1.0, 2.0], name='weights') bias1 = tf.Variable([0.3], name='bias') with tf.name_scope('conv2') as scope: weights2 = tf.Variable([4.0, 2.0], name='weights') bias2 = tf.Variable([0.33], name='bias') print weights1.name print weights2.name
conv1_1/weights:0
conv2_1/weights:0
import tensorflow as tf

2.下面来看看 tf.variable_scope(‘scope_name’)

tf.variable_scope() 主要结合 tf.get_variable() 来使用,实现 变量共享。

# 这里是正确的打开方式~~~可以看出,name 参数才是对象的唯一标识
import tensorflow as tf
with tf.variable_scope('v_scope') as scope1: Weights1 = tf.get_variable('Weights', shape=[2,3]) bias1 = tf.get_variable('bias', shape=[3]) # 下面来共享上面已经定义好的变量 # note: 在下面的 scope 中的变量必须已经定义过了,才能设置 reuse=True,否则会报错 with tf.variable_scope('v_scope', reuse=True) as scope2: Weights2 = tf.get_variable('Weights') print Weights1.name print Weights2.name # 可以看到这两个引用名称指向的是同一个内存对象
v_scope/Weights:0
v_scope/Weights:0

也可以结合 tf.Variable() 一块使用。

import tensorflow as tf
# 注意, bias1 的定义方式
with tf.variable_scope('v_scope') as scope1: Weights1 = tf.get_variable('Weights', shape=[2,3]) # bias1 = tf.Variable([0.52], name='bias') # 下面来共享上面已经定义好的变量 # note: 在下面的 scope 中的get_variable()变量必须已经定义过了,才能设置 reuse=True,否则会报错 with tf.variable_scope('v_scope', reuse=True) as scope2: Weights2 = tf.get_variable('Weights') bias2 = tf.Variable([0.52], name='bias') print Weights1.name print Weights2.name print bias2.name
v_scope/Weights:0
v_scope/Weights:0
v_scope_1/bias:0

如果 reuse=True 的scope中的变量没有已经定义,会报错!!

import tensorflow as tf
# 注意, bias1 的定义方式
with tf.variable_scope('v_scope') as scope1: Weights1 = tf.get_variable('Weights', shape=[2,3]) bias1 = tf.Variable([0.52], name='bias') print Weights1.name print bias1.name # 下面来共享上面已经定义好的变量 # note: 在下面的 scope 中的get_variable()变量必须已经定义过了,才能设置 reuse=True,否则会报错 with tf.variable_scope('v_scope', reuse=True) as scope2: Weights2 = tf.get_variable('Weights') bias2 = tf.get_variable('bias', [1]) # ‘bias print Weights2.name print bias2.name # 这样子的话就会报错 # Variable v_scope/bias does not exist, or was not created with tf.get_variable()
v_scope/Weights:0
v_scope/bias:0

本文代码:https://github.com/yongyehuang/Tensorflow-Tutorial

TensorFlow基础笔记(13) tf.name_scope tf.variable_scope学习相关推荐

  1. TensorFlow基础篇(六)——tf.nn.max_pool()和tf.nn.avg_pool()

    tf.nn.max_pool()和tf.nn.avg_pool()是TensorFlow中实现最大池化和平均池化的函数,在卷积神经网络中比较核心的方法. 有些和卷积很相似,可以参考TensorFlow ...

  2. tf.name_scope tf.variable_scope学习

    1. 首先看看比较简单的 tf.name_scope('scope_name'). tf.name_scope 主要结合 tf.Variable() 来使用,方便参数命名管理. ''' Signatu ...

  3. TensorFlow基础篇(二)——tf.get_variable()和tf.get_variable_scope()

    1.tf.get_variable() tf.get_variable()用来创建变量时,和tf.Variable()函数的功能基本等价. v = tf.get_variable("v&qu ...

  4. 【Tensorflow教程笔记】常用模块 tf.function :图执行模式

    基础 TensorFlow 基础 TensorFlow 模型建立与训练 基础示例:多层感知机(MLP) 卷积神经网络(CNN) 循环神经网络(RNN) 深度强化学习(DRL) Keras Pipeli ...

  5. 【Tensorflow教程笔记】常用模块 tf.train.Checkpoint :变量的保存与恢复

    基础 TensorFlow 基础 TensorFlow 模型建立与训练 基础示例:多层感知机(MLP) 卷积神经网络(CNN) 循环神经网络(RNN) 深度强化学习(DRL) Keras Pipeli ...

  6. TensorFlow基础篇(七)——tf.nn.conv2d()

    tf.nn.conv2d是TensorFlow里面实现卷积的函数,是搭建卷积神经网络比较核心的一个方法. 函数格式: tf.nn.conv2d(input, filter, strides, padd ...

  7. TensorFlow基础笔记(6) 图像风格化实验

    参考 http://blog.csdn.net/wspba/article/details/53994649 https://www.ctolib.com/AdaIN-style.html Ackno ...

  8. TensorFlow基础笔记(11) max_pool2D函数 深度学习

    # def max_pool2d(inputs, # kernel_size, # stride=2, # padding='VALID', # data_format=DATA_FORMAT_NHW ...

  9. TensorFlow基础篇(八)——tf.contrib.layers.l1regularizer()-12_regularizer(lambda)

    TensorFlow中计算L1正则化和L2正则化的函数: L1正则化:tf.contrib.layers.l1regularizer(lambda)(w),它可以返回一个函数,这个函数可以计算一个给定 ...

最新文章

  1. Android深度探索(卷1)HAL与驱动开发第五章总结
  2. 百度一口气亮出NLP十年积累:完整技术布局全面披露,面向业界砸下11项七夕大礼...
  3. linux socket 阻塞与非阻塞,同步与异步
  4. redis windows
  5. qsort()与sort的用法(收藏)
  6. 让 Hangfire 使用 MongoDB 存储
  7. YbtOJ#883-最大的割【带修线性基】
  8. python中argument函数_PythonStudy——函数的参数 Function argument
  9. 详解:设计模式之-适配器模式
  10. linux c语言变量地址类型,C语言基础知识:访问内存地址的方法
  11. windows server搭建php mysql数据库_windows server 2008/2012安装php iis7 mysql环境搭建教程...
  12. NHibernate Configuring
  13. readelf ELF 文件格式分析
  14. C++ 笔记 加号运算符重载
  15. 为什么php-fpm会使用内存一直增加_百度输入法重大BUG:内存占用随用户词库增大而暴涨...
  16. PPT调整同一行字符间距的三种常用方法
  17. NOI的1.8.20反反复复
  18. 使用Android自带的DownloadManager下载ApK并安装
  19. WTF Solidity极简入门: 35. 荷兰拍卖
  20. 【第175期】游戏策划的跳槽:做决定前,建议你要知道的两个事

热门文章

  1. 树形结构递归_递归和匿名函数
  2. autocad型源代码_C# 实现预览dwg文件完整源代码(无需autocad环境)
  3. html一个页面多个动画,如何在单个html页面中添加两个相同的adobe边缘动画?
  4. wpf tabitem 点击事件_Mindfusion教程:WPF中的Fishbone(Ishikawa)图
  5. python exe enter退出,Python程序退出处理程序(atexit)
  6. openjudge用c语言答案,OpenJudge - NOI - 1.4编程基础之逻辑表达式与条件分支(C语言 全部题解)...
  7. Jstatd命令 Java Statistics Monitoring Daemon
  8. 报错,ERROR 2572 --- [MessageBroker-2] o.h.engine.jdbc.spi.SqlExceptionHelper : Column ‘create_by‘
  9. 无心剑中译狄金森诗36首
  10. Python案例:四种方法判断回文字符串