Learn practical skills, build real-world projects, and advance your career
Created 4 years ago
Pytorch Zero to GANS Assignment 1
5 Tensor Operations
Pytorch is a tensor operations library used mainly for deep learning. In this notebook I introduce five Tensor operations.
- torch.Tensor.contiguous
- torch.Tensor.expand
- torch.Tensor.fill_diagonal_
- torch.Tensor.index_add_
- torch.Tensor.index_fill_
# Import torch and other required modules
import torch
Function 1 - torch.Tensor.contiguous
Each newly created tensor is contiguous, but if you transpose it, expand it etc. you get a non-contiguous Tensor, Which in some cases is problematic. The contiguous function creates a contiguous copy of the Tensor if it isn't contiguous already, otherwise it returns self.
# Example 1 - working
x = torch.Tensor([[1, 2], [3, 4.]])
y = torch.transpose(x,0 , 1)
y = y.contiguous()
y
tensor([[1., 3.],
[2., 4.]])
A basic use of contiguous. When y is created, its simply a different way to look at x. After using contiguous, a copy of y is created that looks just like y for a human but has a different representation in memory, specifically, just like if we would create a whole new Tensor with the data stored in y.