1

I am learning about RNN and I wrote this simple LSTM model in keras (theano) using a sample dataset generated using sklearn.

from sklearn.datasets import make_regression
from keras.models import Sequential
from keras.layers import Dense,Activation,LSTM

#creating sample dataset
X,Y=make_regression(100,9,9,2)
X.shape
Y.shape

#creating LSTM model
model = Sequential()
model.add(LSTM(32, input_dim=9))
model.add(Dense(2))
model.compile(loss='mean_squared_error', optimizer='adam')

#model fitting
model.fit(X, Y, nb_epoch=1, batch_size=32)

The sample data set contains 9 features and 2 targets. when I tried to fit my model using those features and targets its giving me this error

Exception: Error when checking model input: expected lstm_input_9 to have 3 dimensions, but got array with shape (100, 9)
Eka
  • 14,170
  • 38
  • 128
  • 212
  • If any one interested I solved the problem by reshaping my `X` value like this `X=X.reshape(X.shape[0],1,X.shape[1])` – Eka Oct 15 '16 at 09:11

1 Answers1

2

If I'm correct, then LSTM expects a 3D input.

X = np.random.random((100, 10, 64))
y = np.random.random((100, 2))

model = Sequential()
model.add(LSTM(32, input_shape=(10, 64)))
model.add(Dense(2)) 
model.compile(loss='mean_squared_error', optimizer='adam')

model.fit(X, Y, nb_epoch=1, batch_size=32)

UPDATE: If you want to convert X, Y = make_regression(100, 9, 9, 2) into 3D, then you can use this.

from sklearn.datasets import make_regression
from keras.models import Sequential
from keras.layers import Dense,Activation,LSTM

#creating sample dataset
X, Y = make_regression(100, 9, 9, 2)
X = X.reshape(X.shape + (1,))

#creating LSTM model
model = Sequential()
model.add(LSTM(32, input_shape=(9, 1)))
model.add(Dense(2))
model.compile(loss='mean_squared_error', optimizer='adam')

model.fit(X, Y, nb_epoch=1, batch_size=32)
Anish Shah
  • 7,669
  • 8
  • 29
  • 40
  • is there any way to convert this `X,Y=make_regression(100,9,9,2)` into 3D input – Eka Oct 11 '16 at 18:21
  • What is this line `X = X.reshape(X.shape + (1,))` does? and even after that model is also not fitting and showing some error? – Eka Oct 13 '16 at 17:20