Learn practical skills, build real-world projects, and advance your career
Updated 3 years ago
Here I will discuss about 5 different functions of torch with examples as a part of Assignment of pytorch tutorial by freecodecamp and jovian. The 5 different fuctions discussed here are:
- torch.eye(n, m=None, *, out=None, dtype=None, layout=torch.strided, device=None, requires_grad=False)
- torch.complex(real, imag, *, out=None)
- torch.reshape(input, shape)
- torch.rand(*size, *, out=None, dtype=None, layout=torch.strided, device=None, requires_grad=False)
- torch.narrow(input, dim, start, length)
# 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)
Requirement already satisfied: torch==1.7.0+cpu in /usr/local/lib/python3.6/dist-packages (1.7.0+cpu)
Requirement already satisfied: torchvision==0.8.1+cpu in /usr/local/lib/python3.6/dist-packages (0.8.1+cpu)
Requirement already satisfied: torchaudio==0.7.0 in /usr/local/lib/python3.6/dist-packages (0.7.0)
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)
# 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.
#Example 1- Working
torch.eye(3)
tensor([[1., 0., 0.],
[0., 1., 0.],
[0., 0., 1.]])