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

5 Interesting Pytorch operations.

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 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.

  1. Creating Ops - torch.eye & torch.complex

    • torch.eye -> Returns a 2-D tensor with ones on the diagonal and zeros elsewhere.

    • torch.complex -> Constructs a complex tensor with its real part equal to real and its imaginary part equal to imag.

  2. Indexing,Slicing,Joining,Mutating Ops - torch.narrow & torch.unbind

    • torch.narrow -> Returns a new tensor that is a narrowed version of input tensor.

    • torch.unsqueeze -> Returns a new tensor with a dimension of size one inserted at the specified position.

  3. Math operations(Pointwise Ops) - torch.imag

    • torch.imag -> Returns a new tensor containing imaginary values of the self tensor.
  • Function 1 - torch.eye
  • Function 2 - torch.complex
  • Function 3 - torch.narrow
  • Function 4 - torch.unsqueeze
  • Function 5 - torch.imag

Before we begin, let's install and import PyTorch

# Uncomment and run the appropriate command for your operating system, if required

# Linux / Binder
# !pip install numpy torch==1.7.0+cpu torchvision==0.8.1+cpu torchaudio==0.7.0 -f https://download.pytorch.org/whl/torch_stable.html

# Windows
!pip install numpy torch==1.7.0+cpu torchvision==0.8.1+cpu torchaudio==0.7.0 -f https://download.pytorch.org/whl/torch_stable.html

# MacOS
# !pip install numpy torch torchvision torchaudio
Looking in links: https://download.pytorch.org/whl/torch_stable.html Requirement already satisfied: numpy in /usr/local/lib/python3.6/dist-packages (1.19.4) Collecting torch==1.7.0+cpu Downloading https://download.pytorch.org/whl/cpu/torch-1.7.0%2Bcpu-cp36-cp36m-linux_x86_64.whl (159.3MB) |████████████████████████████████| 159.3MB 78kB/s Collecting torchvision==0.8.1+cpu Downloading https://download.pytorch.org/whl/cpu/torchvision-0.8.1%2Bcpu-cp36-cp36m-linux_x86_64.whl (11.8MB) |████████████████████████████████| 11.8MB 26.4MB/s Collecting torchaudio==0.7.0 Downloading https://files.pythonhosted.org/packages/3f/23/6b54106b3de029d3f10cf8debc302491c17630357449c900d6209665b302/torchaudio-0.7.0-cp36-cp36m-manylinux1_x86_64.whl (7.6MB) |████████████████████████████████| 7.6MB 4.5MB/s Requirement already satisfied: dataclasses in /usr/local/lib/python3.6/dist-packages (from torch==1.7.0+cpu) (0.8) Requirement already satisfied: future in /usr/local/lib/python3.6/dist-packages (from torch==1.7.0+cpu) (0.16.0) Requirement already satisfied: typing-extensions in /usr/local/lib/python3.6/dist-packages (from torch==1.7.0+cpu) (3.7.4.3) Requirement already satisfied: pillow>=4.1.1 in /usr/local/lib/python3.6/dist-packages (from torchvision==0.8.1+cpu) (7.0.0) Installing collected packages: torch, torchvision, torchaudio Found existing installation: torch 1.7.0+cu101 Uninstalling torch-1.7.0+cu101: Successfully uninstalled torch-1.7.0+cu101 Found existing installation: torchvision 0.8.1+cu101 Uninstalling torchvision-0.8.1+cu101: Successfully uninstalled torchvision-0.8.1+cu101 Successfully installed torch-1.7.0+cpu torchaudio-0.7.0 torchvision-0.8.1+cpu
# Import torch and other required modules
import torch

Function 1 - TORCH.EYE

 Returns a 2-D tensor with ones on the diagonal and zeros elsewhere.

Syntax: torch.eye(n)

Parameters:

- n (int) – the number of rows
- m (int, optional) – the number of columns with default being n
- out (Tensor, optional) – the output tensor.
- dtype (torch.dtype, optional) – the desired data type of returned  tensor.  Default: if None, uses a global default (see torch.set_default_tensor_type()).

- layout (torch.layout, optional) – the desired layout of returned Tensor.   Default: torch.strided.

- device (torch.device, optional) – the desired device of returned tensor. Default: if None, uses the current device for the default tensor type (see torch.set_default_tensor_type()). device will be the CPU for CPU tensor types and the current CUDA device for CUDA tensor types.

- requires_grad (bool, optional) – If autograd should record operations on the returned tensor. Default: False.
# Example 1 - working
t1 = torch.eye(4)
t1
tensor([[1., 0., 0., 0.],
        [0., 1., 0., 0.],
        [0., 0., 1., 0.],
        [0., 0., 0., 1.]])