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

5 Useful Pytorch methods for Data Science

Pytorch is an open-source machine learning library which is used for deep learning.Pytorch can be used in computer vision and natural language processing.Pytorch is flexible as it allows user to change the deep learning models using basic Python.

  • torch.cat
  • torch.reshape
  • torch.index_select
  • torch.utils.data.TensorDataset
  • torch.from_numpy

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

Torch.cat is used to concatenate tensors in the given dimensions

t1=torch.tensor([[1,2],[3,4]])
t2=torch.tensor([[5,6],[7,8]])

t3=torch.cat((t1,t2))
t3
tensor([[1, 2],
        [3, 4],
        [5, 6],
        [7, 8]])