Variable

tensorflow中有两个关于variable的op,tf.Variable()与tf.get_variable()下面介绍这两个的区别

使用tf.Variable时,如果检测到命名冲突,系统会自己处理。使用tf.get_variable()时,系统不会处理冲突,而会报错

import tensorflow as tf
w_1 = tf.Variable(3,name="w_1")
w_2 = tf.Variable(1,name="w_1")
print w_1.name
print w_2.name
#输出
#w_1:0
#w_1_1:0

import tensorflow as tf

w_1 = tf.get_variable(name="w_1",initializer=1)
w_2 = tf.get_variable(name="w_1",initializer=2)
#错误信息
#ValueError: Variable w_1 already exists, disallowed. Did
#you mean to set reuse=True in VarScope?

基于这两个函数的特性,当我们需要共享变量的时候,需要使用tf.get_variable()。在其他情况下,这两个的用法是一样的

  • tf.get_variable() 以及 tf.Variable() 是 TensorFlow 中创建变量的两种主要方式;
  • 如果在 tf.name_scope() 环境下分别使用 tf.get_variable() 和 tf.Variable(),两者的主要区别在于
    • tf.get_variable() 创建的变量名不受 name_scope 的影响;
    • tf.get_variable() 创建的变量,name 属性值不可以相同;tf.Variable() 创建变量时,name 属性值允许重复(底层实现时,会自动引入别名机制
import tensorflow as tf

with tf.variable_scope("scope1"):
w1 = tf.get_variable("w1", shape=[])
w2 = tf.Variable(0.0, name="w2")
with tf.variable_scope("scope1", reuse=True):
w1_p = tf.get_variable("w1", shape=[])
w2_p = tf.Variable(1.0, name="w2") assert w1 == w1_p
assert w2 != w2_p

get_variable() 函数的行为依赖于 reuse 的状态:

  • case1:reuse 设置为 False,创建并返回新变量:

    with tf.variable_scope('foo'):
    v = tf.get_variable('v', [1])
    assert v.name == 'foo/v:0
  • case2:reuse 设置为 True,将会按照给定的名字在以存的变量中搜寻:

    with tf.variable_scope('foo'):
    v = tf.get_variable('v', [1])
    with tf.variable_scope('foo', reuse=True):
    v1 = tf.get_variable('v')
    assert v1 == v

看到这,就可以明白官网上说的参数复用的真面目了。由于tf.Variable() 每次都在创建新对象,所有reuse=True 和它并没有什么关系。对于get_variable(),来说,如果已经创建的变量对象,就把那个对象返回,如果没有创建变量对象的话,就创建一个新的。

variable_scope()

一个双层嵌套名称空间:

with tf.variable_scope('foo'):
with tf.variable_scope('bar'):
v = tf.get_variable('v', [1])
assert v.name == 'foo/bar/v:0'
with tf.name_scope('foo'):
with tf.variable_scope('bar'):
v = tf.get_variable('v', [1])
assert v.name == 'bar/v:0'

TensorFlow学习笔记(1):variable与get_variable, name_scope()和variable_scope()的更多相关文章

  1. Tensorflow学习笔记02-Session,Variable,placeholder

    Session会话控制 使用tensorflow创建两个矩阵,并使其相乘 matrix1=tf.constant([[3,3]]) matrix2=tf.constant([[2], [2]]) pr ...

  2. TensorFlow学习笔记——LeNet-5(训练自己的数据集)

    在之前的TensorFlow学习笔记——图像识别与卷积神经网络(链接:请点击我)中了解了一下经典的卷积神经网络模型LeNet模型.那其实之前学习了别人的代码实现了LeNet网络对MNIST数据集的训练 ...

  3. tensorflow学习笔记——VGGNet

    2014年,牛津大学计算机视觉组(Visual Geometry Group)和 Google DeepMind 公司的研究员一起研发了新的深度卷积神经网络:VGGNet ,并取得了ILSVRC201 ...

  4. Tensorflow学习笔记2019.01.03

    tensorflow学习笔记: 3.2 Tensorflow中定义数据流图 张量知识矩阵的一个超集. 超集:如果一个集合S2中的每一个元素都在集合S1中,且集合S1中可能包含S2中没有的元素,则集合S ...

  5. tensorflow学习笔记——使用TensorFlow操作MNIST数据(2)

    tensorflow学习笔记——使用TensorFlow操作MNIST数据(1) 一:神经网络知识点整理 1.1,多层:使用多层权重,例如多层全连接方式 以下定义了三个隐藏层的全连接方式的神经网络样例 ...

  6. tensorflow学习笔记——使用TensorFlow操作MNIST数据(1)

    续集请点击我:tensorflow学习笔记——使用TensorFlow操作MNIST数据(2) 本节开始学习使用tensorflow教程,当然从最简单的MNIST开始.这怎么说呢,就好比编程入门有He ...

  7. Tensorflow学习笔记2:About Session, Graph, Operation and Tensor

    简介 上一篇笔记:Tensorflow学习笔记1:Get Started 我们谈到Tensorflow是基于图(Graph)的计算系统.而图的节点则是由操作(Operation)来构成的,而图的各个节 ...

  8. Tensorflow学习笔记2019.01.22

    tensorflow学习笔记2 edit by Strangewx 2019.01.04 4.1 机器学习基础 4.1.1 一般结构: 初始化模型参数:通常随机赋值,简单模型赋值0 训练数据:一般打乱 ...

  9. TensorFlow学习笔记之--[compute_gradients和apply_gradients原理浅析]

    I optimizer.minimize(loss, var_list) 我们都知道,TensorFlow为我们提供了丰富的优化函数,例如GradientDescentOptimizer.这个方法会自 ...

随机推荐

  1. 除了Office和wps,还有什么办公软件比较好用?

    一.不能没有的pdf软件 以下介绍的是PDF(Portable Document Format)常用的阅读.编撰以及其他工具.阅读工具1.Adobe AcrobatReader中文版应用平台:Wind ...

  2. 状态机学习(六)解析JSON2

    来自 从零开始的 JSON 库教程 从零开始教授如何写一个符合标准的 C 语言 JSON 库 作者 Milo Yip https://zhuanlan.zhihu.com/json-tutorial ...

  3. Sort Array By Parity II LT922

    Given an array A of non-negative integers, half of the integers in A are odd, and half of the intege ...

  4. mysql查询数据

    select column,column from table where clause [limit n] [offset]; 查询语句中你可以使用一个或者多个表,表之间使用逗号(,)分割,并使用W ...

  5. Testng用例失败重新运行

    Testng用例失败重新运行   在ui测试用例的运行过程中,发现有很多不确定的因素会导致用例失败,比如网络原因,比如屏幕滑动失败等.想到需要让测试用例,在失败后重新运行来提高测试成功率. 在gith ...

  6. ABP框架系列之四十六:(Setting-Management-设置管理)

    Introduction Every application need to store some settings and use these settings in somewhere in th ...

  7. 走进JDK(五)------AbstractList

    接下来的一段时间重点介绍java.util这个包中的内容,这个包厉害了,包含了collection与map,提供了集合.队列.映射等实现.一张图了解java中的集合类: AbstractList 一. ...

  8. 曙光服务器挂载EMC存储

    1.登录集群(用户名密码远程登录,然后切换到root用户) 2.连接主机:ssh node72 3.在主机下进行存储挂载: 1)fdisk -l 查看磁盘信息,如下图所示: 2)查看磁盘挂载信息:mo ...

  9. 使用freemarker对模板进行渲染

    最近项目中使用到了,对word模板进行编辑和渲染,所以使用到了模板引擎技术. 在项目中,我们前端使用的富文本编辑器,进行展示和保存(和word格式一致),后端采用了freemarker进行数据的渲染. ...

  10. spass按位置编码,进行排序题处理与分析

    本范例即需建立Q4_1至Q4_4 等四个变项, 各变量的数值则是排序的内容,共有0.1.2.3.4 等五种可能,0代表该选项没有被受测者选取,1.2.3.4分别代表被受测者指为第一至第四顺位. htt ...