Master LLMs with our FREE course in collaboration with Activeloop & Intel Disruptor Initiative. Join now!

Publication

The ABCs of PyTorch in 4 Minutes
Latest   Machine Learning

The ABCs of PyTorch in 4 Minutes

Last Updated on July 20, 2023 by Editorial Team

Author(s): Rohit Sharma

Originally published on Towards AI.

PyTorch 101 U+007C Towards AI

Introducing the basics of PyTorch in four minutes, with sample code

© AITS (www.ai-techsystems.com)

This article helps newbies to get started with python PyTorch in 2 minutes with code written in python. For those who don’t know what PyTorch is, it is an open source deep learning platform from Facebook that provides a seamless path from research prototyping to production deployment.

Without further ado, let’s dive right in.

Tensors

It is is a multi-dimensional matrix containing elements of a single data type, that is declared as

Operations

PyTorch contains many mathematical operations over tensors. Additionally, it provides many utilities for efficient serializing of Tensors and arbitrary types and other useful utilities. Here is an example of the addition/subtraction of tensors.

PyTorch and NumPy

One can easily go back and forth between PyTorch and NumPy. Here is a simple example of converting np.matrix to PyTorch and changing the dimension to a single column

Here is a useful GitHub repo outlining PyTorch<->numpy conversions.

CPU and GPUs

PuTorch allows your variables to change devices on the fly with a torch.cuda.device context manager. Here is the sample code:

PyTorch Variables

A Variable nothing but a thin layer that wrapped around Tensor. It supports almost all the APIs defined by a Tensor. Variable is cleverly defined as part of the autograded package. It provides classes and functions implementing automatic differentiation of arbitrary scalar-valued functions.

Here is a simple example of PyTorch variable usage:

Back Propagation

The backpropagation algorithm is used to compute the gradients of the loss with respect to the input weights and biases to update the weights in the next iteration of optimization and eventually reduce the loss. PyTorch is smart in hierarchically defining a backward method on Variables to perform backpropagation.

Here is a simple example of backpropagation to compute differential with an example of sin(x)

SLR: Simple Linear Regression

Now we’ve collected all the ammunition to get started on machine learning examples with simple linear regression problems. We’ll do that in 4 easy steps:

SLR: Step 1

In step 1, we create an artificial dataset created from an equation y=w.x+b with random error injected. See the example below:

SLR: Step 2

In step 2, we define a simple Class LinearRegressionModel with a method forward and constructor using torch.nn.Linear to a linear transformation to the incoming data

SLR: Step 3

The next step is training the model with MSELossas cost function and SGD as the optimizer.

SLR: Step 4

Now that training is done, let’s inspect our model visually.

There is a lot to learn, so get started with the python notebook with the code presented here. Feel free to change and start playing with PyTorch.

PyTorch_Overview

Link to entire notebook with sample code used in this article …

cainvas.ai-tech.systems

Have fun. U+1F44D

References:

  1. Explainer Video — https://youtu.be/drwrMiARTQ4
  2. Notebook URL — https://cainvas.ai-tech.systems/notebooks/details/?path=rohit/Scholar%20Modules/M4%20Frameworks/PyTorch/PyTorch_Overview.ipynb
  3. PyTorch.org
  4. Hello World of Deep Learning
  5. Machine Intelligence in Design Automation
  6. Design Your Neural Network for EDA Software

Join thousands of data leaders on the AI newsletter. Join over 80,000 subscribers and keep up to date with the latest developments in AI. From research to projects and ideas. If you are building an AI startup, an AI-related product, or a service, we invite you to consider becoming a sponsor.

Published via Towards AI

Feedback ↓