Learn practical skills, build real-world projects, and advance your career
Updated 3 years ago
# My 5 Pytorch functions.
An short introduction about PyTorch and about the chosen functions.
- Take
- As_tensor
- Expand
- Range
- Unfold
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.TAKE
Returns a new tensor with the elements of input(Tensor) – the input tensor. The input tensor is treated as if it were viewed as a 1-D tensor. The result takes the same shape as the indices(LongTensor) – the indices into tensor
# Example 1 - torch.take
src = torch.tensor([[0, 3, 2],
[6, 7, 11]])
torch.take(src, torch.tensor([0, 2, 5]))
tensor([ 0, 2, 11])