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

  tf.Variable() 与 tf.get_variable() 的作用与区别:

  1)tf.Variable() 会自动监测命名冲突并自行处理,但是tf.get_variable() 遇到重名的变量创建且没有设置为共享变量时,则会报错。

import tensorflow as tf;

a1 = tf.Variable(tf.random_normal(shape=[2, 3], mean=0, stddev=1), name='a2')

a2 = 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) # 输出
a2:0
a2_1:0
import tensorflow as tf;

a1 = tf.get_variable(name='a1', shape=[1], initializer=tf.constant_initializer(1))
a3 = 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(a3.name) # 输出
ValueError: Variable a1 already exists, disallowed. Did you mean to set reuse=True or reuse=tf.AUTO_REUSE in VarScope? Originally defined at:

  2) tf.Variable() 和 tf.get_variable() 都是用于在一个name_scope下面获取或创建一个变量的两种方式,区别在于: tf.Variable()用于创建一个新变量,在同一个name_scope下面,可以创建相同名字的变量,底层实现会自动引入别名机制,两次调用产生了其实是两个不同的变量。tf.get_variable(<variable_name>)用于获取一个变量,并且不受name_scope的约束。当这个变量已经存在时,则自动获取;如果不存在,则自动创建一个变量。

  

import tensorflow as tf;
import numpy as np; with tf.name_scope('V1'): a1 = tf.Variable(tf.random_normal(shape=[2,3], mean=0, stddev=1), name='a2')
with tf.name_scope('V2'):
a2 = 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) # 输出
V1/a2:0
V2/a2:0
import tensorflow as tf;  

with tf.name_scope('V1'):
a1 = tf.get_variable(name='a1', shape=[1], initializer=tf.constant_initializer(1))
with tf.name_scope('V2'):
a2 = 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) # 输出
Variable a1 already exists, disallowed. Did you mean to set reuse=True in VarScope? Originally defined at:

  3)tf.name_scope() 与 tf.variable_scope(): tf.name_scope():主要用于管理一个图里面的各种op,返回的是一个以scope_name命名的context manager。一个graph会维护一个name_space的 堆,每一个namespace下面可以定义各种op或者子namespace,实现一种层次化有条理的管理,避免各个op之间命名冲突。 tf.variable_scope() 一般与tf.get_variable()配合使用,用于管理一个graph中变量的名字,避免变量之间的命名冲突。

import tensorflow as tf;
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')
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

  4)当要重复使用变量共享时,可以用tf.variable_scope() 和 tf.get_variable()来实现

import tensorflow as tf

with tf.variable_scope('V1', reuse=None):
a1 = tf.get_variable(name='a1', shape=[1], initializer=tf.constant_initializer(1)) with tf.variable_scope('V1', reuse=True):
a2 = 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) #输出
V1/a1:0
V1/a1:0

  上面的代码在第一个variable_scope中的reuse=None,在之后的variable_scope中若是要共享变量,就要将reuse=True。

  

tensorflow中的name_scope, variable_scope的更多相关文章

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

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

  2. Tensorflow中的name_scope和variable_scope

    Tensorflow是一个编程模型,几乎成为了一种编程语言(里面有变量.有操作......). Tensorflow编程分为两个阶段:构图阶段+运行时. Tensorflow构图阶段其实就是在对图进行 ...

  3. tensorflow里面共享变量、name_scope, variable_scope等如何理解

    tensorflow里面共享变量.name_scope, variable_scope等如何理解 name_scope, variable_scope目的:1 减少训练参数的个数. 2 区别同名变量 ...

  4. [翻译] Tensorflow中name scope和variable scope的区别是什么

    翻译自:https://stackoverflow.com/questions/35919020/whats-the-difference-of-name-scope-and-a-variable-s ...

  5. TensorFlow中的变量命名以及命名空间.

    What: 在Tensorflow中, 为了区别不同的变量(例如TensorBoard显示中), 会需要命名空间对不同的变量进行命名. 其中常用的两个函数为: tf.variable_scope, t ...

  6. tensorflow中slim模块api介绍

    tensorflow中slim模块api介绍 翻译 2017年08月29日 20:13:35   http://blog.csdn.net/guvcolie/article/details/77686 ...

  7. 第二十二节,TensorFlow中的图片分类模型库slim的使用、数据集处理

    Google在TensorFlow1.0,之后推出了一个叫slim的库,TF-slim是TensorFlow的一个新的轻量级的高级API接口.这个模块是在16年新推出的,其主要目的是来做所谓的“代码瘦 ...

  8. 第二十二节,TensorFlow中RNN实现一些其它知识补充

    一 初始化RNN 上一节中介绍了 通过cell类构建RNN的函数,其中有一个参数initial_state,即cell初始状态参数,TensorFlow中封装了对其初始化的方法. 1.初始化为0 对于 ...

  9. 第十八节,TensorFlow中使用批量归一化(BN)

    在深度学习章节里,已经介绍了批量归一化的概念,详情请点击这里:第九节,改善深层神经网络:超参数调试.正则化以优化(下) 神经网络在进行训练时,主要是用来学习数据的分布规律,如果数据的训练部分和测试部分 ...

随机推荐

  1. HTML表格,table,thead,tbody,tfoot,th,tr,td,的属性以及跨行,跨列

    在HTML中表格是作为一个整体来解析的,解析完才会在页面显示,如果表格很复杂很长,加载时间很长,用户体验就不好.所以这里就要用到表格结构标签,解析一部分就显示一部分,不用等表格全部加载完再显示. 表格 ...

  2. linux 下 ifcfg-ethx配置和解析

    网络接口配置文件[root@localhost ~]# cat /etc/sysconfig/network-scripts/ifcfg-eth0# Intel Corporation 82545EM ...

  3. 15-可视化工具Navicat的使用

    [转]15-可视化工具Navicat的使用 本节重点: 掌握Navicat的基本使用 # PS:在生产环境中操作MySQL数据库还是推荐使用命令行工具mysql,但在我们自己开发测试时,可以使用可视化 ...

  4. VMWAR-workstatuon : 安装win10、server 2008 r2、server 2012 r2

    最新版的VMWAR 不是很文档,建议大家还是下载稳定版,截止当前最新版的为15,用了,有点问题. 换成14~ 可以了.(15创建虚拟机安装vmware tools 怎么都安装不了). 关于创建虚拟机, ...

  5. java程序存入数据库中文乱码解决方案

    一.问题描述 背景:代码迁移,ssm框架在插入数据到mysql数据库时,中文乱码.代码中的编码配置没有问题,因为该项目代码以前使用过,没有问题.现在换了数据库,数据库配置也做了修改,统一使用utf8, ...

  6. neutron 多租户隔离的实现以及子网间路由的实现

    1.一个network相当于一个二层网络,使用vxlan 隧道连通所有的CNA节点. 2.一个VPC下有多个network,也就是会分配多个vxlan隧道,这些子网间的路由是通过DVR实现的.DVR就 ...

  7. Windows 批处理获取某路径下最新创建的文件的名称

    批处理获取某路径下最新创建的文件的名称 by:授客 QQ:1033553122 echo off setlocal enabledelayedexpansion rem 设置文件所在目录 set sr ...

  8. ReactNative编写规范

    <一>  React 代码规范 文件与组件命名 扩展名: 使用.js作为js文件的扩展名.如果同一个文件夹下有同名而不同作用的js文件,则通过中缀(小写)进一步区分,例如:HomePage ...

  9. PL/SQL重新编译包无反应案例2

    在这篇"PL/SQL重新编译包无反应"里面介绍了编译包无反应的情况,今天又遇到一起案例, 在测试环境中,一个包的STATUS为INVALID,重新编译时,一直处于编译状态,检查发现 ...

  10. mssql sqlerver 脚本 计算数据表的结余数的方法分享

    转自:http://www.maomao365.com/?p=5710 摘要:今天接到一个需求,有一张数据表,记录的是消费明细数据,现在需要做一个累计结余,记录每次的数据结余合计,下文将展示一种sql ...