了解将密集层连接到LSTM

时间:2018-07-10 14:20:44

标签: python machine-learning keras

我无法正确地将密集层连接到LSTM层。我的Y值范围是0-1,所以S型对我来说似乎很合逻辑。

我得到了错误:

  

检查目标时出错:预期density_4具有2维,但是   得到了形状为(993,300,1)的数组

在我看来,我输入的形状正确的总DF为(350700,2413),我将其重塑为(1169,300,2413)//不包括Y值。我只是似乎无法弄清楚如何使致密层正常工作并将S形曲线应用于我的Y。

通过火车测试拆分,我的y_train为(993,300,1),这是我的错误的主要问题,但是我似乎无法理解自己做错了什么。 x_train是(933,300,2413)x_test =(176,300,2413)y_test =(176,300,1)

这是我建立的网络。后端张量流(也使用theano相同的问题)

_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
lstm_21 (LSTM)               (None, 300, 1000)         13656000  
_________________________________________________________________
lstm_22 (LSTM)               (None, 300, 500)          3002000   
_________________________________________________________________
lstm_23 (LSTM)               (None, 300, 250)          751000    
_________________________________________________________________
lstm_24 (LSTM)               (None, 300, 100)          140400    
_________________________________________________________________
lstm_25 (LSTM)               (None, 50)                30200     
_________________________________________________________________
dense_4 (Dense)              (None, 1)                 51        
=================================================================
Total params: 17,579,651
Trainable params: 17,579,651
Non-trainable params: 0
_________________________________________________________________

这是我的代码。

import numpy as np
import pandas as pd
from keras.models import Sequential
from keras.layers.advanced_activations import LeakyReLU
from keras.layers import Dense, Activation, LSTM, Flatten
from keras import backend as K
from sklearn.model_selection import train_test_split

aa = aa[np.isfinite(aa['Y1'])]
aa=aa[-350700:]

Y=aa['Y1'].values.reshape(1169,300,1) #break into 1169 samples @ 300 timestamps
aa.drop(drop1, axis=1, inplace=True) #drop the Y1 feature and others not needed.


features=aa.shape[1]

X=aa.values.reshape(1169,300,features)

seed = 7

X_train, X_test, y_train, y_test = train_test_split(X, Y, test_size=0.15, random_state=seed)

model = Sequential()
model.add(LSTM(1000, input_shape=(300,features),activation='relu',return_sequences=True))
model.add(LSTM(500,activation='relu',return_sequences=True))
model.add(LSTM(250,activation='relu',return_sequences=True))
model.add(LSTM(100, activation='relu',return_sequences=True))
model.add(LSTM(50,activation='relu',return_sequences=False))
model.add(Dense(1,activation='sigmoid'))
model.compile(loss='mae',
              optimizer='adam',
              metrics=['mse', 'mae', 'mape'])

print(model.summary())


# evaluate model with standardized dataset
model.fit(X_train, y_train, validation_data=(X_test,y_test), epochs=15000)

1 个答案:

答案 0 :(得分:1)

您的“数据”与您的“最后一层形状”不兼容。

  • 您需要Y_train的形状为(993,1)-对整个序列进行分类
  • 或者您需要将return_sequences=True保留在“所有” LSTM层中-对每个时间步进行分类

正确的方法取决于您要执行的操作。