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

Starting with PyTorch functions

PyTorch has long been the preferred deep-learning library for researchers, used primarily for applications such as computer vision and natural language processing.

  • torch.as_tensor
  • torch.set_default_tensor_type()
  • torch.where(condition, t1, t2)
  • torch.reshape()
  • torch.from_numpy(ndarray)

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

Converts any data type to tensor type

torch.as_tensor(data, dtype=None, device=None)

# Example 1 - working 
list1=[1,2,3,4]
list1=torch.as_tensor(list1)
type(list1)
torch.Tensor