MITB Banner

How to perform gradient-based optimization using Autograd?

Autograd is a python package that can provide us with a way to differentiate Numpy and Python code. It is a library for gradient-based optimization. Using this package we can work with a large subset of features of python including loops, ifs, recursion, and closures. Also, this package is capable of taking multiple step-wise derivatives of functions.
Share

Testing a predictive model plays a crucial role in machine learning. We usually evaluate these models by optimizing some loss functions with respect to the parameters defined by the model. In-depth, we can optimize these loss functions using gradient descent. Autograd is a python package that can help us in optimizing various functions whether they are related to machine learning models or related to core mathematics. This package can also replace the popular NumPy library in various cases. In this article, we will discuss the Autograd package and we will understand how we can use it. The major points to be discussed in this article are listed below. 

Table of contents

  1. What is Autograd?
  2. Implementation of Autograd
    1. Evaluating the gradient of the hyperbolic tangent function
    2. Optimizing loss functions in Logistic regression     

Let us start with having an introduction with Autograd.

What is Autograd?

Autograd is a python package that can provide us with a way to differentiate Numpy and Python code. It is a library for gradient-based optimization. Using this package we can work with a large subset of features of python including loops, ifs, recursion, and closures. Also, this package is capable of taking multiple step-wise derivatives of functions. Most of the time we find that this package supports the backpropagation and forward propagation methods that can also be called reverse-mode differentiation and forward mode differentiation in mathematics, which means this package is capable of picking gradients from a function with scalar values.  

Using this package we can compose these two methods arbitrarily. Basically, this package is intended to provide us with the facility of gradient-based optimization. Using this package we can utilize gradients for the following operations and functions:

  • Mathematical operations – various gradients are implemented for optimizing most of the mathematical operations.
  • Gradients for completing various array manipulation are available.
  • Gradients for completing various matrix manipulations are in the package.
  • Various gradients are in the package for some linear algebra and Fourier transform routines.
  • Modules are there for N-dimensional convolutions.
  • Full support for complex numbers.

We can install this package using the following lines of codes:

!pip install autograd

After installing this package in the environment, we are ready to use this package for our work.        

Are you looking for for a complete repository of Python libraries used in data science, check out here.

Implementations of Autograd

In this section of the article, we will look at some of the functions that can be followed using the Autograd package.

Evaluating the gradient of the hyperbolic tangent function 

In this implementation, we will look at how we can evaluate the gradient of the Tanh function. Let’s define the function.

import autograd.numpy as agnp
def tanh(x):                 
     y = agnp.exp(-2.0 * x)
     return (1.0 - y) / (1.0 + y)

In the above codes, we can see that we have used module autograd.numpy which is a wrapper of NumPy in the autograd package. Also, we have defined a function for tan. Let’s evaluate the gradient of the above-defined function.

from autograd import grad 
grad_tanh = grad(tanh)    
grad_tanh(1.0)

Output: 

Here in the above codes, we have initiated a variable that can hold the tanh function and for evaluation, we have imported a function called grad from the autograd package. Let’s compare the finite range difference.

(tanh(1.0001) - tanh(0.9998)) / 0.0002 

Output:

We also have the facility to differentiate the functions as many times as we want, for this, we just need to call a module named as the elementwise_grad.

from autograd import elementwise_grad
x = agnp.linspace(-10, 10, 100)

In the above codes, we have called our module and defined an array having random values between -10 to 10. In this, we will try to draw derivatives for the above-defined function.

plt.plot(x, agnp.tanh(x),
         x, elementwise_grad(agnp.tanh)(x),
         x, elementwise_grad(elementwise_grad(agnp.tanh))(x),
         x, elementwise_grad(elementwise_grad(elementwise_grad(agnp.tanh)))(x),
         x, elementwise_grad(elementwise_grad(elementwise_grad(elementwise_grad(agnp.tanh))))(x))        
plt.show()

Output:

Here we can see how the function is varying for our defined x.

Note- at the very start of the implementation we have defined a function of tanh and here in the recent one we have used the function given by autograd.    

In the above example, we have discussed how to use modules from autograd. Let’s see how we can use it for logistic regression.

Optimizing loss functions in Logistic regression 

Let’s define a sigmoid function.

def function1(x):
    return 0.5 * (agnp.tanh(x / 2.) + 1)

Defining a function for predictions:

def function2(w, i):
    return function1(agnp.dot(i, w))

Defining loss function for training:

def loss_function(w):
    preds = function2(w, i)
    label_probabilities = preds * targets + (1 - preds) * (1 - targets)
    return -agnp.sum(agnp.log(label_probabilities))

Defining the weights  and input:

i = agnp.array([[0.52, 1.12,  0.77],                
                [0.88, -1.08, 0.15],
                [0.52, 0.06, -1.30],
                [0.74, -2.49, 1.39]])
w = agnp.array([0.0, 0.0, 0.0])

Defining the target:

targets = agnp.array([True, True, False, True])

Defining gradient function for training loss:

training_gradient = grad(loss_function)

Optimization of weight using the gradient descent:

w = np.array([0.0, 0.0, 0.0])
print("loss in initial:", loss_function(w))
for i in range(100):
    weights -= training_gradient(w) * 0.01
print("loss after training:", loss_function(w))

Output:

Here we have seen an example of logistic regression for optimization of weights that we pushed in between using the modules of the autograd package. 

Final words

In this article, we have discussed what Autograd is, which is a package for gradient-based optimization of various functions. Along with this, we have gone through some of the implementations for optimizing functions related to mathematics and machine learning.    

References

PS: The story was written using a keyboard.
Share
Picture of Yugesh Verma

Yugesh Verma

Yugesh is a graduate in automobile engineering and worked as a data analyst intern. He completed several Data Science projects. He has a strong interest in Deep Learning and writing blogs on data science and machine learning.
Related Posts

CORPORATE TRAINING PROGRAMS ON GENERATIVE AI

Generative AI Skilling for Enterprises

Our customized corporate training program on Generative AI provides a unique opportunity to empower, retain, and advance your talent.

Upcoming Large format Conference

May 30 and 31, 2024 | 📍 Bangalore, India

Download the easiest way to
stay informed

Subscribe to The Belamy: Our Weekly Newsletter

Biggest AI stories, delivered to your inbox every week.

AI Courses & Careers

Become a Certified Generative AI Engineer

AI Forum for India

Our Discord Community for AI Ecosystem, In collaboration with NVIDIA. 

Flagship Events

Rising 2024 | DE&I in Tech Summit

April 4 and 5, 2024 | 📍 Hilton Convention Center, Manyata Tech Park, Bangalore

MachineCon GCC Summit 2024

June 28 2024 | 📍Bangalore, India

MachineCon USA 2024

26 July 2024 | 583 Park Avenue, New York

Cypher India 2024

September 25-27, 2024 | 📍Bangalore, India

Cypher USA 2024

Nov 21-22 2024 | 📍Santa Clara Convention Center, California, USA

Data Engineering Summit 2024

May 30 and 31, 2024 | 📍 Bangalore, India