如何在CNN和LSTM相结合的模型中拟合样本数据

时间:2019-05-25 18:42:00

标签: keras

我有一个样本页面访问803天的示例数据。我已经从均值,中位数等数据中提取了特征,数据的最终形状为(803,25)。我已经使用了640套火车和160套测试车。我正在尝试通过Keras使用CNN + LSTM模型。但是我在model.fit方法中遇到错误。

我尝试过置换图层并更改了输入形状,但仍然无法修复。

trainX.shape = (642, 1, 25)
trainY.shape = (642,)
testX.shape = (161, 1, 25) 
testY.shape = (161,)
'''python

# Basic layer

model = Sequential()
model.add(TimeDistributed(Convolution2D(filters = 32, kernel_size = (3, 3), strides=1, padding='SAME', input_shape = (642, 25, 1), activation = 'relu')))
model.add(TimeDistributed(Convolution2D(filters = 32, kernel_size = (3, 3), activation = 'relu')))
model.add(TimeDistributed(MaxPooling2D(pool_size = (2, 2))))
model.add(TimeDistributed(Convolution2D(32, 3, 3, activation = 'relu')))
model.add(TimeDistributed(MaxPooling2D(pool_size = (2, 2))))
model.add(TimeDistributed(Flatten()))
model.add(Permute((2, 3), input_shape=(1, 25)))
model.add(LSTM(units=54, return_sequences=True))

# To avoid overfitting
model.add(Dropout(0.2))  

# Adding 6 more layers
model.add(LSTM(units=25, return_sequences=True)) 
model.add(Dropout(0.2))

model.add(LSTM(units=50, return_sequences=True)) 
model.add(Dropout(0.2))

model.add(LSTM(units=50, return_sequences=True))  
model.add(Dropout(0.2))

model.add(LSTM(units=50, return_sequences=True))  
model.add(Dropout(0.2))

model.add(LSTM(units=50, return_sequences=True))  
model.add(Dropout(0.2))

model.add(LSTM(units=54))
model.add(Dropout(0.2)) 

model.add(TimeDistributed(Dense(units = 1, activation='relu', kernel_regularizer=regularizers.l1(0.0001))))

model.add(PReLU(weights=None, alpha_initializer="zero"))   # add an advanced activation

model.compile(optimizer = 'adam', loss = customSmapeLoss, metrics=['mae'])
model.fit(trainX, trainY, epochs = 50,  batch_size = 32)

predictions = model.predict(testX)  

'''


#Runtime Error
---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
<ipython-input-218-86932db86d0b> in <module>()
     42 
     43 model.compile(optimizer = 'adam', loss = customSmapeLoss, metrics=['mae'])
---> 44 model.fit(trainX, trainY, epochs = 50,  batch_size = 32)


Error - IndexError: list index out of range

1 个答案:

答案 0 :(得分:0)

input_shape与Conv2D一起使用TimeDistributed时,需要一个长度为4的元组

请参阅https://keras.io/layers/wrappers/

input_shape=(10, 299, 299, 3))

您是否认为您的数据字段太少?通常,CNN + LSTM用于具有数千个顺序图像/视频的更复杂的任务。