代码之家  ›  专栏  ›  技术社区  ›  Yes92

卷积后的LSTM单元

  •  0
  • Yes92  · 技术社区  · 8 年前

    我需要在两个卷积层之后实现一个LSTM层。这是我在第一次卷积之后的代码:

    convo_2 = convolutional_layer(convo_1_pooling, shape=[5, 5, 32, 64])
    convo_2_pooling = max_pool_2by2(convo_2)
    convo_2_flat = tf.reshape(convo_2_pooling, shape=[-1, 64 * 50 * 25])
    cell = rnn.LSTMCell(num_units=100, activation=tf.nn.relu)
    cell = rnn.OutputProjectionWrapper(cell, output_size=7)
    conv_to_rnn = int(convo_2_flat.get_shape()[1])
    outputs, states = tf.nn.dynamic_rnn(cell, convo_2_flat, dtype=tf.float32)
    

    我在最后一行得到这个错误:

    ValueError: Shape (?, 50, 64) must have rank 2
    

    我必须指出进入 convo_2_flat 变量,对吗?怎样我真的不知道该怎么做。


    重塑后:

     convo_2_flat = tf.reshape(convo_2_flat, shape=[-1, N_TIME_STEPS, INPUT_SIZE])
    

    哪里

    N_TIME_STEPS = 25
    INPUT_SIZE = int(64 * 50 * 25 / N_TIME_STEPS)
    

    我得到了这个错误:InvalidArgumentError(回溯见上文):logits和labels的大小必须相同:logits\u size=[5000,7]labels\u size=[50,7]在这一行: tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=y_true, logits=outputs)) 在我看来,批次大小在上次整形后发生了变化。

    编辑2:

    convo_2_shape = convo_2_pooling.get_shape().as_list()
    shape_convo_flat = convo_2_shape[1] * convo_2_shape[2] * convo_2_shape[3]
    N_TIME_STEPS = convo_2_shape[1]
    INPUT_SIZE = tf.cast(shape_convo_flat / N_TIME_STEPS, tf.int32)
    convo_2_out = tf.reshape(convo_2_pooling, shape=[-1, shape_convo_flat])
    convo_2_out = tf.reshape(convo_2_out, shape=[-1, N_TIME_STEPS, INPUT_SIZE])
    

    我设置 N_TIME_STEPS INPUT_SIZE tf将抛出一个错误。

    1 回复  |  直到 8 年前
        1
  •  3
  •   Nipun Wijerathne    8 年前

    根据Tensorflow文件( https://www.tensorflow.org/api_docs/python/tf/nn/dynamic_rnn )

    enter image description here

    即。, [批次大小,N\u时间步长,输入大小] . 因此,您可以重塑 Conva_2_平坦 如下所示,

    #get the shape of the output of max pooling
    shape = convo_2_pooling.get_shape().as_list()
    #flat accordingly
    convo_2_flat = tf.reshape(convo_2_pooling, [-1, shape[1] * shape[2] * shape[3]])
    
    # Here shape[1] * shape[2] * shape[3]] = N_TIME_STEPS*INPUT_SIZE
    
    #reshape according to dynamic_rnn input
    convo_2_flat = tf.reshape(convo_2_flat, shape=[-1, N_TIME_STEPS, INPUT_SIZE])
    
    outputs, states = tf.nn.dynamic_rnn(cell, convo_2_flat, dtype=tf.float32)
    
    # get the output of the last time step
    val = tf.transpose(outputs, [1, 0, 2])
    lstm_last_output = val[-1]
    
    OUTPUT_SIZE = 7 #since you have defined in cell = rnn.OutputProjectionWrapper(cell, output_size=7)
    
    W = {
            'output': tf.Variable(tf.random_normal([OUTPUT_SIZE, N_CLASSES]))
        }
    biases = {
            'output': tf.Variable(tf.random_normal([N_CLASSES]))
        }
    
    #Dense Layer
    pred_Y= tf.matmul(lstm_last_output, W['output']) + biases['output']
    #Softmax Layer
    pred_softmax = tf.nn.softmax(pred_Y)
    
    cross_entropy = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=y_true, logits=pred_softmax))
    

    关于 输出 :

    根据文件,动态rnn的输出如下:,

    enter image description here

    [批处理大小、N\u时间步长、输出大小] . 因此,每个时间步长都有一个输出。在上面的代码中,我只得到最后一个时间步骤的输出。或者,您可以考虑一种不同的rnn输出架构,如下所述( How do we use LSTM to classify sequences? ),

    希望这有帮助。

    推荐文章