Learn practical skills, build real-world projects, and advance your career

PyTorch

Introduction

PyTorch is an open source machine learning library based on the Torch library, used for applications such as computer vision and natural language processing primarily developed by Facebook's AI Research lab (FAIR).

PyTorch is an open source deep learning framework built to be flexible and modular for research, with the stability and support needed for production deployment. PyTorch provides a Python package for high-level features like tensor computation (like NumPy) with strong GPU acceleration and TorchScript for an easy transition between eager mode and graph mode. With the latest release of PyTorch, the framework provides graph-based execution, distributed training, mobile deployment, and quantization.

  • torch.tensor()
  • torch.randn()
  • tensor.view()
  • tensor.permute()
  • tensor.transpose()
# Import torch and other required modules
import torch

Function 1 - torch.tensor()

  • torch.Tensor:

    • It is a multi-dimensional matrix containing elements of a single data type.
      *A tensor can be constructed from a Python list or sequence using the torch.tensor() constructor
    • Syntax:
      torch.tensor(data, dtype=None, device=None, requires_grad=False, pin_memory=False)
# Example 1 - working (change this)
a = torch.tensor([[1, 2], [3, 4.]])
a
tensor([[1., 2.],
        [3., 4.]])

Generates a 2d tensor of float values of dimension (2, 2), whose gardient will not be computed during back propagation