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

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

转载https://www.cnblogs.com/adong7639/p/8136273.html

转载于:https://www.cnblogs.com/gaofighting/p/9626584.html

tf.name_scope tf.variable_scope学习相关推荐

  1. TensorFlow基础笔记(13) tf.name_scope tf.variable_scope学习

    转载http://blog.csdn.net/jerr__y/article/details/60877873 1. 首先看看比较简单的 tf.name_scope('scope_name'). tf ...

  2. tf.Variable,tf.get_variable,tf.variable_scope,tf.name_scope区别分析

    1.tf.Variable与tf.get_variable ①在创建变量的时候,两者是等价的 ②tf.Variable的变量名是一个可选项,而tf.get_variable必须要有变量名 import ...

  3. tf.Variable、tf.get_variable、tf.variable_scope以及tf.name_scope

    tf.Variable与tf.get_variable tensorflow提供了通过变量名称来创建或者获取一个变量的机制.通过这个机制,在不同的函数中可以直接通过变量的名字来使用变量,而不需要将变量 ...

  4. TensorFlow 学习(一)—— tf.get_variable() vs tf.Variable(),tf.name_scope() vs tf.variable_scope()

    scope 命名方法 对于一个复杂的 tensorflow 模型会有很多个变量, tf.variable_scope() :提供了简单的命名空间技术以避免冲突: tf.get_variable():从 ...

  5. tf.variable和tf.get_Variable以及tf.name_scope和tf.variable_scope的区别

    在训练深度网络时,为了减少需要训练参数的个数(比如具有simase结构的LSTM模型).或是多机多卡并行化训练大数据大模型(比如数据并行化)等情况时,往往需要共享变量.另外一方面是当一个深度学习模型变 ...

  6. tf.Variable、tf.get_variable、tf.variable_scope、tf.name_scope、random、initializer

    转自:tensorflow学习笔记(二十三):variable与get_variable TF.VARIABLE.TF.GET_VARIABLE.TF.VARIABLE_SCOPE以及TF.NAME_ ...

  7. 通俗理解tf.name_scope()、tf.variable_scope()

    前言:最近做一个实验,遇到TensorFlow变量作用域问题,对tf.name_scope().tf.variable_scope()等进行了较为深刻的比较,记录相关笔记: tf.name_scope ...

  8. tf.name_scope() 和 tf.variable_scope() 的用法和玄机

    2019独角兽企业重金招聘Python工程师标准>>> 一. name_scope 和 variable_scope的用途 用途1: 共享变量 TensorFlow (TF) 中,n ...

  9. tf.name_scope()与tf.variable_scope()

    TensorFlow的tf.name_scope().tf.variable_scope()是两个作用域函数,一般与两个创建/调用变量的函数tf.variable() 和tf.get_variable ...

最新文章

  1. Caliburn.Micro 资源随时添加
  2. rsyslog+analyzer
  3. 當前主流防拷光碟的備份
  4. wse2.0实现webservice安全(转)
  5. C#中将list使用ProtoBuf进行序列化并使用SharpZipLib进行压缩
  6. 复习javascript中call,apply,bind的用法
  7. wxWidgets:wxMenu/wxMenuBar 示例
  8. 2014\Province_C_C++_B\3 李白打酒
  9. 关于优酷开放SDK中setOnRealVideoStartListener
  10. phpstorm PHP language level无法选择
  11. ogc是一个非营利性组织_我们的非营利组织如何公开运作以使教育变得容易
  12. oracle中imp命令具体解释
  13. JDBC数据库编程:callableStatement接口
  14. BZOJ2241 [SDOI2011]打地鼠 【模拟】
  15. 无锁循环缓冲区的实现c语言,C++ 无锁环形缓冲区实现
  16. 三年程序员成功转型项目经理
  17. c语言中calc用法,CSS中calc()函数怎么使用
  18. 定时关机win10_Windows利用任务计划程序实现定时关[日常]
  19. 穿越火线老是卡在正在连接服务器,修复cf经常提示网络出现异常与服务器断开连接的方法...
  20. 线性方程组求解——基于MTALAB/Octave,Numpy,Sympy和Maxima

热门文章

  1. Hi3518编译器安装
  2. 在C语言中巧用正则表达式
  3. [react] create-react-app创建新运用怎么解决卡的问题?
  4. React开发(124):ant design学习指南之form中的属性isFieldTouched
  5. 前端学习(3222):函数式组件使用props
  6. 前端学习(2751):uni-app目录设置
  7. 工作165:混入调用的时候
  8. 工作128:element上传组件时候的钩子--event里面有数据参数
  9. 前端学习(2528):一个简单的vue app
  10. “约见”面试官系列之常见面试题之第六十七篇之jsonp原理和实现(建议收藏)