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

Pytorch Functions

Pytorch is a open source machine learning library for python & it supports GPU. We are covering some basic functions of pytorch.

  • torch.from_numpy
  • torch.mm
  • torch.cat
  • torch.chunk
  • torch.narrow

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
# Import torch and other required modules
import torch

Function 1 - torch.from_numpy

This Function is used to create tensor from numpy array.

# Example 1 - working (change this)
import numpy as np
a = np.array([32,35,321,98])
b = torch.from_numpy(a)
print(a)
print(b)
[ 32 35 321 98] tensor([ 32, 35, 321, 98])