Tensorflow的参数公用,持久化与重载
1. 参数共用问题:
- tf.variable_scope(name)
- tf.get_variable(name, shape, dtype, initializer)
在搭建网络的时候,一般都会通过variable_scope(name)
来定义我们的命名域,方便图的结构展示,此时的op的名字就会变成:name/opname
,方便统一的管理。
除了命名域之外,一般还会搭配一种参数的定义方法:
tf.get_variable(name)
和tf.Variable(name)
,这两种方法的区别是get_variable()
方法对于那些名字相同的参数,当命名域scope调用reuse_variable()
的时候,不会重新生成一个变量,而是用同一个名字的参数,而tf.Variable()
则不能够重用参数
用以下的代码作为例子,假设有两层的CNN,分别有两个卷积核参数W1和W2,如果多次调用这个前向传播的网络,那么就会生成两次的卷积核(共4个)
def my_image_filter(input_images):
conv1_weights = tf.Variable(tf.random_normal([5, 5, 32, 32]),
name="conv1_weights")
conv1_biases = tf.Variable(tf.zeros([32]), name="conv1_biases")
conv1 = tf.nn.conv2d(input_images, conv1_weights,
strides=[1, 1, 1, 1], padding='SAME')
relu1 = tf.nn.relu(conv1 + conv1_biases)
conv2_weights = tf.Variable(tf.random_normal([5, 5, 32, 32]),
name="conv2_weights")
conv2_biases = tf.Variable(tf.zeros([32]), name="conv2_biases")
conv2 = tf.nn.conv2d(relu1, conv2_weights,
strides=[1, 1, 1, 1], padding='SAME')
return tf.nn.relu(conv2 + conv2_biases)
如果对上面的前向传播方法进行命名域方法改造:
def my_image_filter(input_images):
with tf.variable_scope("conv1"):
# Variables created here will be named "conv1/weights", "conv1/biases".
relu1 = conv_relu(input_images, [5, 5, 32, 32], [32])
with tf.variable_scope("conv2"):
# Variables created here will be named "conv2/weights", "conv2/biases".
return conv_relu(relu1, [5, 5, 32, 32], [32])
if __name__ =="__main__":
with tf.variable_scope("image_filters"<