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?
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 implementon_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 usinguse_multiprocessing=True
.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With