PyTorch API

Free Python courses with 57 real-time projects - Learn Python

An Application Programming Interface (API), is an intermediary between two programs, computers or apps etc. It aids in communication among several software and systems without disclosing the underlying low-level details, facilitating coordination without compromising any party’s security.

The core of PyTorch is written in C++, but we can easily implement them using python codes. This combination makes the models built using PyTorch fast, owing to C++, and convenient because of Python.

1. PyTorch C++ API

The C++ API of PyTorch comprises the following parts.

a. ATen

It is the most fundamental structure on which all the other Python and C++ interfaces are built upon. Being a tensor library, it provides a tensor class on which we can define numerous operations capable of running on both CPUs and GPUs.

b. Autograd

Autograd is a part of C++ API that enables the tensors to compute the derivative at runtime without explicitly requiring any code except calling the backward() function on the leaf node of the computational graph.

c. C++ Frontend

The C++ frontend makes PyTorch models faster than Python could provide. Among many advantages, it offers hierarchical structures to the machine learning models and enables the C++ and Python codes to bind together easily.

d. TorchScript

TorchScript is another language in itself, which can be used to represent PyTorch models and can also be compiled using the TorchScript compiler.

e. C++ Extensions

The integration of Python codes into the C++ API is made possible by the C++ extensions available with PyTorch. It provides a powerful way to access all the other interfaces.

2. PyTorch Object-Oriented API and Function-based API

As we already know, PyTorch has torch.nn and torch.nn.functional, which can be used to create neural layers. They are more or less similar, with the main difference being how they accomplish the desired goals. torch.nn is used to make object-oriented models, whereas torch.nn.functional provides functions to perform the same tasks without requiring any object instantiation.

a. PyTorch Object-Oriented API

Technology is evolving rapidly!
Stay updated with DataFlair on WhatsApp!!

In object-oriented API, we need to build a class of our model, initialise all the layers in the __init__() method and reference them accordingly. To simplify, in object-oriented API, there exists an internal state of the object.

Let’s use an example to understand it better.

Code–Object-Oriented API
class DataFlair(nn.Module):
    def __init__(self,features):
    	    super(DataFlair,self).__init__()
    	    self.layer1= nn.Linear(in_features,out_features)
    	    self.layer2=nn.Linear(features_out,num)
    
     def forward(self,x):
    	    x=self.layer1(x)
    	    x=self.layer2(x)
    	    return x

In the above code we have created a class and initialised the layers we want in the __init__() method. In the forward method we call these layers in the desired order and pass the input data to it.

b. PyTorch Function-Based API

Owing to the torch.nn.functional module, we can build neural layers using a functional approach without having to build an entire class for the same. A function acts like a black box. We give it some parameters and it gives us a result. Similarly, we pass the parameters of the layers we want to build and the functional module creates a layer without associating it to any object.

Example–Function based API
F.conv1d(input_data,filter_layers)

Using the function based approach we eliminate some of the complexities of object oriented programming.

3. PyTorch Mobile API

Most commercial applications today are being used via mobile phones, and neural network applications are no exception. On-device machine learning can improve security and also provide many functionalities even in offline mode, with the only constraint being power.

The steps of deployment of a model consist of building the model, converting it to torchscript, optimising it and finally interpreting it using mobile devices.

Summary

PyTorch’s robust API provides a pleasant coding experience. Different ways of building and deploying these models enable coders to follow different approaches to build models conveniently. No approach is better than the other. It all depends on the applications.

Did we exceed your expectations?
If Yes, share your valuable feedback on Google

follow dataflair on YouTube

Leave a Reply

Your email address will not be published. Required fields are marked *