Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change training dataset every N epochs in Keras

I would like to pass another training dataset (X_train, y_train) every N epochs in Keras, where (X_train, y_train) are obtained through Monte Carlo simulations.

In pseudo-code, it would be done by:

for i in range(nb_total_epochs):
    if i%N == 0:
       X_train, y_train = generate_new_dataset(simulation_parameters)
    train_model(X_train, y_train)

Is there any existing trick to achieve this with the fit() function?

like image 286
floflo29 Avatar asked Mar 27 '18 10:03

floflo29


1 Answers

Use Sequence to create your dataset and pass it to fit_generator. Define the on_epoch_end method to modify the dataset on certain epochs.

Every Sequence must implements the __getitem__ and the __len__ methods. If you want to modify your dataset between epochs you may implement on_epoch_end. The method __getitem__ should return a complete batch.

Also, you can safely use Sequence with multiprocessing data processing:

The use of keras.utils.Sequence guarantees the ordering and guarantees the single use of every input per epoch when using use_multiprocessing=True.

Example

Slightly modified from the Sequence documentation to include on_epoch_end.

class CIFAR10Sequence(Sequence):

    def __init__(self, x_set, y_set, batch_size):
        self.x, self.y = x_set, y_set
        self.epoch = 0
        self.batch_size = batch_size

    def __len__(self):
        return int(np.ceil(len(self.x) / float(self.batch_size)))

    def __getitem__(self, idx):
        batch_x = self.x[idx * self.batch_size:(idx + 1) * self.batch_size]
        batch_y = self.y[idx * self.batch_size:(idx + 1) * self.batch_size]

        return np.array([
            resize(imread(file_name), (200, 200))
               for file_name in batch_x]), np.array(batch_y)

    def on_epoch_end(self):
        if self.epoch % N == 0:
            pass
            # modify data
        self.epoch += 1
like image 154
Fábio Perez Avatar answered Sep 26 '22 00:09

Fábio Perez