4

I have pandas dataframe of length 7499042 like this:

'X'          'y'
[0.1,0.2...] 0.2
[0.3,0.4,..] 0.3
.
.

each value in pandas dataframe is numpy array of length 50. Now I am extracting it like this:

input = df['X'].values

I have layers like this:

main_input = Input(shape=(50,1), name='main_input')    
lstm_out=LSTM(32,activation='tanh',recurrent_activation='sigmoid',return_sequences=True)
mean_pooling=AveragePooling1D(pool_size=2,strides=2,padding='valid')

But when I am passing my input to this while training. It is showing the error:

ValueError: Error when checking input: expected main_input to have 3 dimensions, but got array with shape (7499042, 1)

The shape of input it is showing is (7499042,). Please help me in resolving this.

2
  • 2
    Can you please list out the shapes of the numpy array you are using as input? Commented Jul 3, 2018 at 13:12
  • @VatsalAggarwal The shape of single X is (50,) and input is (7499042,) Commented Jul 3, 2018 at 13:14

1 Answer 1

1

You need to reshape your features before feeding them to the LSTM network. The LSTM layer takes a 3 dimensional input, corresponding to (batch_size, timesteps, features). This means that a single observation has to be two dimensional (timesteps, features)

In your case, a single observation is 1 dimensional (50 ,) : the whole dataset dimension is : (7499042 , 50) if the conversion was done correctly. You have to reshape your input before using it :

input = df['X'].values
input = input.reshape(input.shape[0] , input.shape[1] , 1)

in case Pandas didn't convert your initial feature to a 2d DataFrame, you have to do it before executing the above code.

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

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.