【Tensorflow】name_scope() 和 variable_scope() 区别
2018-03-13 20:04
411 查看
本文主要有两个重点:
1.
2.
输出
总结
1.如果没有给
2.声明重复名字的
3.
4.调用存在可训练变量,长度为0,所以
结果
总结
1.如果没有给
2.声明重复名字的
3.
4.
5.
结果
代码2
结果
代码3
结果
总结
1.
2.使用
3.可以将两个重名的变量在两个
结果
总结
1.不在同一个
2.在输出
结果
总结
在
1.
placeholder、
Variable、
get_variable区别
2.
name_scope、
variable_scope区别
placeholder
、Variable
、get_variable
区别
1.实验一
代码import tensorflow as tf v1 = tf.placeholder(tf.float32, shape=[1]) print(v1.name) v1 = tf.placeholder(tf.float32, shape=[1], name='users') print(v1.name) v1 = tf.placeholder(tf.float32, shape=[1], name='users') print(v1.name) print(type(v1)) phs = tf.trainable_variables() print(len(phs))
输出
Placeholder:0 users:0 users_1:0 <class 'tensorflow.python.framework.ops.Tensor'> 0
总结
1.如果没有给
placeholder指定名字,那么默认是
Placeholder、
Placeholder_1。
2.声明重复名字的
placeholder是允许的,但是系统会按照顺序进行编号
name、
name_1。
3.
placeholder是
Tensor类型。
4.调用存在可训练变量,长度为0,所以
placeholder属于不可训练参数。
2.实验二
代码import tensorflow as tf v2 = tf.Variable([1], dtype=tf.float32) print(v2.name) v2 = tf.Variable([1], dtype=tf.float32, name='v') print(v2.name) v2 = tf.Variable([1], dtype=tf.float32, name='v') print(v2.name) print(type(v2)) vs = tf.trainable_variables() for i in vs: print(i)
结果
Variable:0 v:0 v_1:0 <class 'tensorflow.python.ops.variables.Variable'> <tf.Variable 'Variable:0' shape=(1,) dtype=float32_ref> <tf.Variable 'v:0' shape=(1,) dtype=float32_ref> <tf.Variable 'v_1:0' shape=(1,) dtype=float32_ref>
总结
1.如果没有给
variable指定名字,那么默认是
Variable、
Variable_1。
2.声明重复名字的
variable是允许的,但是系统会按照顺序进行编号
name、
name_1。
3.
variable是
Variable类型。
4.
variable是可训练的。
5.
variable创建并赋值的时候,会构建一个对象存储在内存中。
3.实验三
代码1import tensorflow as tf v3 = tf.get_variable(shape=[1], name='gv') print(v3.name) v3 = tf.get_variable(shape=[1], name='gv') print(v3.name)
结果
gv:0 ValueError: Variable gv already exists, disallowed. Did you mean to set reuse=True or reuse=tf.AUTO_REUSE in VarScope? Originally defined at:
代码2
import tensorflow as tf with tf.variable_scope('test') as scope: v3 = tf.get_variable(shape=[1], name='gv') print(v3.name) scope.reuse_variables() v3 = tf.get_variable(shape=[1], name='gv') print(v3.name)
结果
test/gv:0 test/gv:0
代码3
import tensorflow as tf with tf.variable_scope('test') as scope: v3 = tf.get_variable(shape=[1], name='gv') print(v3.name) with tf.variable_scope('test1') as scope: v3 = tf.get_variable(shape=[1], name='gv') print(v3.name)
结果
test/gv:0 test1/gv:0
总结
1.
get_variable不可以重复创建相同名字的变量。
2.使用
reuse_variable()可以实现变量重用(但是要放在一个变量域中
variable_scope,重用之后还是一个可训练变量。
3.可以将两个重名的变量在两个
scope中用
get_variable定义。也就是说变量是可以被域限定的。
name_scope
、variable_scope
区别
1.实验一
代码import tensorflow as tf with tf.name_scope('ns1'): v1 = tf.Variable([1], name='v1') v4 = tf.get_variable(shape=[1], name='v3') with tf.variable_scope('vs1'): v2 = tf.Variable([1], name='v2') v3 = tf.get_variable(shape=[1], name='v3') print('v1-name: ',v1.name) print('v2-name: ',v2.name) print('v3-name: ',v3.name) print('v4-name: ',v4.name)
结果
v1-name: ns1/v1:0 v2-name: ns1/vs1/v2:0 v3-name: vs1/v3:0 v4-name: v3:0
总结
1.不在同一个
variable_scope的
get_variable是互不影响的。
2.在输出
get_variable变量时,是不受
name_scope的影响的。
2.实验二
代码import tensorflow as tf def conv(kernel_shape, bias_shape): weights = tf.get_variable('weights', kernel_shape, initializer=tf.random_normal_initializer()) biases = tf.get_variable('biases', bias_shape, initializer=tf.constant_initializer()) return None def net(): with tf.variable_scope('net1'): conv([5, 5, 1, 32], [32]) with tf.variable_scope('net2'): conv([5, 5, 32, 64], [64]) return None if __name__ == '__main__': with tf.variable_scope('test') as scope: net() scope.reuse_variables() net() for var in tf.trainable_variables(): print(var)
结果
<tf.Variable 'test/net1/weights:0' shape=(5, 5, 1, 32) dtype=float32_ref> <tf.Variable 'test/net1/biases:0' shape=(32,) dtype=float32_ref> <tf.Variable 'test/net2/weights:0' shape=(5, 5, 32, 64) dtype=float32_ref> <tf.Variable 'test/net2/biases:0' shape=(64,) dtype=float32_ref>
总结
在
net第一次调用时定义了四个变量,通过使用函数
reuse_variables(),在第二次调用
net的时候,共享了上一次的变量参数。
相关文章推荐
- Tensorflow name_scope和variable_scope的区别
- tensorflow的共享变量,tf.Variable(),tf.get_variable(),tf.Variable_scope(),tf.name_scope()联系与区别
- tensorflow的共享变量,tf.Variable(),tf.get_variable(),tf.Variable_scope(),tf.name_scope()联系与区别:
- tensorflow的共享变量,tf.Variable(),tf.get_variable(),tf.Variable_scope(),tf.name_scope()联系与区别
- 【tensorflow】scope的使用以及tf.Variable()和tf.get_variable()的区别
- 2. tensorflow学习之name/variable_scope 的使用
- TensorFlow 学习(一)—— tf.get_variable() vs tf.Variable(),tf.name_scope() vs tf.variable_scope()
- tensorflow 中 name_scope 及 variable_scope 的异同
- TensorFlow基础name_scope与variable_scope
- TensorFlow基础笔记(13) tf.name_scope tf.variable_scope学习
- tensorflow 中 name_scope和variable_scope
- tf.variable和tf.get_Variable以及tf.name_scope和tf.variable_scope的区别
- tensorflow:上下文管理器 与 name_scope, variable_scope
- TensorFlow 学习(一)—— tf.get_variable() vs tf.Variable(),tf.name_scope() vs tf.variable_scope()
- tf 共享变量 tensorflow 里面 name_scope, variable_scope
- Tensorflow学习(二) name_scope和variable_scope
- tensorflow name_scope && variable_scope
- Tensorflow函数说明(4)—— variable_scope/name_scope
- TensorFlow:作用域name_scope和variable_scope
- tensorflow里面共享变量、name_scope, variable_scope等如何理解