Pytorch Matrix Functions

Pytorch has many inbuilt functions for operations on matrix. In this chapter of Pytorch Tutorial, you will learn about handful of these Pytorch matrix functions.

torch.t()

torch.t(x) returns the transpose of the x, where x is a tensor of dimensions 2 or less. The t() function does not make changes to the original tensor by default, It creates a new tensor to store the result and returns it. To make changes to the original tensor, you should call t_() function.

t() and t_() can also be called directly on the tensor.

Example

x = torch.tensor([[1, 2], [3, 4]])

# Does not make changes to tensor x, stores the result in y
y = torch.t(x)

print(y)
# Outputs- tensor([[1, 3], [2, 4]])

print(x.t())
# Ouputs- tensor([[1, 3], [2, 4]])

# Changes the original tensor
x.t_()

print(x)
# Outputs- tensor([[1, 3], [2, 4]])

torch.det()

torch.det(x) calculates returns the determinant of a square matrix x. Determinant can only be calculated for a square matrix. Hence, if the input x is not a square matrix, a RuntimeError will be raised.

det() can also be called directly on the tensor.

Example

x = torch.tensor([[1., 2.], [3., 4.]])
y = torch.det(x)

print(y)
# Outputs- tensor(-2.0000)

print(x.det())
# Ouputs- tensor(-2.0000)

Note– The det() function accepts tensors with floating point datatype only.


torch.matmul()

torch.matmul(tensor_1, tensor_2) calculates and returns the product of tensors tensor_1 and tensor_2.

Example

mat_1 = torch.tensor([[4, 1], [2, 3]])
mat_2 = torch.tensor([[2, 1], [5, 1]])

result_mat = torch.matmul(mat_1, mat_2)

print(result_mat)
# Outputs- tensor([[13, 5], [19, 5]])

Note– The tensors being multiplied need to be of compatible shapes. If the tensors are of non-compatible shapes, then a RuntimeError will be raised.

The matmul() function can also be called directly on a tensor and passing the second tensor as an argument to the function.

tensor_1.matmul(tensor_2) is therefore equivalent to torch.matmul(tensor_1, tensor_2).

Example

mat_1 = torch.tensor([[4, 1], [2, 3]])
mat_2 = torch.tensor([[2, 1], [5, 1]])

result_mat = mat_1.matmul(mat_2)

print(result_mat)
# Outputs- tensor([[13, 5], [19, 5]])

torch.inverse()

torch.inverse(x) calculates and returns the inverse of a square matrix x. Inverse can only be calculated for a square matrix. Hence, if the input x is not a square matrix, a RuntimeError will be raised.

inverse() can also be called directly on the tensor.

Example

x = torch.tensor([[1., 2.], [3., 1.]])
inv = torch.inverse(x)

print(inv)
# Outputs- tensor([[-0.2000, 0.4000], [0.6000, -0.2000]])

print(x.inv())
# Outputs- tensor([[-0.2000, 0.4000], [0.6000, -0.2000]])

Note– The inverse() function accepts tensors with floating point datatype only.

A non-invertible matrix is a matrix which has no inverse. The inverse function raises a RuntimeError when it is passed a non-invertible matrix. The determinant of non-invertible matrix is 0, whereas a matrix whose determinant is non-zero will always have an inverse.

Example

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

print(x.det())
# Outputs- tensor(0.)

print(x.inverse)
# Outputs- RuntimeError: inverse_cpu: U(3,3) is zero, singular U.

dim()

dim() function when called on a tensor returns the number of dimensions of the tensor.

Example

tensor_1 = torch.tensor([[1, 2], [3, 4]])
print(tensor_1.dim())
# Outputs- 2

tensor_2 = torch.tensor([[[1], [2]], [[3], [4]]])
print(tensor_2.dim())
# Outputs- 3

item()

item() function when called on a tensor with only one element will return a scalar.

tensor_1 = torch.tensor([4])
tensor_2 = torch.tensor([4, 7, 9])

tensor_1.item()
# Outputs- 4

tensor_2.item()
# Outputs- ValueError: only one element tensors can be converted to Python scalars

Note– When called on a tensor which has more than one element, a ValueError will be raised.


ndim attribute

Each Pytorch tensor has ndim attribute which will return the number of dimensions of the tensor. ndim is an alias for the dim() function.

tensor_1 = torch.tensor([[1, 2], [3, 4]])
print(tensor_1.ndim)
# Outputs- 2

tensor_2 = torch.tensor([[[1], [2]], [[3], [4]]])
print(tensor_2.ndim)
# Outputs- 3

T attribute

Each Pytorch tensor has T attribute which will return the transpose of the matrix.

Example

x = torch.tensor([[1., 2.], [3., 1.]])
print(x.T)
# Outputs- tensor([[1., 3.], [2., 1.]])