TransWikia.com

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

Data Science Asked by Torben. on December 10, 2020

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?

One Answer

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?

Answered by Torben. on December 10, 2020

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP