Back

Explore Courses Blog Tutorials Interview Questions
0 votes
4 views
in Python by (19.9k points)

I am building my first neural network with LSTM and I have an error in the input size.

I guess the error is in the input parameters, in the size, the dimension but I can not understand the error.

print df.shape

data_dim = 13

timesteps = 13

num_classes = 1

batch_size = 32

model = Sequential()

model.add(LSTM(32, return_sequences = True, stateful = True,

               batch_input_shape = (batch_size, timesteps, data_dim)))

model.add(LSTM(32, return_sequences = True, stateful = True))

model.add(LSTM(32, stateful = True))

model.add(Dense(1, activation = 'relu'))

#Compile.

model.compile(loss = 'binary_crossentropy', optimizer = 'adam', metrics = ['accuracy'])

model.summary()

#Fit.

history = model.fit(data[train], label[train], epochs = iteraciones, verbose = 0)

#Eval.

scores = model.evaluate(data[test], label[test], verbose = 0)

#Save.

cvshistory.append(history)

cvscores.append(scores[1] * 100)

Shape:

(303, 14)

summary:

_________________________________________________________________

Layer (type)                 Output Shape              Param #   

=================================================================

lstm_19 (LSTM)               (32, 13, 32)              5888      

_________________________________________________________________

lstm_20 (LSTM)               (32, 13, 32)              8320      

_________________________________________________________________

lstm_21 (LSTM)               (32, 32)                  8320      

_________________________________________________________________

dense_171 (Dense)            (32, 1)                   33        

=================================================================

Total params: 22,561

Trainable params: 22,561

Non-trainable params: 0

_________________________________________________________________

The error output tells me the following:

---> 45   history = model.fit(data[train], label[train], epochs = iteraciones, verbose = 0)

ValueError: Error when checking input: expected lstm_19_input to have 3 dimensions, but got array with shape (226, 13)

1 Answer

0 votes
by (25.1k points)

LSTM requires input of shape (batch_size, timestep, feature_size). You are passing only two dimension features. Since timesteps=13 you need to add one more dimension to your input.

If data is a numpy array, then: data = data[..., np.newaxis] should do it.

Shape of data now will be (batch_size, timesteps, feature)

Browse Categories

...