Yixiang Cai
Yixiang Cai

Reputation: 95

The starting point of next epoch for fit_generator in Keras

In Keras documentation, steps_per_epoch in fit_generator means:

Total number of steps (batches of samples) to yield from generator before declaring one epoch finished and starting the next epoch.

I am wondering if I have an infinite generator, then where do I start when I enter the next epoch? Do I pick up where I was left? Or do I start over from the first sample again?

Upvotes: 1

Views: 329

Answers (1)

today
today

Reputation: 33410

It depends on the definition of your generator. Actually, if you have a generator that yields batches infinitely, then you must make sure it is defined such that after iterating over all the samples (i.e. one epoch) it goes back to the starting point (i.e. we don't generate a sample more than once in an epoch). For example, a rough sketch of such a generator would be like this:

def my_gen(x_train, y_train, batch_size):
    n_samples = x_train.shape[0]
    n_steps = np.ceil(n_samples // batch_size)
    while True:
        # one epoch: goes over all the samples
        for i in range(n_steps):
            # the last batch might have less than batch_size samples
            if (i+1) * batch_size > n_samples:
                x = x_train[i * batch_size:]
                y = y_train[i * batch_size:]
            else:  
                x = x_train[i * batch_size : (i+1) * batch_size]
                y = y_train[i * batch_size : (i+1) * batch_size]
            yield x, y

Upvotes: 1

Related Questions