0

I am implementing a lstm model with keras.

There are 11200 lines in my dataset and 5 columns. Each data is a vector. The shape of the dataset is (11200, 5, 54) and it looks like this.

col1 col2 col3 col4 col5

[1,3,...,-999] [2,4,...,-999] [3,4,...,-999] [5,6,...,-999] [4,5,...,-999]

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

...

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

The length of each vector like this one [1,3,...,-999] is 54.

The target is a boolean vector of size (11200, 1) like this

1      T

2      F

...    ...

11200  F   

I created my model like this:

X_train, X_test, y_train, y_test = train_test_split(data,target, test_size=0.2, random_state=1)  
batch_size = 32 
timesteps = None 
output_size = 1
epochs=120

inputs = Input(batch_shape=(batch_size, timesteps, output_size))
lay1 = LSTM(20, stateful=True, return_sequences=True)(inputs)
output = Dense(units = output_size)(lay1)
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 problem is I have this error :

ValueError: Error when checking input: expected input_1 to have shape (None, 1) but got array with shape (5, 54).

What is wrong? The input or the output ? How can I feed the wrong one ?

Thanks.

5
  • you have timesteps = None but you feed 5 to it, try to change timesteps to 0 Commented May 16, 2018 at 6:58
  • I did not feed anything. This is my previous question : stackoverflow.com/questions/50331324/…. Commented May 16, 2018 at 7:05
  • Ok I did what you told me but now I have this error : ValueError: slice index 0 of dimension 0 out of bounds. for 'lstm_1/strided_slice_13' (op: 'StridedSlice') with input shapes: [0,30,1], [1], [1], [1] and with computed input tensors: input[1] = <0>, input[2] = <1>, input[3] = <1>. Commented May 16, 2018 at 7:08
  • inputs = Input(batch_shape=(batch_size, timesteps, output_size)) which is the same as inputs = Input(batch_shape=(32, None, 1)) Try changing Timesteps to 5, and output_size to 54. Though why you have output_size as input, I'm not sure. Commented May 16, 2018 at 8:31
  • I use a tutorial from coursera and the link to the code is: github.com/romeokienzler/developerWorks/blob/master/coursera/ai/…. Yes I did try with timesteps = 5 but I still have ValueError: Error when checking input: expected input_1 to have shape (5, 1) but got array with shape (5, 54). So I think I need to reshape my output vector. I was thinking with an array of 54 (0,..,0,1) for True and (0,..,0,0) for False. But it does not feel like a good idea. Commented May 16, 2018 at 8:35

1 Answer 1

1

The problem is in the shape of the Input layer:

inputs = Input(batch_shape=(batch_size, timesteps, output_size)) # output_size = 1

where you need it the final dimension to be 54 instead of output_size so batch_shape=(batch_size, timesteps, 54). Having timesteps=None is not a problem, if you fix to 5 it will allow you to optionally unroll the LSTM which might speed up computation. Otherwise, it says for some unknown number of timesteps, in your case it is 5.

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

5 Comments

Sorry but I don't understand what needs to be 54. What do I need to change ?
Apologies, I've notices that is unclear. I've updated the answer.
Thanks I got it ! But do I need to reshape the output ('target' in my case) ? Because its shape is (11200,1). And now I have another issue : ValueError: Error when checking target: expected dense_1 to have 3 dimensions, but got array with shape (11200, 1).
Ah, that is because you return_sequence=True which returns LSTM output at every timestep. Presumably you don't want and want the LSTM to summarise the input into a single vector.
Also, are you confident that you need stateful=True? Only used if your data is linked across batches, often it is not necessary to use stateful.

Your Answer

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