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

5 PyTorch functions for Machine Learning Projects

PyTorch is a Machine Learning library with increasing popularity. PyTorch's ease of use combined with the default eager execution mode for easier debugging predestines it to be used for fast, hacky solutions and smaller-scale models.

First, we will import PyTorch using import torch and then we will discuss and use these functions.

  • torch.linspace
  • torch.eye
  • torch.full
  • torch.cat
  • torch.take

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.18.5) 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 91kB/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.5MB/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.0MB/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.linspace

torch.linspace is used to create a 1D equally spaced tensor between the values start and end . We can specify the size of the tensor with the steps parameters. The default is steps=100

torch.linspace(1, 10)
/usr/local/lib/python3.6/dist-packages/ipykernel_launcher.py:1: UserWarning: Not providing a value for linspace's steps is deprecated and will throw a runtime error in a future release. This warning will appear only once per process. (Triggered internally at /pytorch/aten/src/ATen/native/RangeFactories.cpp:23.) """Entry point for launching an IPython kernel.
tensor([ 1.0000,  1.0909,  1.1818,  1.2727,  1.3636,  1.4545,  1.5455,  1.6364,
         1.7273,  1.8182,  1.9091,  2.0000,  2.0909,  2.1818,  2.2727,  2.3636,
         2.4545,  2.5455,  2.6364,  2.7273,  2.8182,  2.9091,  3.0000,  3.0909,
         3.1818,  3.2727,  3.3636,  3.4545,  3.5455,  3.6364,  3.7273,  3.8182,
         3.9091,  4.0000,  4.0909,  4.1818,  4.2727,  4.3636,  4.4545,  4.5455,
         4.6364,  4.7273,  4.8182,  4.9091,  5.0000,  5.0909,  5.1818,  5.2727,
         5.3636,  5.4545,  5.5455,  5.6364,  5.7273,  5.8182,  5.9091,  6.0000,
         6.0909,  6.1818,  6.2727,  6.3636,  6.4545,  6.5455,  6.6364,  6.7273,
         6.8182,  6.9091,  7.0000,  7.0909,  7.1818,  7.2727,  7.3636,  7.4545,
         7.5455,  7.6364,  7.7273,  7.8182,  7.9091,  8.0000,  8.0909,  8.1818,
         8.2727,  8.3636,  8.4545,  8.5455,  8.6364,  8.7273,  8.8182,  8.9091,
         9.0000,  9.0909,  9.1818,  9.2727,  9.3636,  9.4545,  9.5455,  9.6364,
         9.7273,  9.8182,  9.9091, 10.0000])