EderSantana / seya

Bringing up some extra Cosmo to Keras.
Other
377 stars 103 forks source link

StatefulGRU "Input dimension mis-match" on predict() #14

Closed ekerazha closed 8 years ago

ekerazha commented 8 years ago

I have this code:

in_out_neurons = 1
hidden_neurons = 10
batch_size = 10

model = Sequential()
model.add(SGRU(output_dim=hidden_neurons, input_dim=in_out_neurons, return_sequences=True, batch_size=batch_size))
model.add(Dropout(0.2))
model.add(TimeDistributedDense(in_out_neurons))
model.add(Activation("linear"))
model.compile(loss="mean_squared_error", optimizer="rmsprop")

early_stopping = EarlyStopping(monitor='val_loss', patience=10)
model.fit(X_train, y_train, batch_size=batch_size, nb_epoch=1000, validation_split=0.1, callbacks=[early_stopping])
predicted = model.predict(X_test)

Training seems to be ok, but it gives

ValueError: Input dimension mis-match. (input[0].shape[0] = 10, input[1].shape[0] = 1)
Apply node that caused the error: Elemwise{mul,no_inplace}(<TensorType(int8, col)>, <TensorType(float64, matrix)>)
Toposort index: 0
Inputs types: [TensorType(int8, col), TensorType(float64, matrix)]
Inputs shapes: [(10L, 1L), (1L, 10L)]

on model.predict().

Standard "GRU" from Keras does work fine. Theano is up-to-date (GIT version).

Am I missing something?

EderSantana commented 8 years ago

Were you able to train the Stateful GRU on your data? If so try making the batch size fixed: predicted = model.predict(X_test, batch_size=batch_size) Statefulness forces a cap on the batch size.

Also, note that statefulness is handled with callbacks during training. If you want to use states passed from one batch to another, I think you won't be able to use predict. You will have to write you own function for testing. That function should get the final state of the model after each batch and pass it as initial state for the next batch. But maybe you don't have to do that if results are good enough.

ekerazha commented 8 years ago

Thank you for your answer.

I'm able to train the Stateful GRU, but it gives that error when I call model.predict()

I changed

predicted = model.predict(X_test)

to

predicted = model.predict(X_test, batch_size=batch_size)

but it still gives the same identical error.

EderSantana commented 8 years ago

What is the size of your test set and batch size?

ekerazha commented 8 years ago

X_test shape is (120L, 5L, 1L). Batch size is 10 (it's 10 for training too).

ekerazha commented 8 years ago

Stateful RNNs are now part of keras.