简体   繁体   English

了解将密集层连接到LSTM

[英]Understanding connecting a Dense layer to LSTM

I am unable to correctly connect my dense layer to my LSTM layers. 我无法正确地将密集层连接到LSTM层。 My Y values range from 0-1 so sigmoid seems logical to me. 我的Y值范围是0-1,所以S型对我来说似乎很合逻辑。

I get the error: 我得到错误:

Error when checking target: expected dense_4 to have 2 dimensions, but got array with shape (993, 300, 1) 检查目标时出错:预期density_4具有2维,但数组的形状为(993,300,1)

To me it seems i have the input shape correct total DF is (350700 , 2413) which i reshape to a ( 1169 , 300 , 2413 ) // not including Y value. 对我来说,我似乎输入形状正确的总DF是(350700,2413),我将其整形为(1169,300,2413)//不包括Y值。 I just can't seem to figure out how to get the dense layer working and apply the sigmoid to my Y. 我只是似乎无法弄清楚如何使密集层正常工作并将S形应用于我的Y。

With the train test split i have a y_train of (993, 300, 1) which is the main issue of my error but i can't seem to understand what i have done wrong. 通过火车测试拆分,我的y_train为(993,300,1),这是我的错误的主要问题,但我似乎无法理解自己做错了什么。 x_train is ( 933, 300, 2413) x_test = ( 176, 300 , 2413) y_test= (176, 300, 1) x_train是(933,300,2413)x_test =(176,300,2413)y_test =(176,300,1)

Here is the network i have set up. 这是我建立的网络。 backend tensorflow (also used theano same issue) 后端张量流(也使用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
_________________________________________________________________

here is a my code. 这是我的代码。

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)

Your "data" is not compatible with your "last layer shape". 您的“数据”与“最后一层形状”不兼容。

  • Either you need Y_train with shape (993,1) - Classifying the entire sequence 要么你需要Y_train与形状(993,1) -判断整个序列
  • Or you need to keep return_sequences=True in "all" LSTM layers - Classifying each time step 或者您需要在“所有” LSTM层中保持return_sequences=True True-对每个时间步骤进行分类

What is correct depends you what you're trying to do. 正确的方法取决于您要执行的操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM