Python keras 如何在卷积层后调整输入大小以进入LSTM层

我在卷积层和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:目标形状。整数元组,不包括样本维度(批量大小)。

Related Posts

使用LSTM在Python中预测未来值

这段代码可以预测指定股票的当前日期之前的值,但不能预测…

如何在gensim的word2vec模型中查找双词组的相似性

我有一个word2vec模型,假设我使用的是googl…

dask_xgboost.predict 可以工作但无法显示 – 数据必须是一维的

我试图使用 XGBoost 创建模型。 看起来我成功地…

ML Tuning – Cross Validation in Spark

我在https://spark.apache.org/…

如何在React JS中使用fetch从REST API获取预测

我正在开发一个应用程序,其中Flask REST AP…

如何分析ML.NET中多类分类预测得分数组?

我在ML.NET中创建了一个多类分类项目。该项目可以对…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注