Andrew
Andrew

Reputation: 2173

Keras: shape error when using validation data

Trying to add validation to model.fit, but whenever I do I get an error:

ValueError: Cannot feed value of shape (6, 4, 10) for Tensor 'lstm_input_1:0', which has shape '(32, 4, 10)'

Model:

data_dim = 10
timesteps = 4
batch_size = 32

model = Sequential()
model.add(LSTM(128, batch_input_shape=(batch_size, timesteps, data_dim), return_sequences=True, stateful=True))
model.add(LSTM(64, return_sequences=True, stateful=True))
model.add(LSTM(32, stateful=True))
model.add(Dense(2, activation='softmax'))

sgd = SGD(lr=0.001, momentum=0.0, decay=0.0, nesterov=False)
model.compile(optimizer=sgd, loss='categorical_crossentropy', metrics=['accuracy'])

model.fit(x_train, y_train, nb_epoch=50, batch_size=batch_size, validation_split=0.5)

What could be the error? If I remove validation_split the training works just fine. I've also tried to manually split my training set into two and add it with validation_data=(x_val, y_val) but I got the exact same error.

Upvotes: 2

Views: 1935

Answers (1)

Nassim Ben
Nassim Ben

Reputation: 11543

The issue comes from the fact that you hard code the batch_size value of your inputs. You have fixed it to 32 and then when you try and validate your model, the validation data is sent with a batch of 6 samples, this might be because you don't have enough validation data or maybe because the number of sample isn't a multiple of 32... However, I would let the batch_size free if I was you. Like this:

model.add(LSTM(128, input_shape=(timesteps, data_dim), return_sequences=True, stateful=True))

You specify input_shape instead of batch_input_shape. That way, your network will accept any size of batch, every layer down in the stream of your model are made to adapt to any batch_size if not hardcoded.

I hope this helps :)

Upvotes: 1

Related Questions