1.tf.variable_scope

功能:tf.variable_scope可以让不同命名空间中的变量取相同的名字,无论tf.get_variable或者tf.Variable生成的变量

TensorFlow链接:https://tensorflow.google.cn/api_docs/python/tf/variable_scope?hl=en

举例:

with tf.variable_scope('V1'):
a1 = tf.get_variable(name='a1', shape=[1], initializer=tf.constant_initializer(1))
a2 = tf.Variable(tf.random_normal(shape=[2, 3], mean=0, stddev=1), name='a2')
with tf.variable_scope('V2'):
a3 = tf.get_variable(name='a1', shape=[1], initializer=tf.constant_initializer(1))
a4 = tf.Variable(tf.random_normal(shape=[2, 3], mean=0, stddev=1), name='a2') with tf.Session() as sess:
sess.run(tf.initialize_all_variables())
print(a1.name)
print(a2.name)
print(a3.name)
print(a4.name)

with tf.variable_scope("foo"):
v = tf.get_variable("v", [1])
with tf.variable_scope("foo", reuse=True):
v1 = tf.get_variable("v", [1])
assert v1 == v #不报错

如果想要重用变量,可以设置reuse_variables()

import numpy as np
with tf.variable_scope('V1'):
a1 = tf.get_variable(name='a1', shape=[1], initializer=tf.constant_initializer(1))
a2 = tf.Variable(tf.random_normal(shape=[2, 3], mean=0, stddev=1), name='a2')
tf.get_variable_scope().reuse_variables()
assert tf.get_variable_scope().reuse == True
a3 = tf.get_variable(name='a1', shape=[1], initializer=tf.constant_initializer(1))
a4 = tf.Variable(tf.random_normal(shape=[2, 3], mean=0, stddev=1), name='a2') with tf.variable_scope('V1',reuse=True):
a5 = tf.get_variable(name='a1', shape=[1], initializer=tf.constant_initializer(1)) with tf.Session() as sess:
sess.run(tf.initialize_all_variables())
print(a1.name)
print(a2.name)
print(a3.name)
print(a4.name)
print(a5.name)

variable重名,虽然name设置的一样,但是实际是不共享同一个变量的;get_variable重name,其实是共享的同一个变量。

2.tf.name_scope

功能:tf.name_scope具有类似的功能,但只限于tf.Variable生成的变量

TensorFlow链接:https://tensorflow.google.cn/api_docs/python/tf/name_scope?hl=en

with tf.name_scope('V1'):
a1 = tf.get_variable(name='a1', shape=[1], initializer=tf.constant_initializer(1))
a2 = tf.Variable(tf.random_normal(shape=[2, 3], mean=0, stddev=1), name='a2')
with tf.name_scope('V2'):
a3 = tf.get_variable(name='a1', shape=[1], initializer=tf.constant_initializer(1))
a4 = tf.Variable(tf.random_normal(shape=[2, 3], mean=0, stddev=1), name='a2') with tf.Session() as sess:
sess.run(tf.initialize_all_variables())
print(a1.name)
print(a2.name)
print(a3.name)
print(a4.name)

a1,a3会报错:ValueError: Variable a1 already exists, disallowed. Did you mean to set reuse=True or reuse=tf.AUTO_REUSE in VarScope? Originally defined at:

 

参考文献:

【1】tf.variable_scope和tf.name_scope的用法

【2】参数共享:https://jasdeep06.github.io/posts/variable-sharing-in-tensorflow/

tf.variable_scope()和tf.name_scope()的更多相关文章

  1. TF.VARIABLE、TF.GET_VARIABLE、TF.VARIABLE_SCOPE以及TF.NAME_SCOPE关系

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

  2. 理解 tf.Variable、tf.get_variable以及范围命名方法tf.variable_scope、tf.name_scope

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

  3. 彻底弄懂tf.Variable、tf.get_variable、tf.variable_scope以及tf.name_scope异同

    https://blog.csdn.net/qq_22522663/article/details/78729029 1. tf.Variable与tf.get_variabletensorflow提 ...

  4. TensorFlow函数(三)tf.variable_scope() 和 tf.name_scope()

    tf.name_scope() 此函数作用是共享变量.在一个作用域scope内共享一些变量,简单来说,就是给变量名前面加个变量空间名,只限于tf.Variable()的变量 tf.variable_s ...

  5. tensorflow中使用tf.variable_scope和tf.get_variable的ValueError

    ValueError: Variable conv1/weights1 already exists, disallowed. Did you mean to set reuse=True in Va ...

  6. tf.name_scope()和tf.variable_scope() (转)

    网络层中变量存在两个问题: 随着层数的增多,导致变量名的增多: 在调用函数的时候,会重复生成变量,但他们存储的都是一样的变量.   tf.variable不能解决这个问题. 变量作用域使用tf.var ...

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

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

  8. tf.name_scope tf.variable_scope学习

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

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

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

随机推荐

  1. Linux引导过程与服务控制

    一:系统引导流程: 开机自检(BIOS)-->MBR引导-->GRUB菜单-->加载内核(kernel)-->init进程初始化  二:系统引导级别: 0 poweroff.t ...

  2. nginx日志说明

    一.日志说明 nginx日志主要有两种:访问日志和错误日志.访问日志主要记录客户端访问nginx的每一个请求,格式可以自定义:错误日志主要记录客户端访问nginx出错时的日志,格 式不支持自定义.两种 ...

  3. Idea用maven给springboot打jar包

    一.准备工作 1.工具:Idea2018,maven3.5 2.首先得保证pom有maven插件 <plugin> <groupId>org.springframework.b ...

  4. python 获取日期以及时间

    >>> import datetime >>> >>> i = datetime.datetime.now() >>> prin ...

  5. oracle--10GRAC集群搭建问题OUI-25031

    一,问题描述 安装RAC的过程中在结束 的阶段出现的错误 02,解决方式 这个可能在root.sh 执行的时候报错 由于版本问题: 修改vim /etc/redhat-release 把6.9改为4. ...

  6. GreenPlum 大数据平台--集群恢复

    一,问题描述 :::: gpinitstandby:greenplum01:gpadmin-[ERROR]:-Cannot use -n option when standby master has ...

  7. mysql修改windows下的data目录

    在windows下,安装完mysql后,建议把数据目录(data目录)移动到非系统目录,避免系统出问题的时候还原. 1.在d盘创建一个mysqldata 2.给新文件增加权限NetworkServic ...

  8. ASP.NET Core基于微软微服务eShopOnContainer事件总线EventBus的实现

    这个EventBus的实现是基于微软微服务https://github.com/dotnet-architecture/eShopOnContainers项目的,我把它从项目中抽离出来,打包成nuge ...

  9. Android studio(AS) svg图片使用

    1.下载svg文件,https://www.iconfont.cn/阿里的2.通过AS - New - Vectro Asset转换成安卓可用的xml文件(ic_back.xml)3.配置Gradle ...

  10. 【须弥SUMERU】分布式安全服务编排实践

    一.概要 1.分布式安全服务编排概念 2.须弥(Sumeru)关键实现思路 3.应用场景 二.前言 在笔者看来,安全防御的本质之一是增加攻击者的攻击成本,尤其是时间成本.那么从防御的角度来说,如何尽早 ...