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

5 Types of operation on Tensor

A torch.Tensor is a multi-dimensional matrix containing elements of a single data type.

List of functions

  • torch.random
  • torch.mean
  • torch.arange
  • torch.dot
  • torch.log
# Import torch and other required modules
import torch

Function 1 - torch.rand

Create and tensor with random data

# Example 1 - We are creating an tensor with random number of 4*5 matrix.
tensor = torch.rand((4,5))
tensor
tensor([[0.1062, 0.8706, 0.4207, 0.8578, 0.5275],
        [0.5091, 0.6393, 0.4732, 0.5094, 0.7542],
        [0.0044, 0.1526, 0.4638, 0.9872, 0.1389],
        [0.1543, 0.6116, 0.7800, 0.2625, 0.9335]])

Explanation about example