1

I have created a Keras LSTM model that does sequence classification. I have 27 sequences in the Training set and 18 sequences in the Test set. Each sequence has 4000 time-steps that I have achieved by padding with zeroes. Each sequence is a combination of 2499 parallel series. This means I have 2499 Features.

  • Dimensions of X_Train is (27 x 4000 x 2499) :27-Sequences, 4000 Timesteps in each sequence and 2499 features.
  • Dimension of Y_Train is (27 x 4000 x 1)
  • Dimension of X_Test is (18 x 4000 x 2499)
  • Dimension of Y_Test is (18 x 4000 x 1)

I am using Bi-directional LSTM Model with return_sequences set to True

My ultimate goal is to get Feature Importances using ELI5 Package's Permutation Importance.

Since ELI5 package does not support Keras framework, I want to use a Scikit learn Wrapper around keras to get it to behave like scikit learn.

Then I can finally use ELI5 package on my model to get Important features.

I have used these parameters:

  • layer1_units = 40
  • layer1_act = 'tanh'
  • go_backwards = False
  • return_sequences = True
  • merge_mode = 'concat'
  • lr = 0.01
  • epochs = 2
  • batch_size = 200

I am going to use this lstm model in the build_fn attribute of KerasClassifier().

model = KerasClassifier(build_fn= lstm_Trial.model(),  epochs=3, batch_size=40, verbose=1)

Then I am trying to use .fit() method.

model.fit(x = X_Train, y = Y_Train_Ori)

This throws an error.


ValueError Traceback (most recent call last) in () ----> 1 model.fit(x = X_Train, y = Y_Train_Ori) ~/anaconda3/lib/python3.6/site-packages/keras/wrappers/scikit_learn.py in fit(self, x, y, sample_weight, **kwargs) 203 y = np.searchsorted(self.classes_, y) 204 else: --> 205 raise ValueError('Invalid shape for y: ' + str(y.shape)) 206 self.n_classes_ = len(self.classes_) 207 if sample_weight is not None:

ValueError: Invalid shape for y: (27, 4000, 1)

How do I use KerasClassifier properly so that Ultimately I am able to use ELI5 package?

3
  • 1
    Can you please reduce your problem to an MCVE? There seems to be a lot of unnecessary information which makes it hard to debug. Try to replicate the problem in the simplest way possible. Commented Oct 18, 2018 at 17:15
  • Thank you for your response. I will do that. Commented Oct 20, 2018 at 20:48
  • I also am wondering how this will be done. LSTM's require you reshape your input data, ideally using keras.preprocessing.timeseries_dataset_from_array Each sample input requires a sequence length of past data for it to work. At the moment, I wrote a custom wrapper instead of using scikeras, which on fit/predict will perform the above transform of the X input so it will work for training (at the moment using fixes tscv 5 fold, first 4 for training last 1 for validation) and stopping when the validation does not improve. Commented Oct 28, 2024 at 2:57

1 Answer 1

0

I have encountered the same problem and to avoid double-posting, I would like to share with you my answer which I have posted on another Stackoverflow question similar to yours.

TL;DR: eli5 requires 2d arrays but LSTM layers need 3d arrays, so you cannot get it to work currently

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.