在Tensorflow中将CNN输出传递给LSTM?
假设CNN的输出形状为在Tensorflow中将CNN输出传递给LSTM?,tensorflow,lstm,convolution,Tensorflow,Lstm,Convolution,假设CNN的输出形状为[批次大小、高度、宽度、通道数](假设格式为通道数最后一次),我有以下方法将CNN维度转换为RNN维度: def collapse_to_rnn_dims(inputs): batch_size, height, width, num_channels = inputs.get_shape().as_list() if batch_size is None: batch_size = -1 return tf.reshape(inpu
[批次大小、高度、宽度、通道数]
(假设格式为通道数最后一次
),我有以下方法将CNN维度转换为RNN维度:
def collapse_to_rnn_dims(inputs):
batch_size, height, width, num_channels = inputs.get_shape().as_list()
if batch_size is None:
batch_size = -1
return tf.reshape(inputs, [batch_size, width, height * num_channels])
它确实有效。然而,我只是想问一下,这是否真的是重塑CNN输出的正确方法,以便它们可以传递到LSTM层。我找到了一个答案,它与我在手写文本识别中所做的完全相同,尽管这个答案假设时间步数(宽度)是动态的,而不是批大小
shape = cnn_net.get_shape().as_list() # [batch, height, width, features]
transposed = tf.transpose(cnn_net, perm=[0, 2, 1, 3],
name='transposed') # [batch, width, height, features]
conv_reshaped = tf.reshape(transposed, [shape[0], -1, shape[1] * shape[3]],
name='reshaped') # [batch, width, height x features]
您可以简单地采用重塑操作: