tensorflow参数共享
要用到额外的函数:tf.trainable_variables(), 它能够将我们定义的所有的 trainable=True 的所有变量以一个list的形式返回
tensorflow三种创建变量的方式:
1 tf.placeholder方法
v1 = tf.placeholder(dtype= tf.float32 , shape = (3,3))
print(v1.name) >>> Placeholder:0
v1 = tf.placeholder(tf.float32,shape = (3,3),name = 'ppp')
print(v1.name) >>> ppp:0
v1 = tf.placeholder(tf.float32,shape = (3,3),name = 'ppp')
print(v1.name) >>> ppp_1:0 若重名会自动加下划线1,2,3处理
2 tf.Variable
v2 = tf.Variable([3,3],dtype = tf.float32)
前面写shape,直接括号不要shape=
print(v2.name) >>> Variable_1:0
v2 = tf.Variable([3,3],dtype = tf.float32,name = 'www')
print(v2.name) >>>www:0
v2 = tf.Variable([3,3],dtype = tf.float32,name = 'www')
print(v2.name) >>>www_1:0 和placeholder一样,若重名自动加下划线123
3 tf.get_variable # v小写
tf.get_variable方法一定要写name
v3 = tf.get_variable(name = 'vvv',shape = [3,3],dtype = tf.float32,)
print(v3.name) >>> vvv:0
v3 = tf.get_variable(name = 'vvv',shape = [4,4],dtype = tf.float32,)
print(v4.name) #若出现重名则会报错
比较name_scope和variable_scope
with tf.name_scope('nsc1'):
v1 = tf.Variable([1], name='v1')
with tf.variable_scope('vsc1'):
v2 = tf.Variable([1], name='v2')
v3 = tf.get_variable(name='v3', shape=[])
print(v1.name) >>> nsc1/v1:0
print(v2.name) >>> nsc1/vsc1/v2:0
print(v3.name) >>> vsc1/v3:0
with tf.name_scope('nsc1'):
v4 = tf.Variable([1], name='v4')
print(v4.name) >>>nsc1_1/v4:0 # nsc1会自动加下划线123...
with tf.variable_scope('vsc1'):
v5 = tf.Variable([1], name='v5')
tf.name_scope() 并不会对 tf.get_variable() 创建的变量有任何影响。 换言之,在tf.name_scope() 下使用tf.get_variable() 会报错!
tf.name_scope() 主要是用来管理命名空间的,这样子让我们的整个模型更加有条理。而 tf.variable_scope() 的作用是为了实现变量共享,它和 tf.get_variable() 来完成变量共享的功能。
---------------------
理解不是很清楚往下看!!!!!!!!!!!
import tensorflow as tf
def my_image_filter():
conv1_weughts = tf.Variable(tf.random_normal([5,5,32,32]),name = 'conv1_weights')
conv1_biases = tf.Variable(tf.zeros([32]),name = 'conv1_biases')
conv2_weughts = tf.Variable(tf.random_normal([6,5,32,32]),name = 'conv2_weights')
conv2_biases = tf.Variable(tf.zeros([32]),name = 'conv2_biases')
return None
result1 = my_image_filter()
result2 = my_image_filter()
vs = tf.trainable_variables()
>>> 结果输出有8个变量,result1里面conv1/weights、conv1/biases、conv2/weights、conv2/biases
result2里面conv1/weights_1、conv1/biases_1、conv2/weights_1、conv2/biases_1
print(len(vs)) >>> 8
for i in vs:
print(i)
`````````````````````````````````````
import tensorflow as tf
def convv(kernel_shape,biases_shape):
weights = tf.get_variable('weight',kernel_shape,initializer = tf.random_normal_initializer())
biases = tf.get_variable('biases',biases_shape,initializer = tf.constant_initializer())
return None
def my_image_filter1():
with tf.variable_scope('conv1'):
result1 = convv([5,5,32,32],[32])
with tf.variable_scope('conv2'):
result2 = convv([5,5,32,32],[32])
with tf.variable_scope('myyyy') as scope:
result1 = my_image_filter1()
scope.reuse_variables()
result2 = my_image_filter1()
vs = tf.trainable_variables()
print(len(vs)) >>> 4
for i in vs:
print(i)
>>>myyyy/conv1/weights、
myyyy/conv1/biases、
myyyy/conv2/weights、
myyyy/conv2/biases # tf.get_variable和tf.variable_scope实现了result1和result2的参数共享