Lucas Lucas - 6 months ago 32
Python Question

How to train using batch inputs with Keras, but predicting with single example with an LSTM?

I have a a list of training data that I am using to train. However, when I predict, the prediction will be done online with a single example at a time.

If I declare my model with input like the following

model = Sequential()
model.add(Dense(64, batch_input_shape=(100, 5, 1), activation='tanh'))
model.add(LSTM(32, stateful=True))
model.add(Dense(1, activation='linear'))
optimizer = SGD(lr=0.0005)
model.compile(loss='mean_squared_error', optimizer=optimizer)


When I go to predict with a single example of shape (1, 5, 1), it gives the following error.

ValueError: Shape mismatch: x has 100 rows but z has 1 rows


The solution I came up with was to just train my model iteratively using a batch_input_shape of (1,5,1) and calling fit for each single example. This is incredibly slow.

Is there not a way to train on a large batch size, but predict with a single example using LSTM?

Thanks for the help.

Answer Source

Try something like this:

model2 = Sequential()
model2.add(Dense(64, batch_input_shape=(1, 5, 1), activation='tanh')) 
model2.add(LSTM(32, stateful=True))
model2.add(Dense(1, activation='linear'))
optimizer2 = SGD(lr=0.0005)
model2.compile(loss='mean_squared_error', optimizer=optimizer)

for nb, layer in enumerate(model.layers):
    model2.layers[nb].set_weights(layer.get_weights())

You are simply rewritting weights from one model to another.