根据Tensorflow文件(
https://www.tensorflow.org/api_docs/python/tf/nn/dynamic_rnn
)
即。,
[批次大小,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的输出如下:,
[批处理大小、N\u时间步长、输出大小]
. 因此,每个时间步长都有一个输出。在上面的代码中,我只得到最后一个时间步骤的输出。或者,您可以考虑一种不同的rnn输出架构,如下所述(
How do we use LSTM to classify sequences?
),
希望这有帮助。