Interesting Tensor Operations with Pytorch
By: Adesh Gadge
Tags: Deep Learning, Machine Learning, Python, Data Science, Pytorch

This article contains 5 interesting functions from the Torch Library which I found interesting.

PyTorch is an open-source machine learning library based on the Torch library. It developed by Facebook’s AI Research lab. This blog will cover the following 5 tensor operations.

torch.det(input) → Tensor : The determinant of a square matrix or batches of square matrices.

: The determinant of a square matrix or batches of square matrices. torch.svd(input, some=True, compute_uv=True, out=None) -> (Tensor, Tensor, Tensor) : Returns singular value decompositions of a matrix

: Returns singular value decompositions of a matrix torch.addcmul(input, tensor1, tensor2, *, value=1, out=None) → Tensor : Returns tensor1 x tensor2 x value + input

: Returns tensor1 x tensor2 x value + input torch.ne(input, other, out=None) → Tensor : Computes input != other element wise

: Computes != element wise torch.cartesian_prod(*tensors) : Cartesian product of the given sequence of tensors

Let's start with importing the PyTorch library

Function 1 — torch.det(input) → Tensor

This function finds determinant of a square matrix or batches of square matrices. The determinant is also known as the product of all eigenvalues of that matrix. The determinant of a matrix describes the way matrix will scale the other matrix is multiplied together. The determinant is zero when the matrix has no inverse.

Let’s see some examples of this operation.

The determinant of the matrix A is calculated as (1 x 1) — 2 x 2= -3

The determinant of the randomly generated matrices is calculated by calculating the determinant for each square matrix.

Here we get an error because matrix has to be square in order to calculate the determinant for this function to work as only square matrices have determinant value.

Final Remarks:

Utilize this function for finding determinants of the square matrices

Function 2 — torch.svd(input, some=True, compute_uv=True, out=None) -> (Tensor, Tensor, Tensor)

This function returns a named tuple (U, S, V) which is the singular value decomposition of an input real matrix or batches of real matrices input such that input =U×diag(S)×V⊺ (Where V⊺ is the transpose of V)

Matrix A is decomposed into U, S, and V matrices.

If the compute_uv parameter, set to false, does not calculate the U and V values of the decomposition. Values of V and U are set to zero matrices.

Here we get an error becausethe data type of the values in tensor has to be float in order to calculate the SVD decomposition.

Final Remarks:

To calculate SVD Decomposition of matrices this function should be used.

Function 3 — torch.addcmul(input, tensor1, tensor2, *, value=1, out=None) → Tensor

Performs the element-wise multiplication of tensor1 by tensor2 , multiply the result by the scalar value and add it to input .

Here, value =1, tensor1 = t1, tensor2=t2 , and input =t

And the answer returned is t1 x t2 x 1 + t

Here, value =3, tensor1 = t1, tensor2 =t2, and input =t

And the answered returned is t1 x t2 x 3 + t

Here we get an error because t1 and t2 are not broadcastable as their dimensions do not match.

Final remarks:

This function can be used for linear combinations of matrices

Function 4 — torch.ne(input, other, out=None) → Tensor

This function is used for element-wise comparing the two tensors. Returns False at the tensor positions where the elements match and True at the other places.

Element wise comparison is done and False is returned for not the same values and True is returned for the same values.

During comparison 1.0 and 1 and True are the same.

Here arguments input and other do not have the same dimensions and hence can't be compared elementwise.

Final Remark:

This function can be used to compare if the two tensors have the same value at respective positions.

Function 5 — torch.cartesian_prod(*tensors)

Cartesian product of the given sequence of tensors. The behavior is similar to python’s itertools.product.

Cartesian product of A and B is found. Each element from A is paired with each element from B.

If elements are repeated in the particular 1-D tensor, the cartesian product has the pair repeated too.

The cartesian_prod function has to have a 1D tensor vector as inputs.

Final Remark:

This function can be used for calculating the cartesian_product between two 1D tensors.

Conclusion

This notebook covered some of the operations that can be performed on the tensors, such as, finding determinant, SVD decomposition, linearly combining tensors, comparing tensors, finding the cartesian product. More information about other tensor functions could be found at the official documentation link provided in the reference links section.

Reference Links

Provide links to your references and other interesting articles about tensors