卷积后的LSTM细胞

2024-04-18 22:24:38 发布

您现在位置:Python中文网/ 问答频道 /正文

我需要在两个卷积层之后实现一个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)

最后一行有个错误:

^{pr2}$

我必须指出convo_2_flat变量的时间步数,对吗?怎样?我真不知道该怎么做。在

编辑:
在此整形后:

^{3}$

在哪里

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

我得到了这个错误:InvalidArgumentError(请参阅上面的回溯):logits和labels的大小必须相同:logits_size=[5000,7]labels_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将抛出一个错误。在


Tags: inputsizetimetfcellnnstepsshape
1条回答
网友
1楼 · 发布于 2024-04-18 22:24:38

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

输入应为以下形状(我在这里使用默认值), enter image description here

即,【批次大小、时间步长、输入大小】。因此,您可以按如下方式重塑护送平面

#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))

关于输出的注释

根据文档,动态网络的输出如下:

enter image description here

即,[批次大小、时间步长、输出大小]。因此,每个时间步都有一个输出。在上面的代码中,我只得到最后一个时间步的输出。或者,您可以为rnn输出考虑一种不同的体系结构,其描述如下(How do we use LSTM to classify sequences?

希望这有帮助。在

相关问题 更多 >