I have data in a dataframe named
ddf as follows:
labels X L1 [1,2,3,7,8,9...] L1 [4,2,6,9,8,7...] ... L2 [5,6,8,9,6,3...] L2 [7,8,5,6,9,0...] ...
There are 250 rows, 7 labels and 2000 elements in every list under X. These 2000 elements are values of a signal over a period of about 60 seconds.
I am trying to build a recurrent neural network for above data. Following is my code:
Xall = ddf['X'].values Xall = np.array(Xall) ydf = pd.get_dummies(ddf.drop('X', axis=1)) Yall = np.array(ydf.values) # Split the data from sklearn.model_selection import train_test_split X_train, X_test, Y_train, Y_test = train_test_split(Xall, Yall, test_size=0.1, random_state=0) from keras.models import Sequential from keras.layers import Embedding, LSTM, Dense model_lstm = Sequential() model_lstm.add(Embedding(2000, 128)) model_lstm.add(LSTM(100, dropout=0.2, recurrent_dropout=0.2)) model_lstm.add(LSTM(200, dropout=0.2, recurrent_dropout=0.2)) model_lstm.add(Dense(Yall.shape, activation='softmax')) model_lstm.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy']) model_lstm.fit(X_train, Y_train, epochs=50, verbose=True, validation_data=(X_test, Y_test))
However, I am getting error at second LSTM layer:
ValueError: Input 0 is incompatible with layer lstm_2: expected ndim=3, found ndim=2
I think this has something to do with LSTM arguments. Also are arguments of Embedding layer OK? How are both these adjusted? Where is the error coming from and how can it be solved? Thanks for your help.