tf.variable_scope中的reuse
一、两种scope:variable与name
tf.variable_scope()是对变量进行命名管理,而tf.name_scope()是对算子(op)进行命名管理,二者相互不影响。见下例:
import tensorflow as tffor i in range(10):with tf.name_scope('test'):a = tf.constant([1])b = tf.constant([1])c = a + bwith tf.variable_scope("foo",reuse=True): # 设定为重用模式,v = tf.get_variable("v", [1])v1 = tf.get_variable("v", [1])print(v)print(v1)assert v is v1print(c)
结果中算子c的名称会随着循环的增加而改变:"test_1_2_..._9/add:0"。而v和v1两个变量始终都是'foo/v:0'。
二、variable_scope的reuse方法
如果需要同一个参数参与不同的运算,可以使用reuse构建不同的指针指向相同的variable。例如在tf中想在多GPU上进行并行计算,需要使用reuse完成模型的复制。
import tensorflow as tfwith tf.variable_scope("foo",reuse=False): # 只有在false下才能新增变量v = tf.get_variable("v", [1]) # 创建新变量tf.get_variable_scope().reuse_variables() # 开启reuse模式,不能够再新建变量v1 = tf.get_variable("v", [1]) # 自动reuse变量vprint(v)print(v1)assert v is v1for i in range(10):with tf.variable_scope("foo",reuse=True): # 开启reuse模式v = tf.get_variable("v", [1]) # 自动reuse变量vprint(v)
最终结果:全为'foo/v:0'
官方示例:
import tensorflow as tfwith tf.variable_scope("foo"):v = tf.get_variable("v", [1])
with tf.variable_scope("foo", reuse=True):v1 = tf.get_variable("v", [1])
注:可以在后续对scope的reuse模式进行改变,从而新增变量。reuse的本质是为创建好的variable添加指针。
参考内容:
https://www.jianshu.com/p/ab0d38725f88
tf.variable_scope中的reuse相关推荐
- tf.Variable,tf.get_variable,tf.variable_scope,tf.name_scope区别分析
1.tf.Variable与tf.get_variable ①在创建变量的时候,两者是等价的 ②tf.Variable的变量名是一个可选项,而tf.get_variable必须要有变量名 import ...
- tf.get_variable与tf.variable_scope
一.tf.Variable与tf.get_variable tensorflow提供了通过变量名称来创建或者获取一个变量的机制.通过这个机制,在不同的函数中可以直接通过变量的名字来使用变量,而不需要将 ...
- 30天从入门到精通TensorFlow1.x 第三天,tf.variable_scope()共享或重用变量
tf.variable_scope()共享或重用变量 文章目录 一.接前一天 二.`tf.variable_scope()`共享或重用变量 1. 背景 2. 目的 3. `tf.variable_sc ...
- 通俗理解tf.name_scope()、tf.variable_scope()
前言:最近做一个实验,遇到TensorFlow变量作用域问题,对tf.name_scope().tf.variable_scope()等进行了较为深刻的比较,记录相关笔记: tf.name_scope ...
- Tensorflow函数——tf.variable_scope()
tf.variable_scope(name_or_scope,default_name=None,values=None,initializer=None,regularizer=None,cach ...
- tf.variable_scope 参数
最近在看TensorFlow的变量管理,发现很多代码中tf.variable_scope()参数的数量及意义还不太清楚,特此记录: def __init__(self, name_or_scope, ...
- tf.name_scope() 和 tf.variable_scope() 的用法和玄机
2019独角兽企业重金招聘Python工程师标准>>> 一. name_scope 和 variable_scope的用途 用途1: 共享变量 TensorFlow (TF) 中,n ...
- tf.variable和tf.get_Variable以及tf.name_scope和tf.variable_scope的区别
在训练深度网络时,为了减少需要训练参数的个数(比如具有simase结构的LSTM模型).或是多机多卡并行化训练大数据大模型(比如数据并行化)等情况时,往往需要共享变量.另外一方面是当一个深度学习模型变 ...
- tf.name_scope()与tf.variable_scope()
TensorFlow的tf.name_scope().tf.variable_scope()是两个作用域函数,一般与两个创建/调用变量的函数tf.variable() 和tf.get_variable ...
最新文章
- jenkins 修改工作目录
- go语言mysql视频_Go语言实战流媒体视频网站
- 120000字,你们要的Java 并发编程图文小册整理出来了,免费送给大家!
- python打开文件并读取内容-Python实现的读取文件内容并写入其他文件操作示例
- ORA-38301:can not perform DDL/DML over objects in Recycle Bin
- 绝对干货,教你4分钟插入1000万条数据到mysql数据库表,快快进来
- linux 快照备份导出,KVM之虚拟机几种备份方法(快照/克隆/导出)
- 一文读懂什么是C++移动语义《一》
- python写xml文件_用python写xml文件
- 安卓逆向_24( 一 ) --- Hook 框架 frida( Hook Java层 和 so层) )
- HTML+CSS+JS实现 ❤️卡通人物吃水果游戏❤️
- 几个提升Go业务开发效率的流行框架和开源库
- linux下的open file是什么,linux修改open files数
- mysql 十年_十年前的老问题,MySQL 8.0有了答案
- 神秘的罗斯柴尔德家族
- paypal简单分享
- 丢番图生平编程java_丢番图
- 日语基础学习 Day 07
- 详解办公室装修设计省钱妙计
- win10清理C盘空间
热门文章
- 软工专业全国第二,南京大学软件学院逐年扩招
- spring cloud分布式微服务-大话Spring Cloud
- 酒,引人深思的神奇存在
- 计算机网络与互联网(一)
- uni-app图片加水印;小程序图片添加水印;使用canvas上传图片加水印
- iOS安全逆向之旅--安全逆向环境搭建和工具使用介绍
- 国家税务总局北京市电子税务局代开增值税电子普通发票教程
- 用matlab实现女声变男声步骤,男声变女声matlab程序
- MySQL Prepare 语句使用方法
- 前后端分离之后端代码实现获取数据库数据(2)——django+mysql+vue+element