# Predictions with arbitrairy sequence length for stateful RNN (LSTM/GRU) in Keras

I have time series data of the following properties:

input shape:  (num_timesteps, num_features)
output shape: (num_timesteps, num_outputs)


I reshape it to batch form:

input shape:  (num_batches, num_timesteps_in_batch, num features)
output shape: (num_batches, num_timesteps_in_batch, num outputs)


I have a stateful RNN in Keras:

modelinput = Input(batch_shape=(num_batches,None,num_features))
prediction = GRU(10,return_sequences=True,stateful=True)(inputs)
model = Model(inputs=modelinput,outputs=prediction)


After trainig (which works fine) I would like to predict on a sequence without cutting the data, so input shape (num_timesteps, num_features). How can I do that?

I thought about having a second model that shares the weights with the RNN and that has dynamic input shapes. Is that possible?

• Predict one sequence at a time, there is no need to cut sequences or padding them. May 19 '18 at 21:24

I found a possible solution: You can save the weights, create a complete new model and load the weights again.

Original Stateful Model

modelinput = Input(batch_shape=(num_batches,None,num_features))
prediction = GRU(10,return_sequences=True,stateful=True)(inputs)
model = Model(inputs=modelinput,outputs=prediction)


Save Weights after Training

model.fit(...)
model.save_weights('weights.h5')


Create New Model (same structure, just not stateful and dynamic batch size)

modelinput_pred = Input(batch_shape=(None,None,num_features))
prediction_pred = GRU(10,return_sequences=True,stateful=False)(inputs_pred)
model_pred = Model(inputs=modelinput,outputs=prediction_pred)


model_pred.load_weights('weights.h5')