Python Classes and Their Use in Keras

Classes are one of the fundamental building blocks of the Python language, which may be applied in the development of machine learning applications. As we shall see, the Python syntax for developing classes is simple and can be applied to implement callbacks in Keras. 

In this tutorial, you will discover the Python classes and their functionality. 

After completing this tutorial, you will know:

  • Why Python classes are important
  • How to define and instantiate a class and set its attributes 
  • How to create methods and pass arguments
  • What is class inheritance
  • How to use classes to implement callbacks in Keras

Kick-start your project with my new book Python for Machine Learning, including step-by-step tutorials and the Python source code files for all examples.

Let’s get started.

Python Classes and Their Use in Keras
Photo by S Migaj, some rights reserved.

Tutorial Overview

This tutorial is divided into six parts; they are:

  • Introduction to Classes
  • Defining a Class
  • Instantiation and Attribute References
  • Creating Methods and Passing Arguments
  • Class Inheritance
  • Using Classes in Keras

Introduction to Classes

In object-oriented languages, such as Python, classes are one of the fundamental building blocks. 

They can be likened to blueprints for an object, as they define what properties and methods/behaviors an object should have.

Python Fundamentals, 2018.

Creating a new class creates a new object, where every class instance can be characterized by its attributes to maintain its state and methods to modify its state.

Defining a Class

The class keyword allows for the creation of a new class definition, immediately followed by the class name:

In this manner, a new class object bound to the specified class name (MyClass, in this case) is created. Each class object can support instantiation and attribute references, as we will see shortly.

Instantiation and Attribute References

Instantiation is the creation of a new instance of a class.

To create a new instance of a class, we can call it using its class name and assign it to a variable. This will create a new, empty class object:

Upon creating a new instance of a class, Python calls its object constructor method, __init()__, which often takes arguments that are used to set the instantiated object’s attributes. 

We can define this constructor method in our class just like a function and specify attributes that will need to be passed in when instantiating an object.

Python Fundamentals, 2018.

Let’s say, for instance, that we would like to define a new class named Dog:

Here, the constructor method takes two arguments, name and breed, which can be passed to it upon instantiating the object:

In the example that we are considering, name and breed are known as instance variables (or attributes) because they are bound to a specific instance. This means that such attributes belong only to the object in which they have been set but not to any other object instantiated from the same class. 

On the other hand, family is a class variable (or attribute) because it is shared by all instances of the same class.

You may also note that the first argument of the constructor method (or any other method) is often called self. This argument refers to the object that we are in the process of creating. It is good practice to follow the convention of setting the first argument to self to ensure the readability of your code for other programmers. 

Once we have set our object’s attributes, they can be accessed using the dot operator. For example, considering again the dog1 instance of the Dog class, its name attribute may be accessed as follows:

Producing the following output:

Want to Get Started With Python for Machine Learning?

Take my free 7-day email crash course now (with sample code).

Click to sign-up and also get a free PDF Ebook version of the course.

Creating Methods and Passing Arguments

In addition to having a constructor method, a class object can also have several other methods for modifying its state. 

The syntax for defining an instance method is familiar. We pass the argument self … It is always the first argument of an instance method.

Python Fundamentals, 2018.

Similar to the constructor method, each instance method can take several arguments, with the first one being the argument self that lets us set and access the object’s attributes:

Different methods of the same object can also use the self argument to call each other:

An output string can then be generated as follows:

We find that, in doing so, the barks on command input is appended to the tricks list when the info() method calls the add_tricks() method. The following output is produced:

Class Inheritance

Another feature that Python supports is class inheritance. 

Inheritance is a mechanism that allows a subclass (also known as a derived or child class) to access all attributes and methods of a superclass (also known as a base or parent class). 

The syntax for using a subclass is the following:

It is also possible that a subclass inherits from multiple base classes, too. In this case, the syntax would be as follows:

Class attributes and methods are searched for in the base class and also in subsequent base classes in the case of multiple inheritances. 

Python further allows a method in a subclass to override another method in the base class that carries the same name. An overriding method in the subclass may be replacing the base class method or simply extending its capabilities. When an overriding subclass method is available, it is this method that is executed when called, rather than the method with the same name in the base class. 

Using Classes in Keras

A practical use of classes in Keras is to write one’s own callbacks. 

A callback is a powerful tool in Keras that allows us to look at our model’s behavior during the different stages of training, testing, and prediction. 

Indeed, we may pass a list of callbacks to any of the following:

  • keras.Model.fit()
  • keras.Model.evaluate()
  • keras.Model.predict()

The Keras API comes with several built-in callbacks. Nonetheless, we might wish to write our own, and for this purpose, we shall look at how to build a custom callback class. In order to do so, we can inherit several methods from the callback base class, which can provide us with information of when:

  • Training, testing, and prediction starts and ends
  • An epoch starts and ends
  • A training, testing, and prediction batch starts and ends

Let’s first consider a simple example of a custom callback that reports back every time that an epoch starts and ends. We will name this custom callback class, EpochCallback, and override the epoch-level methods, on_epoch_begin() and on_epoch_end(), from the base class, keras.callbacks.Callback:

In order to test the custom callback that we have just defined, we need a model to train. For this purpose, let’s define a simple Keras model:

We also need a dataset to train on, for which purpose we will be using the MNIST dataset:

Now, let’s try out the custom callback by adding it to the list of callbacks that we pass as input to the keras.Model.fit() method:

 

The callback that we have just created produces the following output:

We can create another custom callback that monitors the loss value at the end of each epoch and stores the model weights only if the loss has decreased. To this end, we will be reading the loss value from the log dict, which stores the metrics at the end of each batch and epoch. We will also be accessing the model corresponding to the current round of training, testing, or prediction, by means of self.model. 

Let’s call this custom callback, CheckpointCallback:

We can try this out again, this time including the CheckpointCallback into the list of callbacks:

The following output of the two callbacks together is now produced:

Other classes in Keras

Besides callbacks, we can also make derived classes in Keras for custom metrics (derived from keras.metrics.Metrics), custom layers (derived from keras.layers.Layer), custom regularizer (derived from keras.regularizers.Regularizer), or even custom models (derived from keras.Model, for such as changing the behavior of invoking a model). All you have to do is follow the guideline to change the member functions of a class. You must use exactly the same name and parameters in the member functions.

Below is an example from Keras documentation:

This reveals why we would need a class for the custom metric: A metric is not just a function but a function that computes its value incrementally, once per batch of training data during the training cycle. Eventually, the result is reported at the result() function at the end of an epoch and reset its memory using the reset_state() function so you can start afresh in the next epoch.

For the details on what exactly has to be derived, you should refer to Keras’ documentation.

Further Reading

This section provides more resources on the topic if you are looking to go deeper.

Books

Websites

Summary

In this tutorial, you discovered Python classes and their functionality.

Specifically, you learned:

  • Why Python classes are important
  • How to define and instantiate a class and set its attributes 
  • How to create methods and pass arguments
  • What is class inheritance
  • How to use classes to implement callbacks in Keras

Do you have any questions?
Ask your questions in the comments below, and I will do my best to answer.

Get a Handle on Python for Machine Learning!

Python For Machine Learning

Be More Confident to Code in Python

...from learning the practical Python tricks

Discover how in my new Ebook:
Python for Machine Learning

It provides self-study tutorials with hundreds of working code to equip you with skills including:
debugging, profiling, duck typing, decorators, deployment, and much more...

Showing You the Python Toolbox at a High Level for
Your Projects


See What's Inside

, , , ,

4 Responses to Python Classes and Their Use in Keras

  1. Avatar
    Walle December 22, 2021 at 1:21 am #

    Some minor corrections/comments on the code examples:
    – add a colon to the simple_model function definition
    – y_train -> y_train_cat in model.fit
    – verbose=0 also to the first EpochCallback model.fit example

    • Avatar
      James Carmichael January 10, 2022 at 11:41 am #

      Thank you for the feedback, Walle!

  2. Avatar
    Michael L December 25, 2021 at 2:46 am #

    Thanks Stefania. Really important topic.

    Could you shed some light on whether it’s preferable or not (and why) to define a model as a class (i.e. https://www.tensorflow.org/guide/keras/custom_layers_and_models#the_model_class) or to simply define the model via a function?

    I’ve been building models using subclassing recently as it appears to be the more pythonic approach. Define a class and then instantiate an instance of said class as the model. However, every code repo and website that I’ve seen builds models with functions. Is there a benefit to subclassing?

    If not, why would TensorFlow bother implementing this?

Leave a Reply