Pytorch

Pytorch

What Is PyTorch?

PyTorch is an optimized Deep Learning tensor library based on Python and Torch and is mainly used for applications using GPUs and CPUs. PyTorch is favored over other Deep Learning frameworks like TensorFlow and Keras since it uses dynamic computation graphs and is completely Pythonic. It allows scientists, developers, and neural network debuggers to run and test portions of the code in real-time. Thus, users don’t have to wait for the entire code to be implemented to check if a part of the code works or not. 

The two main features of PyTorch are:

  • Tensor Computation (similar to NumPy) with strong GPU (Graphical Processing Unit) acceleration support
  • Automatic Differentiation for creating and training deep neural networks

Basics of PyTorch

The basic PyTorch operations are pretty similar to Numpy. Let’s understand the basics first.

  • Introduction to Tensors

In machine learning, when we represent data, we need to do that numerically. A tensor is simply a container that can hold data in multiple dimensions. In mathematical terms, however, a tensor is a fundamental unit of data that can be used as the foundation for advanced mathematical operations. It can be a number, vector, matrix, or multi-dimensional array like Numpy arrays. Tensors can also be handled by the CPU or GPU to make operations faster. There are various types of tensors like Float Tensor, Double Tensor, Half Tensor, Int Tensor, and Long Tensor, but PyTorch uses the 32-bit Float Tensor as the default type. 

  • Mathematical Operations

The codes to perform mathematical operations are the same in PyTorch as in Numpy. Users need to initialize two tensors and then perform operations like addition, subtraction, multiplication, and division on them. 

  • Matrix Initialization and Matrix Operations

To initialize a matrix with random numbers in PyTorch, use the function randn() that gives a tensor filled with random numbers from a standard normal distribution. Setting the random seed at the beginning will generate the same numbers every time you run this code. Basic matrix operations and transpose operation in PyTorch are also similar to NumPy. 

Common PyTorch Modules

In PyTorch, modules are used to represent neural networks. 

  • Autograd

The autograd module is PyTorch’s automatic differentiation engine that helps to compute the gradients in the forward pass in quick time. Autograd generates a directed acyclic graph where the leaves are the input tensors while the roots are the output tensors. 

  • Optim

The Optim module is a package with pre-written algorithms for optimizers that can be used to build neural networks. 

Dynamic Computation Graph

Computational graphs in PyTorch allow the framework to calculate gradient values for the neural networks built. PyTorch uses dynamic computational graphs. The graph is defined indirectly using operator overloading while the forward computation gets executed. Dynamic graphs are more flexible than static graphs, wherein users can make interleaved construction and valuation of the graph. These are debug-friendly as it allows line-by-line code execution. Finding problems in code is a lot easier with PyTorch Dynamic graphs – an important feature that makes PyTorch such a preferred choice in the industry. 

Computational graphs in PyTorch are rebuilt from scratch at every iteration, allowing the use of random Python control flow statements, which can impact the overall shape and size of the graph every time an iteration occurs. The advantage is – there’s no need to encode all possible paths before launching the training. You run what you differentiate.

Data Loader

Working with large datasets requires loading all data into memory in one go. This causes memory outage, and programs run slowly. Besides, it’s hard to maintain data samples processing code. PyTorch offers two data primitives - Data Loader and Dataset – for parallelizing data loading with automated batching and better readability and modularity of codes. Datasets and Data Loader allow users to use their own data as well as pre-loaded datasets. While Dataset houses the samples and the respective labels, DataLoader combines dataset and sampler and implements an iterable around the Dataset so users can easily access samples.

Key Features of PyTorch

Some of the key features of PyTorch include:

  • TorchScript- This is the production environment of PyTorch that enables users to seamlessly transition between modes. Torch script optimizes functionality, speed, ease-of-use and flexibility.
  • Dynamic graph computation- This feature allows users to change network behavior on the fly, rather than waiting for the entire code to be executed.
  • Automatic differentiation- This technique numerically computes the derivative of a function by making backward passes in neural networks.
  • Python support- Because PyTorch is based on Python, it can be used with popular libraries and packages such as NumPy, SciPy, Numba and Cynthon.

PyTorch Benefits

Using PyTorch can provide developers with the following benefits:

  • Is based on Python, making it easy to learn and simple to code.
  • Allows for easy debugging with popular Python tools.
  • Is well supported on major cloud platforms, making it easy to scale.
  • Has a small community of focused on open source.
  • Can export learning models to the Open Neural Network Exchange (ONNX) standard format.
  • Has a user-friendly interface.
  • Offers a C++ front end interface option.

PyTorch vs. Tensorflow

PyTorch is often compared to Tensorflow, a deep learning framework developed by Google. Because Tensorflow has been around longer, it has a larger community of developers and more documentation.

However, PyTorch does come with its advantages over Tensorflow. PyTorch defines computational graphs in a dynamic way, unlike the static approach of Tensorflow. Dynamic graphs can be manipulated in real time instead of at the end. Additionally, Tensorflow has a steeper learning curve as PyTorch is based on intuitive Python.

Tensorflow may be better suited for projects that require production models and scalability, as it was created with the intention of being production-ready. PyTorch is easier and lighter to work with, making it a good option for creating prototypes quickly and conducting research.

要查看或添加评论,请登录

Anjali Kumari的更多文章

  • Blockchain Technology

    Blockchain Technology

    For all you’ve probably heard about Bitcoin, Ethereum, and other cryptocurrencies lately, many financial experts say…

  • Apache Airflow

    Apache Airflow

  • STLC

    STLC

  • DBI SQL

    DBI SQL

  • Azure Data Factory

    Azure Data Factory

  • Backend Developer

    Backend Developer

    What Is The Role Of A Back-End Developer The back-end developers generally work along with the front-end developers as…

  • DAX

    DAX

    What is DAX DAX stands for Data Analysis Expressions, it is language developed by Microsoft to interact with data in a…

  • GitHub

    GitHub

    GitHub is a code hosting platform for version control and collaboration. It lets you and others work together on…

  • JIRA

    JIRA

    A Jira 'issue' refers to a single work item of any type or size that is tracked from creation to completion. For…

  • Data Repository

    Data Repository

    Data Repository: Types, Challenges, and Best Practice The importance of data is growing as everyone uses data to make…

社区洞察

其他会员也浏览了