keras.fit() and keras.fit_generator()


Introduction

The fit() and fit generator() methods in Keras make it incredibly easy to train deep neural networks in Python. The fit() method makes it possible to efficiently process and train on batches of data, making it particularly useful for smaller datasets that can be loaded into memory. On the other hand, the fit generator() method, which enables dynamic loading and processing of batches, is better suited for larger datasets that cannot be loaded into memory all at once.

Basics of Keras

Today, technology has improved in every way possible. So, in this advanced environment of technology and future, Keras has come to be known as one of the famous python library. It mainly works for creating,assessing and training deep neural networks.The structure and operation of the human brain are mimicked by these deep neural networks, a form of machine learning algorithm.Image recognition, natural language processing,speech recognition etc are some of its activities.

The fit() Method

This is Keras' go-to method for model training, and it works well with small to medium-sized datasets. If your dataset is too huge to fit into memory, the fit() function needs you to put it completely into memory before training. Regardless, using the fit() technique is straightforward. Simply enter your training data, labels, number of epochs, and batch size. Keras then trains your model by iterating through your dataset for a defined number of epochs and batch sizes. The fit() method is excellent for basic training, but it has limitations.

Limitations of fit()

Keras' fit() function is commonly used for training deep neural networks (DNNs). However, it has significant limitations that can restrict its usefulness. One of the most serious problems with fit() is when dealing with huge datasets. Despite being a strong and versatile tool for training DNNs, a large dataset can cause the fit() function to take a long time to process, causing your project to be delayed.Memory usage is another constraint of fit(). When you have limited memory, this might be a serious difficulty. It is also not particularly adaptable in terms of customisation.

The fit_generator() Method

The fit_generator() method, like the fit() method, trains a neural network on a dataset. The only variation is how the data is processed. While fit() loads the entire dataset into memory at once, fit_generator() processes data in batches. This minor distinction may not appear to be significant, but it provides numerous unique benefits. For starters, it enables you to work with significantly larger datasets. No more running out of memory midway through a training session! Furthermore, it allows you to customize your data processing in more intricate ways.

Syntax

fit() −

model.fit(x=x_train, y=y_train, batch_size=batch_size, epochs=epochs, validation_data=(x_val, y_val))

fit_generator −

model.fit_generator(generator=train_generator, steps_per_epoch=steps_per_epoch,  epochs=epochs, validation_data=val_generator, validation_steps=val_steps)

Explanation

We need the model instance itself, which serves as the foundation for the training process. Next, you'll need the input data (x_train) and labels (y_train) that will be used to train the model.

The batch_size option specifies the number of samples that will be processed before the model's internal parameters are updated. This value can affect the pace and efficiency of the training process. Furthermore, the number of epochs specifies how many times the complete training dataset will be processed through the model during training.

If available, you can add optional validation_data to test the model's performance and track the loss on a distinct dataset throughout training. This allows you to examine the model's generalization ability and make any necessary improvements.

Alternatively, you can use a train_generator, which generates batches of training data on the fly. The steps_per_epoch option indicates how many batches the generator should generate for each epoch. Similarly, a val_generator can be used with the val_steps argument, which specifies the number of batches the validation generator should generate for each epoch.

Algorithm

  • Step 1 − Import all the necessary libraries and modules.

  • Step 2 − Next remember, to load or produce all the required training and validation data.

  • Step 3 − Keras API must be used , to provide the model architecture.

  • Step 4 − Compile the model after loss function, optimizer and evaluation matrix is set

  • Step 5 − To train the model use any of the above given model (that is fit() or fit_generator)

Approach 1: Supervised Learning Using fit().

This code instructs a computer programme to use a neural network to learn patterns in data. The programme has two layers: one that looks for patterns in the data and another that predicts the data's category.

Example

# Importing required libraries
import tensorflow as tf
from tensorflow import keras
import numpy as np
# Defining the Sequential model with 2 layers
model = keras.Sequential([
	keras.layers.Dense(units=64, activation='relu', input_dim=100),
	keras.layers.Dense(units=10, activation='softmax')
])
# Compiling the model with required configuration
model.compile(loss='categorical_crossentropy', optimizer='sgd', metrics=['accuracy'])
# Creating a dummy dataset
x_train = np.random.random((1000, 100))
y_train = np.random.random((1000, 10))
# Training the model
model.fit(x_train, y_train, epochs=20, batch_size=128)

Output

Epoch 1/20
8/8 [==============================] - 1s 3ms/step - loss: 12.2208 - accuracy: 0.1070
Epoch 2/20
8/8 [==============================] - 0s 3ms/step - loss: 12.6586 - accuracy: 0.1090
...
Epoch 20/20
8/8 [==============================] - 0s 2ms/step - loss: 35170420.0000 - accuracy: 0.1060
<keras.callbacks.History at 0x7f14d04b2c80>

Approach 2: Example Using Keras.fit_generator()

In this code we will perform data augmentation and rescaling by using ImageDataGenerator class from Keras.Data generators yield batches of data on-the-fly, enabling training with large-scale datasets.

Example

import numpy as np
from keras.models import Sequential
from keras.layers import Dense
from keras.preprocessing.image import ImageDataGenerator
x_train = np.random.random((1000, 20))
y_train = np.random.randint(2, size=(1000, 1))
x_val = np.random.random((100, 20))
y_val = np.random.randint(2, size=(100, 1))
train_datagen = ImageDataGenerator(rescale=1./255)
val_datagen = ImageDataGenerator(rescale=1./255)
train_generator = train_datagen.flow(x_train, y_train, batch_size=32)
val_generator = val_datagen.flow(x_val, y_val, batch_size=32)
model = Sequential()
model.add(Dense(64, activation='relu', input_dim=20))
model.add(Dense(1, activation='sigmoid'))
model.compile(optimizer='rmsprop', loss='binary_crossentropy', metrics=['accuracy'])
model.fit_generator(generator=train_generator, steps_per_epoch=30, epochs=10, validation_data=val_generator, validation_steps=3)

Output

Epoch 1/10
30/30 [==============================] - 0s 5ms/step - loss: 0.7105 - accuracy: 0.4969 - val_loss: 0.6967 - val_accuracy: 0.4792
...
Epoch 10/10
30/30 [==============================] - 0s 2ms/step - loss: 0.6932 - accuracy: 0.5031 - val_loss: 0.6931 - val_accuracy: 0.4792

Conclusion

Remember, the fit() method is great for small datasets and simple models, but it has its limitations. The fit_generator() method offers more flexibility and customization options for larger datasets and more complex models. Choose wisely based on your specific needs.

Updated on: 13-Oct-2023

356 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements