Found 26504 Articles for Server Side Programming

PyTorch – How to compute the pseudoinverse of a matrix?

Shahid Akhtar Khan
Updated on 07-Jan-2022 06:26:38

712 Views

To compute the pseudoinverse of a square matrix, we could apply torch.linalg.pinv() method. It returns a new tensor with pseudoinverse of the given matrix. It accepts a matrix, a batch of matrices and also batches of matrices. A matrix is a 2D torch Tensor. It supports input of float, double, cfloat, and cdouble data types.Syntaxtorch.linalg.pinv(M)Where M is a matrix or batches of matrices.StepsWe could use the following steps to compute the pseudoinverse of a matrix −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 matrix. ... Read More

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

Shahid Akhtar Khan
Updated on 07-Jan-2022 06:21:14

1K+ Views

To compute the inverse of a square matrix, we could apply torch.linalg.inv() method. It returns a new tensor with inverse of the given matrix. It accepts a square matrix, a batch of square matrices, and also batches of square matrices.A matrix is a 2D torch Tensor. It supports input of float, double, cfloat, and cdouble data types. The inverse matrix exists if and only if the square matrix is invertible.Syntaxtorch.linalg.inv(M)Where M is a square matrix or a batch of square matrices. It returns the inverse matrix.StepsWe could use the following steps to compute the inverse of a square matrix −Import ... Read More

PyTorch – torch.linalg.solve() Method

Shahid Akhtar Khan
Updated on 07-Jan-2022 06:15:03

827 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 QR decomposition of a matrix?

Shahid Akhtar Khan
Updated on 07-Jan-2022 06:10:48

343 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 eigenvalues and eigenvectors of a square matrix?

Shahid Akhtar Khan
Updated on 07-Jan-2022 06:08:11

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 compute the norm of a vector or matrix?

Shahid Akhtar Khan
Updated on 07-Jan-2022 06:16:07

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 – How to compute the determinant of a square matrix?

Shahid Akhtar Khan
Updated on 07-Jan-2022 06:14:10

997 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
Updated on 07-Jan-2022 06:12:35

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 the error function of a tensor?

Shahid Akhtar Khan
Updated on 07-Jan-2022 06:10:14

389 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 element-wise entropy of an input tensor?

Shahid Akhtar Khan
Updated on 06-Jan-2022 13:29:57

2K+ Views

To compute the element-wise entropy of an input tensor, we use torch.special.entr() method. It returns a new tensor with entropy computed element-wise.If the element of tensor is negative, the entropy is negative infinity.If the element of the tensor is a zero, the entropy is zero.The entropy for a positive number element is computed as the negative value of the element multiplied by its natural logarithm. It accepts torch tensor of any dimension.StepsWe could use the following steps to compute the entropy on a tensor element-wise −Import the required library. In all the following examples, the required Python library is torch. Make ... Read More

Advertisements