Name: Towards AI Legal Name: Towards AI, Inc. Description: Towards AI is the world's leading artificial intelligence (AI) and technology publication. Read by thought-leaders and decision-makers around the world. Phone Number: +1-650-246-9381 Email: [email protected]
228 Park Avenue South New York, NY 10003 United States
Website: Publisher: https://towardsai.net/#publisher Diversity Policy: https://towardsai.net/about Ethics Policy: https://towardsai.net/about Masthead: https://towardsai.net/about
Name: Towards AI Legal Name: Towards AI, Inc. Description: Towards AI is the world's leading artificial intelligence (AI) and technology publication. Founders: Roberto Iriondo, , Job Title: Co-founder and Advisor Works for: Towards AI, Inc. Follow Roberto: X, LinkedIn, GitHub, Google Scholar, Towards AI Profile, Medium, ML@CMU, FreeCodeCamp, Crunchbase, Bloomberg, Roberto Iriondo, Generative AI Lab, Generative AI Lab Denis Piffaretti, Job Title: Co-founder Works for: Towards AI, Inc. Louie Peters, Job Title: Co-founder Works for: Towards AI, Inc. Louis-François Bouchard, Job Title: Co-founder Works for: Towards AI, Inc. Cover:
Towards AI Cover
Logo:
Towards AI Logo
Areas Served: Worldwide Alternate Name: Towards AI, Inc. Alternate Name: Towards AI Co. Alternate Name: towards ai Alternate Name: towardsai Alternate Name: towards.ai Alternate Name: tai Alternate Name: toward ai Alternate Name: toward.ai Alternate Name: Towards AI, Inc. Alternate Name: towardsai.net Alternate Name: pub.towardsai.net
5 stars – based on 497 reviews

Frequently Used, Contextual References

TODO: Remember to copy unique IDs whenever it needs used. i.e., URL: 304b2e42315e

Resources

Take our 85+ lesson From Beginner to Advanced LLM Developer Certification: From choosing a project to deploying a working product this is the most comprehensive and practical LLM course out there!

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 ↓