我在卷积层和LSTM层之间的连接上遇到了问题。我的数据形状为(75,5),其中有75个时间步,每个时间步有5个数据点。我想对(75×5)进行卷积,得到新的卷积后的(75×5)数据,然后将这些数据输入到LSTM层。然而,这行不通,因为卷积层的输出形状包含我不需要的过滤器数量。因此,卷积层的输出形状为(1,75,5),而LSTM层需要的输入形状为(75,5)。我如何只取第一个过滤器呢?
model = Sequential() model.add(Convolution2D(1, 5,5,border_mode='same',input_shape=(1,75, 5)))model.add(Activation('relu'))model.add(LSTM(75, return_sequences=False, input_shape=(75, 5)))model.add(Dropout(0.5))model.add(Dense(1))model.compile(loss='mse', optimizer='rmsprop')
这是出现的错误信息:
File "/usr/local/lib/python3.4/dist-packages/keras/layers/recurrent.py", line 378, in __init__super(LSTM, self).__init__(**kwargs)File "/usr/local/lib/python3.4/dist-packages/keras/layers/recurrent.py", line 97, in __init__super(Recurrent, self).__init__(**kwargs)File "/usr/local/lib/python3.4/dist-packages/keras/layers/core.py", line 43, in __init__self.set_input_shape((None,) + tuple(kwargs['input_shape']))File "/usr/local/lib/python3.4/dist-packages/keras/layers/core.py", line 138, in set_input_shape', was provided with input shape ' + str(input_shape))Exception: Invalid input shape - Layer expects input ndim=3, was provided with input shape (None, 1, 75, 5)
回答:
你可以在中间添加一个Reshape()层来使维度兼容。
http://keras.io/layers/core/#reshape
keras.layers.core.Reshape(dims)
将输出重塑为特定形状。
输入形状
任意,尽管输入形状中的所有维度必须是固定的。在模型中使用此层作为第一层时,使用关键字参数input_shape(整数元组,不包括样本轴)。
输出形状
(batch_size,) + dims
参数
dims
:目标形状。整数元组,不包括样本维度(批量大小)。