Learn practical skills, build real-world projects, and advance your career
Updated 3 years ago
5 PyTorch functions for Indexing, Slicing, Joining, Mutating Ops
The torch package contains data structures for multi-dimensional tensors and mathematical operations over these are defined. Additionally, it provides many utilities for efficient serializing of Tensors and arbitrary types, and other useful utilities.
- chunk
- dstack
- hstack
- index_select
- masked_select
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 - chunk
This split a tensor in a specif number of chunks, Each chunk is a view of input tensor.
# Example 1 - working (change this)
t1 = torch.tensor([[1, 2], [3, 4.],[5,6]])
torch.chunk(t1,3,0)
(tensor([[1., 2.]]), tensor([[3., 4.]]), tensor([[5., 6.]]))