tf.Variable、tf.get_variable、tf.variable_scope以及tf.name_scope关系

1. tf.Variable与tf.get_variable

tensorflow提供了通过变量名称来创建或者获取一个变量的机制。通过这个机制,在不同的函数中可以直接通过变量的名字来使用变量,而不需要将变量通过参数的形式到处传递。

TensorFlow中通过变量名获取变量的机制主要是通过tf.get_variable和tf.variable_scope实现的。

当然,变量也可以通过tf.Varivale来创建。当tf.get_variable用于变量创建时,和tf.Variable的功能基本等价

#以下两个定义是等价的
v = tf.get_variable(‘v‘, shape=[1], initializer=tf.constant_initializer(1.0))
v = tf.Variable(tf.constant(1.0, shape=[1], name=‘v‘)

tf.get_varialbe和tf.Variable最大的区别在于:tf.Variable的变量名是一个可选项,通过name=’v’的形式给出。但是tf.get_variable必须指定变量名

2. tf.get_variable与tf.variable_scope

上面已经提到过了:TensorFlow中通过变量名获取变量的机制主要是通过tf.get_variable和tf.variable_scope实现的。在这里,我主要解释下大家深恶痛绝的reuse问题

其实只要记住一件事情就ok了:当reuse为False或者None时(这也是默认值),同一个tf.variable_scope下面的变量名不能相同;当reuse为True时,tf.variable_scope只能获取已经创建过的变量。

下面我们通过代码来看下:

#reuse=False时会报错的情况:
with tf.variable_scope(‘foo‘):
    v = tf.get_variable(‘v‘,[1],initializer=tf.constant_initializer(1.0))

with tf.variable_scope(‘foo‘):
    v1 = tf.get_variable(‘v‘,[1])

在这种情况下会报错:Variable foo/v already exists, disallowed.Did you mean to set reuse=True in Varscope?

其原因就是在命名空间foo中创建了相同的变量。如果我要在foo下创建一个变量v1,其name=‘v’,只需要将reuse设置为Ture就ok了。将上面第二部分代码修改为:

with tf.variable_scope(‘foo‘, reuse=True):
    v1 = tf.get_variable(‘v‘,[1])
    print(v1.name)      #结果为foo/v

当reuse已经设置为True时,tf.variable_scope只能获取已经创建过的变量。这个时候,在命名空间bar中创建name=‘v’的变量v3,将会报错:Variable bar/v dose not exists, diallowed. Did you mean to set reuse=None in VarScope?

with tf.variable_scope(‘bar‘, reuse=True):
    v3 = tf.get_variable(‘v‘,[1])

简而言之,reuse=False时,tf.variable_scope创建变量;reuse=True时,tf.variable_scope获取变量

3. tf.variable_scope与tf.name_scope

除了tf.variable_scope,tf.name_scope函数也提供了命名空间管理的功能。这两个函数在大部分情况下是等价的,唯一的区别是在使用tf.get_variable函数时。

tf.get_variable函数不受tf.name_scope的影响

我们从代码看下这句话的具体意思。

首先是tf.variable_scope:

with tf.variable_scope(‘foo‘):
    a = tf.get_variable(‘bar‘,[1])
    print(a.name)#结果为foo/bar:0

  

再看tf.name_scope:

with tf.name_scope(‘a‘):
    a=tf.Variable([1])
    print(a.name)#结果为a/Variable:0

    b=tf.get_variable(‘b‘,[1])
    print(b.name)#结果为b:0

  

从这个结果中,我们能很清晰地看到,tf.get_variable创建的变量并不是a/b:0,而是b:0。这就表示了在tf.name_scope函数下,tf.get_variable不受其约束

原文地址:https://www.cnblogs.com/MY0213/p/9270864.html

时间: 2024-07-31 17:57:37

tf.Variable、tf.get_variable、tf.variable_scope以及tf.name_scope关系的相关文章

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

ValueError: Variable conv1/weights1 already exists, disallowed. Did you mean to set reuse=True in VarScope? Originally defined at: 在使用tensorflow 中的tf.variable_scope和tf.get_variable搭建网络时,重复运行程序会报以上的ValueError错误,这是因为第二次运行时,内存中已经存在名字相同的层或者参数,发生了冲突,所以会提示

TensorFlow基础——tf.Variable()和tf.constant()

tf.Variable()生成变量 tf.constant()生成常量 变量需要初始化: init = tf.initialize_all_variables() sess.run(init) 1 import tensorflow as tf 2 3 state = tf.Variable(0,name='counter') 4 print(state.name) 5 one = tf.constant(1) 6 7 new_value = tf.add(state,one) 8 update

TensorFlow 辨异 —— tf.placeholder 与 tf.Variable

二者的主要区别在于: tf.Variable:主要在于一些可训练变量(trainable variables),比如模型的权重(weights,W)或者偏执值(bias): 声明时,必须提供初始值: 名称的真实含义,在于变量,也即在真实训练时,其值是会改变的,自然事先需要指定初始值:  weights = tf.Variable( tf.truncated_normal([IMAGE_PIXELS, hidden1_units], stddev=1./math.sqrt(float(IMAGE_

深度学习原理与框架-递归神经网络-RNN_exmaple(代码) 1.rnn.BasicLSTMCell(构造基本网络) 2.tf.nn.dynamic_rnn(执行rnn网络) 3.tf.expand_dim(增加输入数据的维度) 4.tf.tile(在某个维度上按照倍数进行平铺迭代) 5.tf.squeeze(去除维度上为1的维度)

1. rnn.BasicLSTMCell(num_hidden) #  构造单层的lstm网络结构 参数说明:num_hidden表示隐藏层的个数 2.tf.nn.dynamic_rnn(cell, self.x, tf.float32) # 执行lstm网络,获得state和outputs 参数说明:cell表示实例化的rnn网络,self.x表示输入层,tf.float32表示类型 3. tf.expand_dim(self.w, axis=0) 对数据增加一个维度 参数说明:self.w表

Variable和get_variable的用法以及区别

在tensorflow中,可以使用tf.Variable来创建一个变量,也可以使用tf.get_variable来创建一个变量,但是在一个模型需要使用其他模型的变量时,tf.get_variable就派上大用场了. 先分别介绍两个函数的用法: 1 import tensorflow as tf 2 var1 = tf.Variable(1.0,name='firstvar') 3 print('var1:',var1.name) 4 var1 = tf.Variable(2.0,name='fi

tensorflow op tf.global_variables_initializer

一.安装目前用了tensorflow.deeplearning4j两个深度学习框架, tensorflow 之前一直支持到python 3.5,目前以更新到3.6,故安装最新版体验使用. 慢慢长征路:安装过程如下 WIN10: anaconda3.5: PYTHON3.6: tensorflow1.4: 二.TensorFlow 基本概念与原理理解 1.TensorFlow 的工作原理 TensorFlow是用数据流图(data flow graphs)技术来进行数值计算的.数据流图是描述有向图

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

翻译自:https://stackoverflow.com/questions/35919020/whats-the-difference-of-name-scope-and-a-variable-scope-in-tensorflow 问题:下面这几个函数的区别是什么? tf.variable_op_scope(values, name, default_name, initializer=None) Returns a context manager for defining an op t

character-RNN模型介绍以及代码解析

RNN是一个很有意思的模型.早在20年前就有学者发现了它强大的时序记忆能力,另外学术界以证实RNN模型属于Turning-Complete,即理论上可以模拟任何函数.但实际运作上,一开始由于vanishing and exploiting gradient问题导致BPTT算法学习不了长期记忆.虽然之后有了LSTM(长短记忆)模型对普通RNN模型的修改,但是训练上还是公认的比较困难.在Tensorflow框架里,之前的两篇博客已经就官方给出的PTB和Machine Translation模型进行了

TensorFlow官方样例

一.PTB模型 RNN 模型作为一个可以学习时间序列的模型被认为是深度学习中比较重要的一类模型.在Tensorflow的官方教程中,有两个与之相关的模型被实现出来.第一个模型是围绕着Zaremba的论文Recurrent Neural Network Regularization,以Tensorflow框架为载体进行的实验再现工作.第二个模型则是较为实用的英语法语翻译器.在这篇博客里,我会主要针对第一个模型的代码进行解析.在之后的随笔里我会进而解析英语法语翻译器的机能. 论文以及Tensorfl