Is fitting a model in a for loop equivalent to using epochs>1?

I'm using tensorflow to train a network to do an image segmentation task, and I have a question about the behavior of model.fit between epochs, specifically:

Is there any difference between calling model.fit with 512 epochs, and calling model.fit 512 times?

Here's a simplified version of my code, in case it helps. First, some setup:

# Create image generators for dataset augmentation
imgGen = ImageDataGenerator(**data_augmentation_parameters)
maskGen = ImageDataGenerator(**data_augmentation_parameters)
seed = random.randint(0, 1000000000)
imgIterator = imgGen.flow(img, seed=seed, shuffle=False, batch_size=batch_size)
maskIterator = maskGen.flow(mask, seed=seed, shuffle=False, batch_size=batch_size)

# Load network structure from model.py file
network = unet(net_scale = 1)

# Calculate # of iterations
steps_per_epoch = int(num_samples / batch_size)

The two methods of iteratively fitting:

Fit method #1:

network.fit(
    ((imgBatch, maskBatch) for imgBatch, maskBatch in zip(imgIterator, maskIterator)),
    steps_per_epoch=steps_per_epoch,
    epochs=512,
)

Fit method #2:

for epoch in range(512):
    network.fit(
        ((imgBatch, maskBatch) for imgBatch, maskBatch in zip(imgIterator, maskIterator)),
        steps_per_epoch=steps_per_epoch,
        epochs=1,
    )

I think this question is the same as mine, but I don't understand how the one answer applies to the question - I simply want to know if there is some internal difference between specifying an epoch number 1 and running model.fit in a for loop.

Thank you!

Topic epochs keras tensorflow python

Category Data Science


According to this question in the github repository for Keras, yes you should be able to incrementally train your model the way you want in a loop. That being said, you should likely run a test on both ways and see if it yields different results.

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.