在训练深度网络时,为了减少需要训练参数的个数(比如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. 面试官:你分析过mybatis工作原理吗?

    Mybatis工作原理也是面试的一大考点,必须要对其非常清晰,这样才能怼回去.本文建立在Spring+SpringMVC+Mybatis整合的项目之上. 我将其工作原理分为六个部分: 读取核心配置文件 ...

  2. Java中static与final

    修饰变量:static:静态变量,是属于这个类的final :常量,只能赋值一次static final:静态常量,必须立即初始化(同时具有static.final的特点) 修饰方法:static:静 ...

  3. canvas实现黑客帝国矩形阵

    在博客园看到了车大棒的写了一篇关于实现黑客帝国矩形阵,觉得canvas还是有一些奇妙的地方所在,故做个笔记记录一下. 实现的效果如下: 真的是一两行关键的代码添加就能实现意想不到的效果. 由于是can ...

  4. angularJS中控制器和作用范围

    $scope是$rootScope的子作用域控制对象,$rootScope的id为1,其他的为2,3,4... 不同的控制器之间,所对应的作用域控制对象$scope,之间是相互隔离的,如果要共享数据, ...

  5. mysql之全球化和本地化:字符集、校对集、中文编码问题

    本文内容: 什么是字符集?什么是校对集? 查看字符集和校对集 设置字符集和校对集 mysql中的中文数据问题 首发日期:2018-04-19 什么是字符集?什么是校对集? 字符集是字母和符号的集合,每 ...

  6. 测者的性测试手册:SWAP的监控

    swap是什么 swap是磁盘上的一块区域,可以使一个磁盘分区,也可以是一个文件,也可能是一个两种的组合.当物理内存资源紧张的时候,操作系统(Linux)会将一些不常访问的数据放到swap里.为其他常 ...

  7. Keras实现VGG16

    一.代码实现 # -*- coding: utf-8 -*- """ Created on Sat Feb 9 15:33:39 2019 @author: zhen & ...

  8. hadoop java上传文件

    import java.io.BufferedInputStream; import java.io.FileInputStream; import java.io.InputStream; impo ...

  9. @Autowired注解与@resource注解的区别(十分详细)

    背景: 今天下班路上看到一个大货车,于是想到了装配,然后脑海里跳出了一个注解@Autowired(自动装配),于是又想到最近工作项目用的都是@Resource注解来进行装配.于是本着学什么东西都要一钻 ...

  10. 前后端分离djangorestframework——限流频率组件

    频率限制 什么是频率限制 目前我们开发的都是API接口,且是开房的API接口.传给前端来处理的,也就是说,只要有人拿到这个接口,任何人都可以通过这个API接口获取数据,那么像网络爬虫的,请求速度又快, ...