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

Tensor operations

Submission for coursework #1

A short introduction about PyTorch and about the chosen functions.

  • torch.cat() for tensor concatenation
  • torch.dist() for p-norms between tensors
  • torch.solve() for for solving systems of linear equations
  • torch.matmul() for matrix products of two tensors
  • torch.Tensor.map() for applying a defined callable function to a tensor and an input tensor
# Import torch and other required modules
import torch

Function 1 - torch.cat (tensors,dim=0,out=None)

This function allows a sequence of tensors to be concatenated along a given dimension. These must be consistent in shape, except along the specified dimension or be empty. dim specifies the dimension along which concatenation occurs (defaults to 0, the first dimension). out specifies an output tensor and supresses the print output is one is defined.

# We begin by initiating a tensor of size (3,2) filled with random numbers 
x = torch.randn(3,2)
print(x)
tensor([[ 0.9678, -1.2386], [ 0.3548, -0.0435], [-0.9744, -0.4675]])

We can then use the function torch.cat((x,x),dim=0,out=None) to 'tile' the tensor along the 0-dimension.