tf.variable_scope()和tf.name_scope()
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()的更多相关文章
- TF.VARIABLE、TF.GET_VARIABLE、TF.VARIABLE_SCOPE以及TF.NAME_SCOPE关系
1. tf.Variable与tf.get_variable tensorflow提供了通过变量名称来创建或者获取一个变量的机制.通过这个机制,在不同的函数中可以直接通过变量的名字来使用变量,而不需要 ...
- 理解 tf.Variable、tf.get_variable以及范围命名方法tf.variable_scope、tf.name_scope
tensorflow提供了通过变量名称来创建或者获取一个变量的机制.通过这个机制,在不同的函数中可以直接通过变量的名字来使用变量,而不需要将变量通过参数的形式到处传递. 1. tf.Variable( ...
- 彻底弄懂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提 ...
- TensorFlow函数(三)tf.variable_scope() 和 tf.name_scope()
tf.name_scope() 此函数作用是共享变量.在一个作用域scope内共享一些变量,简单来说,就是给变量名前面加个变量空间名,只限于tf.Variable()的变量 tf.variable_s ...
- tensorflow中使用tf.variable_scope和tf.get_variable的ValueError
ValueError: Variable conv1/weights1 already exists, disallowed. Did you mean to set reuse=True in Va ...
- tf.name_scope()和tf.variable_scope() (转)
网络层中变量存在两个问题: 随着层数的增多,导致变量名的增多: 在调用函数的时候,会重复生成变量,但他们存储的都是一样的变量. tf.variable不能解决这个问题. 变量作用域使用tf.var ...
- TensorFlow基础笔记(13) tf.name_scope tf.variable_scope学习
转载http://blog.csdn.net/jerr__y/article/details/60877873 1. 首先看看比较简单的 tf.name_scope(‘scope_name’). tf ...
- tf.name_scope tf.variable_scope学习
1. 首先看看比较简单的 tf.name_scope(‘scope_name’). tf.name_scope 主要结合 tf.Variable() 来使用,方便参数命名管理. ''' Signatu ...
- 通俗理解tf.name_scope()、tf.variable_scope()
前言:最近做一个实验,遇到TensorFlow变量作用域问题,对tf.name_scope().tf.variable_scope()等进行了较为深刻的比较,记录相关笔记:tf.name_scope( ...
随机推荐
- Appium入门指南 - 环境搭建和Case编写
本文档将详细介绍如何搭建 Appium 的运行环境,以及如何编写一个简单的 UI 自动化测试用例.其中,也会穿插讲解一些 Appium 的基本知识.关于 Appium 的更多信息,大家可以查看官方文档 ...
- Oracle--RMAN Recover 缺失的归档操作
一,环境简介 Oracle RMAN 备份的恢复分2个步骤:RESTRE 和 RECOVER.在这里回复的时候是依赖者归档文件的,当周一完成数据全备,保留归档的情况下,后期数据有问题,恢复的时候发现少 ...
- 强迫症福利--收起.NET程序的dll来
作为上床后需要下床检查好几次门关了没有的资深强迫症患者,有一个及其搞我的问题,就是dll问题. 曾几何时,在没有nuget的年代,当有依赖项需要引用的时候,只能通过文件引用来管理引用问题,版本问题,更 ...
- 一张图入门Python【中文版】
好久没写了,就拿这张图作为开篇吧,重新梳理自己学习的东西,最近两年人工智能炒红了python,devops的提出也把开发.运维整合到了一起,作为一个运维工程师,随着企业自动化运维的提出,光会shell ...
- FFT(快速傅里叶变换)
FFT(快速傅里叶变换) 前置知识 \(1.复数\) \(2.单位根\) \(3.循环结构\) \(4.C++\) 1.复数 \(定义:形如a+bi的数,其中i^2=-1\) \(计算:1.(a+bi ...
- vertica 设置最大会话数
默认会话数最大值55,如果超过了,就会报如下错误: com.vertica.support.exceptions.NonTransientConnectionException: [Vertica][ ...
- springboot+RabbitMQ 问题 RabbitListener 动态队列名称:Attribute value must be constant
因为多机环境fanout广播模式,每台机器需要使用自己的队列接受消息 所以尝试使用以下的方案 private static final String QUEUE_NAME="foo.&quo ...
- POSIX 正则表达式 BRE与ERE的差异
BRE,标准正则表达式,basic regular expressions ERE,扩展正则表达式,Extended Regular Expressions POSIX 正则表达式 传统上,POSIX ...
- SublimeText3安装Markdown插件
由于Webstrom对md文件的预览效果并不理想(与实际网页编译效果差别较大),所以我又改用Sublime进行本地编辑,下面介绍一下怎么搭建环境. 插件安装 整套环境我们就需要两个插件:Markdow ...
- WPF ObservableCollection,INotifyPropertyChanged
xaml: <DockPanel Margin="10"> <StackPanel DockPanel.Dock="Rig ...