tf.variable_scope和tf.name_scope的区别

TensorFlow入门(七) 充分理解 name / variable_scope 欢迎转载,但请务必注明原文出处及作者信息。@author: huangyongye @creat_date: 2017-03-08 前言: 本例子主要介绍 name_scope 和 variable_scope 的正确使用方式,学习并理解本例之后,你就能够真正读懂 TensorFlow 的很多代码并能够清晰地理解模型结构了。之前写过一个例子了: TensorFlow入门(四) name / v 阅读详情

tf.variable_scope可以让变量有相同的命名,包括tf.get_variable得到的变量,还有tf.Variable的变量

tf.name_scope可以让变量有相同的命名,只是限于tf.Variable的变量

例子1:

import tensorflow as tf;  
import numpy as np;  
import matplotlib.pyplot as plt;  
 
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

输出:
V1/a1:0
V1/a2:0
V2/a1:0
V2/a2:0

例子2:

import tensorflow as tf;  
import numpy as np;  
import matplotlib.pyplot as plt;  
 
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

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

例子2修改后:

import tensorflow as tf;  
import numpy as np;  
import matplotlib.pyplot as plt;  
 
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

输出:
V1/a2:0
V2/a2:0

彻底弄懂tf.Variable、tf.get_variable、tf.variable_scope以及tf.name_scope异同 我之前有转载过一篇讲tf.variable_scope和tf.name_scope的文章,今天一看,觉得还是没介绍太清楚,没讲到点子上,毕竟英文什么的看起来还是挺麻烦的。故而,今天来细细梳理下tf.Variable、tf.get_variable、tf.variable_scope以及tf.name_scope四者之间的区别与联系。 阅读详情

相关推荐

深入解析DDR5 RFM机制:从JESD79-5标准到硬件实现

本文深入解析了DDR5内存标准中的关键机制——RFM(Refresh Management)。文章从JESD79-5标准出发,详细阐述了RFM如何通过RAA计数器实现从被动定时到主动响应的智能刷新管理,并探讨了其硬件实现细节、命令调度挑战及系统级设计考量,为芯片设计工程师优化DDR5内存子系统性能与可靠性提供了实践指导。

fff88的博客 486

tf.name_scope与tf.variable_scope用法区别

本文由网络上一些回答和博文汇总而成。 要将这个问题解释清楚,得结合tensorflow中创建变量的两种方式tf.get_variable()和tf.Variable()一起说明。 在tf.name_scope下: tf.get_variable()创建的变量名不受tf.name_scope的影响,即创建的变量的name没有name_scope定义的前缀。而且,在未指定共享变量时,如果重名会报...

daizongxue的博客 6266

ADS2020实战:村田电感.s2p与.mod文件导入全流程对比(附仿真结果差异分析)

本文详细对比了ADS2020中村田电感的.s2p与.mod文件导入流程及仿真结果差异,重点分析了两种模型在低频段和高频段的精度表现。通过实际案例展示.s2p文件在高频仿真中的优势,以及.mod文件在低频和非线性特性模拟中的准确性,为射频电路设计提供模型选择策略和混合仿真技巧。

weixin_29289821的博客 519

tf.name_scope()和tf.variable_scope()解析

摘要: tf.name_scope()和tf.variable_scope()是两个作用域,一般与两个创建/调用变量的函数tf.variable() 和tf.get_variable()搭配使用。它们搭配在一起的两个常见用途:1)变量共享,2)tensorboard画流程图时为了可视化封装变量,这两种用途有特定的搭配方式,掌握好就可以用了。如果您想打破砂锅问到底,想要探寻更多的知识,了解它们的用法...

gqixf的博客 1万+

【Agent】多智能体框架MetaGPT入门教程

创建一个 Action 类的子类 SimpleWriteCode,子类中定义好 prompt,并解析返回结果。在 MetaGPT 中,

酒酿小圆子呀~ 3133

【Unity】ABB CRB 15000 外部引导运动实战:从配置到实时控制

本文详细介绍了ABB CRB 15000协作机器人的外部引导运动(EGM)功能在Unity环境中的实战应用。从RobotStudio配置到Unity实时通信架构,再到EGM运动控制技巧和问题排查,全面解析如何实现机器人的高精度实时控制。通过实际案例展示EGM技术在精密装配等场景中的显著效果,帮助开发者快速掌握这一先进技术。

weixin_29057695的博客 444

with tf.variable_scope():或with tf.name_scope():

keyword:with 用于上下文管理, 共享变量控制参数reuse取true,则作用域内可重用,否则不可重用,智能一点的话取tf.AUTO_REUSE:如果创建过就返回,没有创建过就创建一个新的变量返回 至于tf.name_scope和tf.variable_scope详情见下面URL: https://www.imooc.com/article/22966 https://www.cnblo...

weixin_45295666的博客 2909

set_data_check专题[使用,report_timing,setup/hold检查]

set_data_check用于约束『数据-对-数据』的『建立保持』时间检查,通常被称为非时序约束 ( non-sequential constraints ), 理论上set_data_check 可以设在任意的两个data pin 上,其中一个pin 称为 constrained pin 类似于寄存器的data pin, 另一个pin 称为related pin 类似于寄存器的clock pin.

cy413026的博客 1万+

tf.variable_scope和tf.name_scope的用法

tf.variable_scope可以让不同命名空间中的变量取相同的名字,无论tf.get_variable或者tf.Variable生成的变量 tf.name_scope具有类似的功能,但只限于tf.Variable生成的变量 import tensorflow as tf; import numpy as np; import matplotlib.pyplot as p...

ningyanggege的博客 380

Android 15.0 添加自定义服务,并生成jar给第三方app调用

在15.0系统ROM产品定制化开发中,由于需要新增加自定义的功能,所以要增加自定义服务,而app上层通过调用自定义服务,来调用相应的功能,所以系统需要先生成jar,然后生成jar 给上层app调用,接下来就来分析实现的步骤,然后来实现相关的功能 从而来实现所需要的功能

安卓兼职framework和app工程师的博客 667

【Pix4d精品教程】pix4d mapper相机未校准后手动添加连接点完美案例图文教程

如图所示,红色的照片为未校准的图片,软件无法自动获取连接点,有关相机未校准的原因及解决办法,可以参考《pix4d未校准相机:287 out of 402 images calibrated (71%), all images enabled, 6 block,问题分析及解决方案汇总》,本文以案例的形式,详细讲解pix4d中手动添加连接点的方法及操作步骤。 文章目录1. 手动添加连接点2. 重新优化3. 生成质量报告,检查精度 1. 手动添加连接点 在【空三射线】模式下查看校准失败的图片,记下照片的名称。.

智慧空间实验室 2万+

Tensorflow函数:tf.variable_scope() & tf.name_scope()

一. 函数的作用 tf.variable_scape() & tf.name_scope()这两个函数的主要作用是结合上下文管理器(with)生成一个变量空间,进而实现变量的高效管理。但是二者也存在区别: *name_scope*: 为了更好的管理变量的命名空间,但一般对get_variable()函数生成的变量不起作用 *variable_scope*:绝大部分情况下会和tf.ge...

abbott_zb的博客 591

华为OD机试真题 新系统 9月16号 【园区基站节能部署规划】

华为OD机试真题 新系统【园区基站节能部署规划】,提供C语言、Java、C++、Python、Go、JavaScript源码实现,并提供完整思路讲解,以及在线OJ模拟做题。华为OD机试真题 【园区基站节能部署规划】多语言题解。

qq_45776114的博客 351

Tensorflow中tf.name_scope() 和 tf.variable_scope() 的区别

目录 一、name_scope和variable_scope的用途 (1)variable_scope用途:共享变量 (2)可视化用途:画Tensorboard流程图时封装 二、TensorFlow中name_scope和variable_scope区别 (1)TF中创建变量的方式有两种:tf.get_variable()和tf.Variable() (2)Tensorflow中有两种...

Microstrong 1640

VB摄像头监控

该VB模块可对摄像头捕获的画面进行录像等,可在创建的窗口进行控制

RU DOS版、EFI版32bit/64bit BIOS常用工具

DOS版、EFI版的RU工具,可用来查看IO、MEM、PCI、SMBIOS等资源。亲测在EFI SHELL下可用。

tf.name_scope()和tf.variable_scope() 的简介和区别

一 tf.name_scope() 用于定义python op的上下文管理器; __init__( name, default_name=None, values=None ) name:此scope的名字; default_name:name没有预设值时使用此默认名字 values:传递给name_scopesd 变量列表,可在上下文区域对这些变量操作   ...

Fan9_的博客 889

IEM模型,地表粗糙度模拟

利用微波反演土壤水分时候,利用IEM模型可以实现对随机地表的模拟

TensorFlow——命名空间tf.variable_scope和tf.name_scope的区别

tf.variable_scope和tf.name_scope都提供了命名空间管理的功能。这两个函数在大部分情况下是等价的,唯一的区别是在使用tf.get_variable函数时。 简单来说,tf.get_variable函数受tf.variable_scope命名空间影响,不受tf.name_scope命名空间影响。如下面代码所示: import tensorflow as tf with tf.variable_scope("a1"): # 在命名空间a1下获取变量“a1”,于是得到的变

CMTSA的博客 169

华为的C语言编程规范.pdf

华为技术有限公司内部技术规范。本规范制定了编写C语言程序的基本原则、规则和建议。从代码的清晰、简洁、可测试、安全、程序效率、可移植各个方面对C语言编程作出了具体指导。

tf.variable_scope()和tf.name_scope()函数的区别

参考链接 https://blog.csdn.net/uestc_c2_403/article/details/72328815tf.variable_scope可以让变量有相同的命名,包括tf.get_variable得到的变量,还有tf.Variable的变量tf.name_scope可以让变量有相同的命名,只是限于tf.Variable的变量...

u011916041的博客 229

Tensorflow笔记——tf.Variable和tf.get_variable的区别以及tf.variable_scope以及tf.name_scope异同

一、 tf.Variable和tf.get_variable的区别 在Tensorflow中有两种方法生成变量variable,tf.get_variable()和tf.Variable() 1.tf,Variable tf.Variable( initial_value=None, trainable=None, validate_shape=True, caching_device=None, name=None, variable_def=None, dtype=None, impor

qq_45704645的博客 554

tf.variable_scope 和 tf.name_scope 的区别

这两个函数不仅名称相近,功能上也差不多,不过name_scope的使用范围更小,一般我们都选择variable_scope,下面说下这两个的区别。 我们知道在TensorFlow中每个变量都有自己的作用域,我们常常用作用域+变量名来作为这个变量独一无二的名称。那么,这两个函数就是为了实现这样的功能的。请看下面代码: import tensorflow as tf with tf.varia...

我的博客有点东西 245

【tensorflow】tf.name_scope和tf.variable_scope以及tf.variable和tf.get_variable区别

tf.name_scope()和tf.variable_scope()是两个作用域,一般与两个创建、调用变量的函数tf.variable() 和tf.get_variable()搭配使用。它们搭配在一起的两个常见用途:1)变量共享,2)tensorboard画流程图时为了可视化封装变量。 tf.name_scope(名字范围)下可以有相同的变量名,但仅限tf.Variable的变量 tf.va...

zkq_1986的博客 206

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

在训练深度网络时,为了减少需要训练参数的个数(比如具有simase结构的LSTM模型)、或是多机多卡并行化训练大数据大模型(比如数据并行化)等情况时,往往需要共享变量。另外一方面是当一个深度学习模型变得非常复杂的时候,往往存在大量的变量和操作,如何避免这些变量名和操作名的唯一不重复,同时维护一个条理清晰的graph非常重要。 因此,tensorflow中用tf.Variable(),tf...

郭耀华's Blog | 人工智能领域技术博客 119

如何理解name_scope / variable_scope 与 tf.Variable()、tf.get_variable()和tf.placeholder() 的区别

一、name_scope 与 variable_scope: name_scope: * tf.name_scope() 主要是用来管理命名空间的,这样子让我们的整个模型更加有条理。为了更好地管理变量的命名空间而提出的。比如在 tensorboard 中,因为引入了 name_scope, 我们的 Graph 看起来才井然有序。 variable_scope: * 而 tf.variable_scope() 的作用是为了实现变量共享,它和 tf.get_variable() 来完成变量共享的功能。 先

393

tf.name_scope和tf.variable_scope的区别

import tensorflow as tf; import numpy as np; import matplotlib.pyplot as plt; # 重置已存在的图 tf.reset_default_graph() with tf.variable_scope('V1'): a1 = tf.get_variable(name='a1', shape...

BlueBubble的博客 403

命名空间:tf.variable_scope()函数和tf.name_scope()函数的使用及区别

先看代码:#命名空间函数tf.variable_scope()和tf.name_scope()函数区别于使用 import tensorflow as tf with tf.variable_scope("foo"): a = tf.get_variable("bar", [1]) print(a.name) #foo/bar:0 ...

u011582187的专栏 2941
上一篇: softmax_cross_entropy_with_logits函数原型解析
下一篇: 心电图入门
王小_
博客等级 码龄11年 28粉丝 37原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值