I am trying to implement LSTM using Keras for a multi class problem. I have input csv of dimension 1007x5. number of features per instances are 5 and there are total 12 classes. Below is the code
seed = 7
numpy.random.seed(seed)
input_file = 'input.csv'
def load_data(test_split = 0.2):
print ('Loading data...')
dataframe = pandas.read_csv(input_file, header=None)
dataset = dataframe.values
X = dataset[:,0:5].astype(float)
print(X)
Y = dataset[:,5]
print("y=", Y)
return X,Y
def create_model(X):
print ('Creating model...')
model = Sequential()
model.add(LSTM(128, input_shape =(5,)))
model.add(Dense(12, activation='sigmoid'))
print ('Compiling...')
model.compile(loss='categorical_crossentropy',
optimizer='rmsprop',
metrics=['accuracy'])
return model
X,Y,dummy_y= load_data()
print("input Lnegth X=",len(X[0]))
model = create_model(X)
print ('Fitting model...')
hist = model.fit(X, Y, batch_size=5, nb_epoch=10, validation_split = 0.1, verbose = 1)
score, acc = model.evaluate(dummy_x,dummy_y)
print('Test score:', score)
print('Test accuracy:', acc)
Following this error in different forums and posts on here, I have tried different inputs shapes but still it is not working. When I am giving input data shape then I get following errors: 1. when I give input_shape as X.shape[1:]) - error is "input 0 is incompatible layer lstm_1: expected ndim =3, found ndim=2"
When I give input_shape=X.shape[1:]), error is "value error: when checking input: expected lstm_1_input to have 3 dimensions, but got array with shape (1007,5)"
Other than shape, if ndim is set to 5, it says "input 0 is incompatible layer lstm_1: expected ndim =3, found ndim=2"
What should be input to lstm first layer? My dimension to layer 1 should be (128,1007,5), right?
LSTMs require a 3 dimensional input. Your input shape should be in the form of (samples, timesteps, features). Since keras infers the first dimension is samples, you should be inputting (timesteps, features) as your input shape.
Since your csv is of dimension 1007*5, I think that the best course of action is to reshape your input to (1007, 5, 1), so your LSTM can get a 3D input.
So inside load_data:
X = X.reshape(X.shape[0], x.shape[1], 1)
And inside create_model:
model.add(LSTM(128, input_shape =(5,1)))