您的位置:首页 > 运维架构

【Tensorflow】name_scope() 和 variable_scope() 区别

2018-03-13 20:04 411 查看
本文主要有两个重点:

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.实验三

代码1

import 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
的时候,共享了上一次的变量参数。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: