【发布时间】:2019-05-23 03:07:12
【问题描述】:
我想将几个不同的输入传递到可重用的 tensorflow 架构(解码器)中。为此,我使用了一个 for 循环,在该循环中我将输入输入到模型中。但是,我没有重用层变量,而是为每个循环迭代创建变量。假设这段代码:
import tensorflow as tf
for i in range(5):
decoder(input=input, is_training=is_training)
当解码器是:
def decoder(self, input, is_training):
with tf.variable_scope("physics", reuse=tf.AUTO_REUSE):
latent = tf.expand_dims(latent, axis=1)
latent = tf.expand_dims(latent, axis=1)
x = latent
""" Layer 1 """
x = tf.layers.conv2d_transpose(x, filters=256, kernel_size=2, strides=1, activation='relu', padding='valid', name="transpose1_1", reuse=tf.AUTO_REUSE)
x = tf.layers.batch_normalization(x, training=is_training, name="transpose_bn_1_1")
""" Layer 2 """
x = tf.layers.conv2d_transpose(x, filters=256, kernel_size=2, strides=2, activation='relu', padding='valid', name="transpose1_2", reuse=tf.AUTO_REUSE)
x = tf.layers.batch_normalization(x, training=is_training, name="transpose_bn_1_2")
...
如果我现在使用
在循环后立即输出变量from pprint import pprint
pprint([n.name for n in tf.get_default_graph().as_graph_def().node])
我得到以下输出,表明我没有在循环迭代之间共享我的变量:
'physics/transpose1_1/kernel/Initializer/random_uniform/shape',
'physics/transpose1_1/kernel/Initializer/random_uniform/min',
'physics/transpose1_1/kernel/Initializer/random_uniform/max',
'physics/transpose1_1/kernel/Initializer/random_uniform/RandomUniform',
'physics/transpose1_1/kernel/Initializer/random_uniform/sub',
'physics/transpose1_1/kernel/Initializer/random_uniform/mul',
'physics/transpose1_1/kernel/Initializer/random_uniform',
'physics/transpose1_1/kernel',
'physics/transpose1_1/kernel/Assign',
'physics/transpose1_1/kernel/read',
'physics/transpose1_1/bias/Initializer/zeros',
'physics/transpose1_1/bias',
'physics/transpose1_1/bias/Assign',
'physics/transpose1_1/bias/read',
'physics/transpose1_1/Shape',
'physics/transpose1_1/strided_slice/stack',
'physics/transpose1_1/strided_slice/stack_1',
'physics/transpose1_1/strided_slice/stack_2',
'physics/transpose1_1/strided_slice',
'physics/transpose1_1/strided_slice_1/stack',
'physics/transpose1_1/strided_slice_1/stack_1',
'physics/transpose1_1/strided_slice_1/stack_2',
'physics/transpose1_1/strided_slice_1',
'physics/transpose1_1/strided_slice_2/stack',
'physics/transpose1_1/strided_slice_2/stack_1',
'physics/transpose1_1/strided_slice_2/stack_2',
'physics/transpose1_1/strided_slice_2',
'physics/transpose1_1/mul/y',
'physics/transpose1_1/mul',
'physics/transpose1_1/add/y',
'physics/transpose1_1/add',
'physics/transpose1_1/mul_1/y',
'physics/transpose1_1/mul_1',
'physics/transpose1_1/add_1/y',
'physics/transpose1_1/add_1',
'physics/transpose1_1/stack/3',
'physics/transpose1_1/stack',
'physics/transpose1_1/conv2d_transpose',
'physics/transpose1_1/BiasAdd',
'physics/transpose1_1/Relu',
...
'physics_4/transpose1_1/Shape',
'physics_4/transpose1_1/strided_slice/stack',
'physics_4/transpose1_1/strided_slice/stack_1',
'physics_4/transpose1_1/strided_slice/stack_2',
'physics_4/transpose1_1/strided_slice',
'physics_4/transpose1_1/strided_slice_1/stack',
'physics_4/transpose1_1/strided_slice_1/stack_1',
'physics_4/transpose1_1/strided_slice_1/stack_2',
'physics_4/transpose1_1/strided_slice_1',
'physics_4/transpose1_1/strided_slice_2/stack',
'physics_4/transpose1_1/strided_slice_2/stack_1',
'physics_4/transpose1_1/strided_slice_2/stack_2',
'physics_4/transpose1_1/strided_slice_2',
'physics_4/transpose1_1/mul/y',
'physics_4/transpose1_1/mul',
'physics_4/transpose1_1/add/y',
'physics_4/transpose1_1/add',
'physics_4/transpose1_1/mul_1/y',
'physics_4/transpose1_1/mul_1',
'physics_4/transpose1_1/add_1/y',
'physics_4/transpose1_1/add_1',
'physics_4/transpose1_1/stack/3',
'physics_4/transpose1_1/stack',
'physics_4/transpose1_1/conv2d_transpose',
'physics_4/transpose1_1/BiasAdd',
'physics_4/transpose1_1/Relu',
这里发生了什么? tf.AUTO_REUSE 标志不应该允许我在i==0 和所有迭代i>0 重用我的变量时首先初始化我的decoder 吗?我的解码器中的每一层都会出现上述情况。
我使用的是TensorFlow版本1.12.0。
谢谢。
【问题讨论】:
标签: python tensorflow scope conv-neural-network