使用TensorArray和while_loop执行动态rnn时发生错误

时间:2017-02-13 03:00:29

标签: python tensorflow

我的目的是做CNN(VGG16),然后将每个帧的输出推送到两层lstm。但是,当我尝试使用sess.run()时发生错误。这表明图形构造正确。那么我的虫子在哪里? 这是错误信息。

tensorflow.python.framework.errors_impl.InvalidArgumentError: The node 'total_loss' has inputs from different frames. The input 'vgg_16/fc8/weights/Regularizer/l2_regularizer' is in frame 'while/while/'. The input 'Mean' is in frame ''.

这是我的一些代码:

lstm=tf.nn.rnn_cell.BasicLSTMCell(128)
cell=tf.nn.rnn_cell.MultiRNNCell([lstm]*2)
state=cell.zero_state(1, tf.float32)

inputImgA=tf.TensorArray(tf.string, length)
outputLSTM=tf.TensorArray(tf.float32, length)
lossLSTM=tf.TensorArray(tf.float32, length)

img=sequence_parsed['imgs_list']
inputImgA=inputImgA.unpack(img)

i=tf.constant(0)


def cond(i, state, inputImgA, outputLSTM, lossLSTM):
    return tf.less(i, length)

def body(i, state, inputImgA, outputLSTM, lossLSTM):
    imcontent=inputImgA.read(i)
    image=tf.image.decode_jpeg(imcontent, 3, name='decode_image')
    with tf.variable_scope('Image_Process'):
        image=tf.image.resize_images(image, [224, 224])
        channels = tf.split(2, 3, image)
        channels[0] -= _R_MEAN
        channels[1] -= _G_MEAN
        channels[2] -= _B_MEAN
        image=tf.concat(2, channels)
        images=tf.expand_dims(image, 0)

    net, end = VggNet(images, is_training=True)
    output, state=cell(net, state)
    outputLSTM=outputLSTM.write(i, output)

    loss=tf.nn.sparse_softmax_cross_entropy_with_logits(output, label)
    lossLSTM=lossLSTM.write(i, loss)
    return (i+1, state, inputImgA, outputLSTM, lossLSTM) 


_, _, _, outputLSTM, lossLSTM=tf.while_loop(cond, body, [i, state, inputImgA, outputLSTM, lossLSTM])

output=outputLSTM.pack()
loss=lossLSTM.pack()
loss=tf.reduce_mean(loss)
losses.add_loss(loss)
total_loss=losses.get_total_loss()

1 个答案:

答案 0 :(得分:1)

问题解决了。看起来如果在while循环中定义VggNet,则必须在循环内计算损失。 这是更改后的代码:

lstm=tf.nn.rnn_cell.BasicLSTMCell(128)
cell=tf.nn.rnn_cell.MultiRNNCell([lstm]*2)
state=cell.zero_state(1, tf.float32)

inputImgA=tf.TensorArray(tf.string, length)
outputLSTM=tf.TensorArray(tf.float32, length)
lossLSTM=tf.TensorArray(tf.float32, length)

img=sequence_parsed['imgs_list']
inputImgA=inputImgA.unpack(img)

i=tf.constant(0)


def cond(i, state, inputImgA, outputLSTM, lossLSTM):
    return tf.less(i, length)

def body(i, state, inputImgA, outputLSTM, lossLSTM):
    imcontent=inputImgA.read(i)
    image=tf.image.decode_jpeg(imcontent, 3, name='decode_image')
    with tf.variable_scope('Image_Process'):
        image=tf.image.resize_images(image, [224, 224])
        channels = tf.split(2, 3, image)
        channels[0] -= _R_MEAN
        channels[1] -= _G_MEAN
        channels[2] -= _B_MEAN
        image=tf.concat(2, channels)
        images=tf.expand_dims(image, 0)

    net, end = VggNet(images, is_training=True)
    output, state=cell(net, state)
    outputLSTM=outputLSTM.write(i, output)

    loss2=tf.get_collection(tf.GraphKeys.REGULARIZATION_LOSSES)
    loss2=tf.add_n(loss2)
    loss1=tf.nn.sparse_softmax_cross_entropy_with_logits(output, label)
    lossLSTM=lossLSTM.write(i, loss1+loss2)
    return (i+1, state, inputImgA, outputLSTM, lossLSTM) 


_, _, _, outputLSTM, lossLSTM=tf.while_loop(cond, body, [i, state, inputImgA, outputLSTM, lossLSTM])

output=outputLSTM.pack()
total_loss=lossLSTM.pack()
total_loss=tf.reduce_mean(total_loss)