1

I'm working on time series prediction with Keras. I have 10 timesteps in my input data which is of the shape (2688, 10, 1) i.e train_x.shape and train_y.shape is (2688, 10, 1). I am getting the following error while I try to feed it into the model.

ValueError: Error when checking target: expected activation_1 to have 2 dimensions, but got array with shape (2688, 10, 1)

The input shape that I am giving to the first lstm layer: input_shape=(1, time_steps) **

Am i reshaping train_y properly?

**

    time_steps=10
    train_x = np.reshape(train_x, (train_x.shape[0], train_x.shape[1], 1))
    train_y = np.reshape(train_y, (train_y.shape[0], train_y.shape[1], 1))

    # lstm model
    model = Sequential()
    model.add(LSTM(128, input_shape=(time_steps, 1), return_sequences=True))
    model.add(LSTM(64))
    model.add(Dense(1))
    model.add(Activation('linear'))

    model.compile(loss='mse', optimizer='adam')
    history = model.fit(train_x, train_y, epochs=10, validation_data=(test_x, 
    test_y), batch_size=64, verbose=1)
4
  • Change your input shape to input_shape=(time_steps, 1) Commented Mar 4, 2019 at 5:26
  • Tried it did not work...doing that gives me the following error ValueError: Error when checking target: expected activation_1 to have 2 dimensions, but got array with shape (2688, 10, 1) Commented Mar 4, 2019 at 5:30
  • You should probably post your code then. I can't really give anything but wild guesses at this point. Commented Mar 4, 2019 at 5:30
  • I have posted the code...please take a look Commented Mar 4, 2019 at 5:36

2 Answers 2

1
train_x = np.reshape(train_x, (train_x.shape[0], train_x.shape[1], 1))
train_y = np.reshape(train_y, (train_y.shape[0], train_y.shape[1], 1)

I think the error is here, you should have given the time steps between shape[0] and shape[1] i.e...

train_x = np.reshape(train_x, (train_x.shape[0], 10,train_x.shape[1]))
train_y = np.reshape(train_y, (train_y.shape[0],10, train_y.shape[1]))

Here the value '10' denotes the time steps!

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

3 Comments

Tried it...gives me this error: ValueError: cannot reshape array of size 26880 into shape (2688,10,10)
yes when you give timesteps as 10, you should make sure the product should also add up to the same product! i.e.. suppose your original shape 2688 * 10, the reshaped matrix should be 2688*10*1. Your product turns up to 268800!! which is wrong it should only come to 26880. make your reshaped matrix such that it should come to product 26880
Yes that is what I was doing originally -> train_x = np.reshape(train_x, (train_x.shape[0], train_x.shape[1], 1)) . It still does not work
0

if you expected shape (2688, 10, 1) then it cant be input_shape=(1, time_steps). It should input_shape=(time_steps,1)

5 Comments

Tried it did not work...doing that gives me the following error ValueError: Error when checking target: expected activation_1 to have 2 dimensions, but got array with shape (2688, 10, 1)
Is it your datasets images size is 2688x10 pixel?
No, it is stock prices...i have posted the code please take a look
Please change input_shape=( 1,time_steps) and try
Does not work...gives me this error ValueError: Error when checking input: expected cu_dnnlstm_1_input to have shape (1, 10) but got array with shape (10, 1)

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.