TensorFlow的tf.name_scope()、tf.variable_scope()是两个作用域函数,一般与两个创建/调用变量的函数tf.variable() 和tf.get_variable()搭配使用。常用于:1)变量共享;2)tensorboard画流程图进行可视化封装变量。

       命名域 (name scope),通过tf.name_scope 或 tf.op_scope创建;
       变量域 (variable scope),通过tf.variable_scope 或 tf.variable_op_scope创建。
       通俗理解就是:tf.name_scope()、tf.variable_scope()会在模型中开辟各自的空间,而其中的变量均在这个空间内进行管理,但是之所以有两个,主要还是有着各自的区别。

(1)tf.Variable() :每次都会新建变量。

(2)tf.get_variable():如果希望重用(共享)一些变量,就需要用到tf.get_variable(),它会去搜索变量名,有就直接用,没有再新建。拥有一个变量检查机制,会检测已经存在的变量是否设置为共享变量,如果已经存在的变量没有设置为共享变量,TensorFlow 运行到第二个拥有相同名字的变量的时候,就会报错。

1. tf.name_scope(‘scope_name’)

tf.name_scope 主要结合 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')  # tf.Variable不会报错print(Weights1.name)
print(bias1.name)'''
v_scope/Weights:0
v_scope/bias:0
'''# 下面来共享上面已经定义好的变量
# 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])  # tf.get_variable会报错print(Weights2.name)
print(bias2.name)# 这样会报错
# Variable v_scope/bias does not exist, or was not created with tf.get_variable()

tf.name_scope()与tf.variable_scope()相关推荐

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

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

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

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

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

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

  4. tf.name_scope与tf.variable_scope用法区别

    转自:https://blog.csdn.net/daizongxue/article/details/84284007 本文由网络上一些回答和博文汇总而成. 要将这个问题解释清楚,得结合tensor ...

  5. tf.Variable() 和 tf.get_variable(),tf.name_scope() 和 tf.variable_scope()

    在gpu并行训练网络时,往往需要共享已创建的变量,tensorflow中为了使这些变量名和操作名唯一并且不重复,用了几个函数来应对这种情况.于是就有了tf.Variable(), tf.get_var ...

  6. tensorflow 变量及命名空间 tf.Variable() vs tf.get_variable() tf.name_scope() vs tf.variable_scope()

    tf.Variable() vs tf.get_variable() tf.name_scope() vs tf.variable_scope() 1. 基础功能 1.1 tf.Variable() ...

  7. tf.name_scope与tf.variable_scope

    1.scope(作用域)   在TensorFlow中有两个作用域 (scope),一个是variable_scope,另一个是name_scope.它们究竟有什么区别呢?简而言之,variable_ ...

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

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

  9. tf.name_scope()和tf.variable_scope()

    https://blog.csdn.net/gqixf/article/details/80191918 https://blog.csdn.net/uestc_c2_403/article/deta ...

最新文章

  1. 企业中如何避免因网卡硬件问题产生的损失
  2. 再谈工作的主动性和有效提问
  3. Docker(一):Docker核心技术预览
  4. 睡不着的时候,我会......
  5. 在 Linux CentOS 上安装 Couchbase Server
  6. 动手学深度深度学习-pycharm中配置mxnet开发环境
  7. jQuery学习笔记1--表格展开关系
  8. ROS2学习(八).ROS概念 - ROS 2接口(Ros2 interface)
  9. powerbi的功能介绍_Power BI:1分钟快速生成可视化报表
  10. Chrome Extension Dark Theme
  11. bagging算法_Bagging/Boosting傻傻分不清?来一探究竟吧~
  12. 怎么把网页源码家入hexo博客_从零开始搭建个人博客(超详细)
  13. 试图在loongarch64上编译JNA失败
  14. Eclipse中修改项目的文本字符集编码
  15. windows 10 应用商店无法下载安装应用的解决
  16. 三角波的傅里叶变换公式_南瓜老师的数学思维训练营 第14期 —— 三角恒等变换公式...
  17. 【毕业设计】基于Stm32的人体心率脉搏无线监测系统 - 单片机 物联网
  18. scala 读取txt文件(从文件读取)
  19. word交叉引用后,移动文章结构,修改引用顺序到符合引用先后
  20. python升级pip_python中pip升级

热门文章

  1. 获取application.yml中的属性的方法
  2. Android --- 调用MediaStore.Images.Media.insertImage保存图片时生成两张图片的问题
  3. 数据结构-深度优先遍历和广度优先遍历(漫画)
  4. 小型云台用的是什么电机_盘一下目前国内主流的电动汽车都用什么电机
  5. 你知道铅酸蓄电池的常见失效模式吗?
  6. 成功解决利用matplotlib.pyplot进行绘图的时候整个画布中的绘制曲线只显示一部分
  7. Python之Pandas:pandas.read_csv()函数的简介、具体案例、使用方法详细攻略
  8. DL之NN/Average_Darkness/SVM:手写数字图片识别(本地数据集50000训练集+数据集加4倍)比较3种算法Average_Darkness、SVM、NN各自的准确率
  9. Python3.x 发送邮件
  10. 决策树-基于不同算法的决策树模型对比