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

PyTorch basic matrix operations functions

inverse, determinant, upper and lower triangular of matrix

  • torch.clamp(input, min, max, out=None)
  • torch.roll(input, shifts, dims=None)
    some matrix operations :-
  • torch.tril(input, diagonal=0, out=None) lower triangular matrix
  • torch.triu(input, diagonal=0, out=None) upper triangular matrix
  • torch.inverse(input, out=None),torch.det()
# Import torch and other required modules
import torch

Function 1 - torch.clamp(input, min, max, out=None)

   - torch.clamp() clamp all inputs in the range [min,max]

Three situations if :-

  • input is less than min. than the function will assign the min value at the output
  • input is greater than max than the function will assign the max value at the output
  • input is in the range of min and max the the value at output remain same as input
a = torch.randn(4)
print(a)
torch.clamp(a,min = 0.5, max = 1.0)
tensor([ 0.6743, -0.8689, 1.1793, -0.3802])
tensor([0.6743, 0.5000, 1.0000, 0.5000])
  • as a2 = *-0.8689 which is less than *0.5 so the clamp function will assign it the minimum value which is *0.5
  • as a2 = *1.1793 which is greater than *1.0 so the clamp function will assign it the maximum value which is *1.0
  • as a1 = 0.6743 is in the range at the output the value remain same