0
$\begingroup$

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?

$\endgroup$
1
  • 1
    $\begingroup$ Predict one sequence at a time, there is no need to cut sequences or padding them. $\endgroup$ Commented May 19, 2018 at 21:24

1 Answer 1

0
$\begingroup$

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)

Load Save Parameters and Predict

model_pred.load_weights('weights.h5')
model_pred.predict(...)

Nevertheless I hope for a better solution through sharing weights between a stateful GRU and a standard GRU. Is that possible?

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.