Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use batch trained model, to predict on single input?

I have RNN model that have been trained on Dataset:

train = tf.data.Dataset.from_tensor_slices((data_x[:train_size],
                          data_y[:train_size])).batch(batch_size).repeat()

model:

    model = tf.keras.Sequential()
    model.add(tf.keras.layers.GRU(units=lstm_num_units,
                                   return_sequences=True,
                                   kernel_initializer='random_uniform',
                                   recurrent_initializer='random_uniform',
                                   bias_initializer='random_uniform',
                                   batch_size=batch_size,
                                   input_shape = [seq_len, num_features]))
    model.add(tf.keras.layers.LSTM(units=lstm_num_units,
                                   batch_size=batch_size,
                                   return_sequences=True,
                                   input_shape = [seq_len, num_features]))
    model.add(tf.keras.layers.Flatten())
    model.add(tf.keras.layers.Dense(units=dence_units))
    model.add(tf.keras.layers.Dropout(drop_flat))
    model.add(tf.keras.layers.Dense(units=out_units))
    model.add(tf.keras.layers.Softmax())   

    model.compile(loss="sparse_categorical_crossentropy",
            optimizer=tf.train.RMSPropOptimizer(opt),
            metrics=['accuracy'])

 model.fit(train, epochs=EPOCHS,
                        steps_per_epoch=repeat_size_train,
                        validation_data=validate,
                        validation_steps=repeat_size_validate,
                        verbose=1,
                        shuffle=True)
                        callbacks=[tensorboard, cp_callback])

I need to do prediction on single input of seq_len, but looks like my input have to be of a batch size:

ar = np.random.randint(98, size=[batch_size, seq_len])
ar = np.reshape(ar, [batch_size, seq_len, 1])
prediction = model.m.predict(ar)

Is there a way to make it work on a single input of shape [1, seq_len, 1]?

like image 295
Artem Avatar asked Jan 25 '26 03:01

Artem


1 Answers

Yes, simply rebuild the model without a batch size in the first layer.

Copy the weights of the old model.

newModel.set_weights(oldModel.get_weights())

The purpose of the batch size only exists in stateful=True models to keep consistency between batches.

Even though, there is no mathematical change due to batch size.

like image 171
Daniel Möller Avatar answered Jan 26 '26 23:01

Daniel Möller