1
votes

I am working on a dummy example to understand how the LSTM works using Keras. I am having a problem with the way I am reshaping the data input and output.

ValueError: Input 0 is incompatible with layer recurrent: expected ndim=3, found ndim=2

import random
import numpy as np

from keras.layers import Input, LSTM, Dense
from keras.layers.wrappers import TimeDistributed
from keras.models import Model

def gen_number():
    return np.random.choice([random.random(), 1], p=[0.2, 0.8])    
truth_input = [gen_number() for i in range(0,2000)]    
# shift input by one
truth_shifted = truth_input[1:] + [np.mean(truth_input)]      
truth = np.array(truth_input)
test_ouput = np.array(truth_shifted)        
truth_reshaped = truth.reshape(1, len(truth), 1)    
shifted_truth_reshaped = test_ouput.reshape(1, len(test_ouput), 1)  
yes = Input(shape=(len(truth_reshaped),), name = 'truth_in')    
recurrent = LSTM(20, return_sequences=True, name='recurrent')(yes)    
TimeDistributed_output = TimeDistributed(Dense(1), name='test_pseudo')(recurrent)    
model_built = Model(input=yes, output=TimeDistributed_output)    
model_built.compile(loss='mse', optimizer='adam')    
model_built.fit(truth_reshaped, shifted_truth_reshaped, nb_epoch=100)

How would I need to do to input the data correctly?

1

1 Answers

1
votes
yes = Input(shape=(len(truth_reshaped),), name = 'truth_in')

Len(truth_reshaped) will return 1 because you shaped it like (1,2000,1). Here the first 1 is your number of sequences, 2000 is the number of timesteps in your sequence and the second 1 is the number of values in every elements of your sequence.

So your input should be

yes = Input(shape=(len(truth),1), name = 'truth_in')

Which will tell to your network that the input will be sequences of length len(truth,1) and with dimension of 1 for the elements.