Shahid Akhtar Khan

Shahid Akhtar Khan

169 Articles Published

Articles by Shahid Akhtar Khan

Page 14 of 17

PyTorch – How to compute the norm of a vector or matrix?

Shahid Akhtar Khan
Shahid Akhtar Khan
Updated on 07-Jan-2022 1K+ Views

To compute the norm of a vector or a matrix, we could apply torch.linalg.norm() method. It returns a new tensor with computed norm. It accepts a vector, matrix, a batch of matrices and also batches of matrices.A vector is a 1D torch Tensor where a matrix is a 2D torch Tensor. It supports input of float, double, cfloat, and cdouble data types. We can compute the norm of the matrix or batch/es of matrices along the different dimensions. For example, we could compute the norm of a matrix along dimension 0 or along dimension1.Syntaxtorch.linalg.norm(A)A is a vector, matrix or batch/s ...

Read More

PyTorch – torch.linalg.solve() Method

Shahid Akhtar Khan
Shahid Akhtar Khan
Updated on 07-Jan-2022 932 Views

To solve a square system of linear equations with unique solution, we could apply the torch.linalg.solve() method. This method takes two parameters −first, the coefficient matrix A, andsecond, the right-hand tensor b.Where A is a square matrix and b is a vector. The solution is unique if A invertible. We can solve a number of systems of linear equations. In this case, A is a batch of square matrices and b is a batch of vectors.Syntaxtorch.linalg.solve(A, b)ParametersA – Square matrix or batch of square matrices. It is the coefficient matrix of system of linear equations.b – Vector or a batch ...

Read More

PyTorch – How to compute the determinant of a square matrix?

Shahid Akhtar Khan
Shahid Akhtar Khan
Updated on 07-Jan-2022 1K+ Views

To compute the determinant of a square matrix, we could apply torch.linalg.det() method. It returns a new tensor with computed determinant. It accepts a square matrix, a batch of square matrices and also batches of square matrices. It supports matrix of float, double, cfloat, and cdouble data types.We could also apply torch.det() method to compute the determinant. It is an alias of the torch.linalg.det() method.Syntaxtorch.linalg.det(mat) torch.det(mat)Where mat is a square matrix or batch/s of square matrices. A matrix is a 2D torch tensor.StepsWe could use the following steps to compute determinant of a square matrix −Import the required library. In ...

Read More

PyTorch – How to compute the logistic sigmoid function of tensor elements?

Shahid Akhtar Khan
Shahid Akhtar Khan
Updated on 07-Jan-2022 1K+ Views

To compute the logistic function of elements of a tensor, we use torch.special.expit() method. It returns a new tensor with computed logistic function element-wise. It accepts torch tensor of any dimension. We could also apply torch.sigmoid() method to compute the logistic function of elements of the tensor. It is an alias of the torch.special.expit() method.Syntaxtorch.special.expit(input) torch.sigmoid(input)Where input is a torch tensor of any dimension.StepsWe could use the following steps to compute logistic sigmoid function of a tensor element-wise −Import the required library. In all the following examples, the required Python library is torch. Make sure you have already installed it.import ...

Read More

PyTorch – How to compute QR decomposition of a matrix?

Shahid Akhtar Khan
Shahid Akhtar Khan
Updated on 07-Jan-2022 417 Views

torch.linalg.qr() computes the QR decomposition of a matrix or a batch of matrices. It accepts matrix and batch of matrices of float, double, cfloat and cdouble data types.It returns a named tuple (Q, R). Q is orthogonal when the matrix is real valued and unitary when matrix is complex valued. And R is an upper triangular matrix.Syntax(Q, R) = torch.linalg.qr(mat, mode='reduced')ParametersMat – Square matrix or a batch of square matrices.mode – It decides mode of QR decomposition. It is set to one of three modes, 'reduced', 'complete', and 'r'. Default is set to 'reduced'. It's an optional parameter.StepsImport the required library. In ...

Read More

PyTorch – How to compute the error function of a tensor?

Shahid Akhtar Khan
Shahid Akhtar Khan
Updated on 07-Jan-2022 448 Views

To compute the error function of a tensor, we use the torch.special.erf() method. It returns a new tensor with computed error function. It accepts torch tensor of any dimension. It is also known as Gauss error functionStepsWe could use the following steps to compute the error function of a tensor element-wise −Import the required library. In all the following examples, the required Python library is torch. Make sure you have already installed it.import torchDefine a torch tensor. Here we define a 2D tensor of random numbers.tensor = torch.randn(2, 3, 3)Compute the error function of the above-defined tensor using torch.special.erf(tensor). Optionally ...

Read More

PyTorch – How to compute the eigenvalues and eigenvectors of a square matrix?

Shahid Akhtar Khan
Shahid Akhtar Khan
Updated on 07-Jan-2022 2K+ Views

torch.linalg.eig() computes the Eigen value decomposition of a square matrix or a batch of square matrices. It accepts matrix and batch of matrices of float, double, cfloat and cdouble data types. It returns a named tuple (eigenvalues, eigenvectors). The eigenvalues and eigenvectors are always complex valued. The eigenvectors are given by columns of eigenvectors.Syntax(eigenvalues, eigenvectors) = torch.linalg.eig(A)Where A is a square matrix or a batch of square matrices. It returns a named tuple (eigenvalues, eigenvectors).StepsImport the required library. In all the following examples, the required Python library is torch. Make sure you have already installed it.import torchCreate a square matrix ...

Read More

PyTorch – How to invert the colors of an image randomly with a given probability?

Shahid Akhtar Khan
Shahid Akhtar Khan
Updated on 06-Jan-2022 779 Views

The RandomInvert() transform inverts the colors of an image randomly with a given probability. The torchvision.transforms module provides many important transforms that can be used to perform different types of manipulations on the image data.RandomInvert() accepts both PIL and tensor images or batch of tensor images. A tensor image is a PyTorch Tensor with shape [3, H, W], where H is the image height and W is the image width. A batch of tensor images is also a torch tensor with [B, 3, H, W] where B is the number of images in the batch.Syntaxtorchvision.transforms.RandomInvert(p)(img)It returns a randomly color inverted ...

Read More

PyTorch – How to rotate an image by an angle?

Shahid Akhtar Khan
Shahid Akhtar Khan
Updated on 06-Jan-2022 6K+ Views

RandomRotation() rotates an image by a random angle. The chosen random angle is from a given range of angles in degree. RandomRotation() is one of the many important transforms provided by the torchvision.transforms module. RandomRotation() transform accepts both PIL and tensor images.A tensor image is a Torch tensor with shape [C, H, W], where C is the number of channels, H is the image height, and W is the image width. If the image is neither a PIL image nor a tensor image, then we first convert it to a tensor image and then apply the transform.Syntaxtorchvision.transforms.RandomRotation(degree)(img)Where degree is the ...

Read More

How to pad an image on all sides in PyTorch?

Shahid Akhtar Khan
Shahid Akhtar Khan
Updated on 06-Jan-2022 3K+ Views

To pad an image on all sides, we can apply Pad() transform provided by the torchvision.transforms module. This module contains many important transformations that can be used to perform different types of manipulations on the image data.Pad() transformation accepts both PIL and tensor images or a batch of tensor images. A tensor image is a torch Tensor with shape [C, H, W], where C is the number of channels, H is the image height, and W is the image width.A batch of tensor images is also a torch tensor with shape [B, C, H, W]. B is the number of ...

Read More
Showing 131–140 of 169 articles
Advertisements