What are PyTorch tensors?

Updated: April 14, 2023 By: Goodman Post a comment

A tensor in PyTorch is a multi-dimensional matrix containing elements of a single data type. Tensors are similar to NumPy arrays but can also be operated on a CUDA-capable NVIDIA GPU.

A tensor can have any number of dimensions and any shape as long as all the numbers have the same type. For example, you can have a tensor of integers or a tensor of floats (decimal numbers) but not a mix of both.

Below are some examples of PyTorch tensors showcasing various dimensions and data types.

Scalar (0-dimensional tensor):

import torch

scalar_tensor = torch.tensor(1.11223344)
print(scalar_tensor)
# tensor(1.1122)

Vector (1-dimensional tensor):

import torch

vector_tensor = torch.tensor([1.0, 2.0, 3.0, 4.0])
print(vector_tensor)
# tensor([1., 2., 3., 4.])

Matrix (2-dimensional tensor):

import torch

matrix_tensor = torch.tensor([[1.0, 2.0], [3.0, 4.0], [5.0, 6.0]])
print(matrix_tensor)

Output:

tensor([[1., 2.],
        [3., 4.],
        [5., 6.]])

4-dimensional tensor:

tensor_4d = torch.tensor([[[[1, 2], [3, 4]], [[5, 6], [7, 8]]], [[[9, 10], [11, 12]], [[13, 14], [15, 16]]]])
print(tensor_4d)

Output:

tensor([[[[ 1,  2],
          [ 3,  4]],

         [[ 5,  6],
          [ 7,  8]]],


        [[[ 9, 10],
          [11, 12]],

         [[13, 14],
          [15, 16]]]])

Tensor with specified data type:

import torch

int_tensor = torch.tensor([1, 2, 3], dtype=torch.int64)
print(int_tensor.dtype)

Output:

torch.int64