Shape mismatch in LSTM in keras

506 Views Asked by At

I am trying to run a LSTM using Keras on my custom features set. I have train and test features in separate files. Each csv file contains 11 columns with last column as class label. There are total 40 classes in my dataset. The problem is I am not able to figure out the correct input_shape to the first layer. I had explored all the stackoverflow and github but still not able to solve this Below is my complete code.

import numpy
from keras.models import Sequential
from keras.layers import Dense
from keras.layers import LSTM

numpy.random.seed(7)

train_dataset = numpy.loadtxt("train.csv", delimiter=",")
X_train = train_dataset[:, 0:10]
y_train = train_dataset[:, 10]

test_dataset = numpy.loadtxt("test.csv", delimiter=",")
X_test = test_dataset[:, 0:10]
y_test = test_dataset[:, 10]

model = Sequential()

model.add(LSTM(32, return_sequences=True, input_shape=X_train.shape))
model.add(LSTM(32, return_sequences=True))
model.add(LSTM(32))
model.add(Dense(1, activation='softmax'))
model.compile(loss='mean_squared_error', optimizer='sgd', metrics=['accuracy'])
model.fit(X_train, y_train, batch_size=10, epochs=1)

score, acc = model.evaluate(X_test, y_test, batch_size=10)
print('Test score:', score)
print('Test accuracy:', acc * 100)

Whatever I change in input_shape parameter wither I get error in first LSTM layer of in fit method.

1

There are 1 best solutions below

3
On

you don't have a time dimension in your input. Input for RNN should be (batch_size, time_step, features) while your input has dimension (batch_size, features).

If you want to use your 10 columns one at a time you should reshape the array with numpy.reshape(train_dataset, (-1, train_dataset.shape[1], 1))

Try this code:

train_dataset = numpy.loadtxt("train.csv", delimiter=",")
train_dataset = numpy.reshape(train_dataset, (-1, train_dataset.shape[1], 1))

X_train = train_dataset[:, 0:10]
y_train = train_dataset[:, 10]

test_dataset = numpy.loadtxt("test.csv", delimiter=",")
test_dataset = numpy.reshape(test_dataset, (-1, train_dataset.shape[1], 1))

X_test = test_dataset[:, 0:10]
y_test = test_dataset[:, 10]

model = Sequential()

model.add(LSTM(32, return_sequences=True, input_shape=(X_train.shape[1], 1)))
model.add(LSTM(32, return_sequences=True))
model.add(LSTM(32))
model.add(Dense(1, activation='softmax'))
model.compile(loss='mean_squared_error', optimizer='sgd', metrics=['accuracy'])
model.fit(X_train, y_train, batch_size=10, epochs=1)

score, acc = model.evaluate(X_test, y_test, batch_size=10)
print('Test score:', score)
print('Test accuracy:', acc * 100)