有没有办法将Conv2D层与LSTM结合起来?

时间:2019-03-22 20:54:15

标签: python-3.x keras lstm

我正在使用Conv2D和LSTM层建立一个keras模型,并尝试以下代码..我试图不对LSTM层进行整形,但是它也给我一个错误,即索引超出范围。我进行了很多搜索,但我不知道问题出在哪里或如何解决 CNN模型的输入图像为128 * 128

我更新了代码

num_steps = 50
lats = 128
lons = 128
features = 4
out_feats = 3

model = Sequential()
model.add(TimeDistributed(Conv2D(16, (3, 3), activation='relu', padding='same'), 
                          input_shape=(1,128, 128, 3)))

model.add(TimeDistributed(MaxPooling2D(pool_size=(2, 2))))
model.add(TimeDistributed(Conv2D(32, (3, 3), activation='relu', padding='same')))
model.add(TimeDistributed(MaxPooling2D(pool_size=(2, 2))))
model.add(TimeDistributed(Conv2D(32, (3, 3), activation='relu', padding='same')))
model.add(TimeDistributed(MaxPooling2D(pool_size=(2, 2))))
model.add(TimeDistributed(Flatten()))
model.add(LSTM(units=64, return_sequences=True))
model.add(TimeDistributed(Reshape((8, 8, 1))))
model.add(TimeDistributed(UpSampling2D((2,2))))
model.add(TimeDistributed(Conv2D(32, (3,3), activation='relu', padding='same')))
model.add(TimeDistributed(UpSampling2D((2,2))))
model.add(TimeDistributed(Conv2D(32, (3,3), activation='relu', padding='same')))
model.add(TimeDistributed(UpSampling2D((2,2))))
model.add(TimeDistributed(Conv2D(16, (3,3), activation='relu', padding='same')))
model.add(TimeDistributed(UpSampling2D((2,2))))
model.add(TimeDistributed(Conv2D(out_feats, (3,3), padding='same')))
model.compile(optimizer='adadelta', loss='categorical_crossentropy', metrics=['accuracy'])
model.summary()

X_data = np.array(X_data)
X_datatest = np.array(X_datatest)

hist=model.fit(X_data, X_data,epochs=15,batch_size=128,verbose = 2,validation_data=(X_datatest, X_datatest))

但是它给了我以下错误

  

回溯(最近通话最近):文件   “ C:\ Users \ bdyssm \ Desktop \ Master \ LSTMCNN2.py”,第107行,在       hist = model.fit(X_data,X_data,epochs = 15,batch_size = 128,verbose = 2,validation_data = {X_datatest,X_datatest))文件   “ C:\ Users \ bdyssm \ AppData \ Local \ Programs \ Python \ Python35 \ lib \ site-packages \ keras \ engine \ training.py”,   线952,适合       batch_size =批处理大小)文件“ C:\ Users \ bdyssm \ AppData \ Local \ Programs \ Python \ Python35 \ lib \ site-packages \ keras \ engine \ training.py”,   _standardize_user_data中的第751行       exception_prefix ='input')文件“ C:\ Users \ bdyssm \ AppData \ Local \ Programs \ Python \ Python35 \ lib \ site-packages \ keras \ engine \ training_utils.py”,   第128行,位于standardize_input_data中       'with shape'+ str(data_shape))ValueError:检查输入时出错:预期的time_distributed_1_input具有5个维度,但得到了   形状为(2892、128、128、3)的数组

这是模型摘要 enter image description here

0 个答案:

没有答案