1

I am trying to implement a lstm model with keras. The problem is that I have data of different shapes. My data looks like this:

col1 col2 col3 col4 col5

[1,2,3] [2,3,4] [3,4,5] [5,6,7] [4,5,9]

[0,2] [1,5] [1,24] [11,7] [-1,4]

[0,2,4,5] [1,5,7,8] [1,24,-7,6] [11,7,4,5] [-1,4,1,2]

My code is

import numpy as np
import pandas as pd
import h5py
from sklearn.model_selection import train_test_split
from keras.layers import Dense
from keras.layers import Input, LSTM
from keras.models import Model

X_train, X_test, y_train, y_test = train_test_split(X, y_target, test_size=0.2, random_state=1)  
batch_size = 32 
timesteps = 300 
output_size = 1
epochs=120

inputs = Input(batch_shape=(batch_size, timesteps, output_size))
lay1 = LSTM(10, stateful=True, return_sequences=True)(inputs)
lay2 = LSTM(10, stateful=True, return_sequences=True)(lay1)
output = Dense(units = output_size)(lay2)

regressor = Model(inputs=inputs, outputs = output)
regressor.compile(optimizer='adam', loss = 'mae')
regressor.summary()

for i in range(epochs):
    print("Epoch: " + str(i))
    regressor.fit(X_train, y_train, shuffle=False, epochs = 1, batch_size = batch_size)
    regressor.reset_states()

The error I have when I run the code is :

ValueError: Error when checking input: expected input_5 to have 3 dimensions, but got array with shape (11200, 5) #11200 lines, 5 columns

Thanks

1 Answer 1

0

A multidimensional numpy array need have a clear shape so putting array of different length inside the same numpy array will result in a numpy array of objects instead odf the desired multidimension array.

So basically it's not possible to feed your data to keras in one go.

However there are several possible solutions. Most of them require that your keras input shape has to be None in your timestep dimension:

  1. Use padding that your data always has the same shape
  2. train with batch_size = 1
  3. Sort your data in batches in a way that inside each batch every sample has the same shape.

The last two options require the usage of the fit_generator option, because you have to feed the data step wise.

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

4 Comments

Hi dennis-ec, Thank you. I will go for padding as the second and third options are not possible for me. Do I still need to put timesteps=None if I use padding?
No, because then you have a fix length, but it won't make a difference either way.
No, if you use padding you can have fixed time steps (but it doesn't make a difference). You should consider using either a symbol that identifies the end of the sequence (and pad with that symbol), and also consider adding a Masking layer to skip calculations for these symbols.
Thank you for mentionning Masking. I didn't know about it, I have just checked and it looks pretty great.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.