Programming Articles - Page 828 of 3363

Python – scipy.linalg.sqrtm

Syed Abeed
Updated on 22-Dec-2021 10:07:13

1K+ Views

The sqrtm() function of scipy.linalg package can be used to find the square root of an input matrix.Syntaxscipy.linalg.sqrtm(x)Example 1Let us consider the following example −# Importing the required libraries from scipy from scipy import linalg import numpy as np # Define the input array x = np.array([[14 , 2] , [89 , 33]]) print("Input array:", x) # Calculate the square root r = linalg.sqrtm(x) # Display the square root print("Square Root of x: ", r)OutputIt will generate the following output −Input array: [[14 2] [89 33]] Square Root of x: [[3.43430132 0.22262855] [9.90697038 5.54927253]]Example 2Let us take ... Read More

Python – scipy.linalg.norm

Syed Abeed
Updated on 22-Dec-2021 10:05:34

617 Views

The norm() function of the scipy.linalg package is used to return one of eight different matrix norms or one of an infinite number of vector norms.Syntaxscipy.linalg.norm(x)Where x is an input array or a square matrix.Example 1Let us consider the following example −# Importing the required libraries from scipy from scipy import linalg import numpy as np # Define the input array x = np.array([7 , 4]) print("Input array:", x) # Calculate the L2 norm r = linalg.norm(x) # Calculate the L1 norm s = linalg.norm(x, 3) # Display the norm values print("Norm Value of r :", ... Read More

Python – scipy.linalg.inv

Syed Abeed
Updated on 22-Dec-2021 10:02:40

427 Views

The scipy.linalg package contains a of different functionalities that are used for Linear Algebra. One of them is the inv() function, which is used to find the inverse of a square matrix.Syntaxscipy.linalg.inv(x)Where x is a square matrix.Example 1Let us consider the following example −# Import the required libraries from scipy import linalg import numpy as np # defines the array a = np.array([[5, 3], [6, 4]]) print("Input matrix :", a) # Finding the inverse of a square matrix x = linalg.inv(a) print(" Inverse of Square Matrix A :", x)OutputThe above program will generate the following output −Input matrix ... Read More

Python – scipy.linalg.det

Syed Abeed
Updated on 22-Dec-2021 09:59:56

344 Views

The scipy.linalg package contains a set of different functionalities that are used for Linear Algebra. One of them is the det() function. This function is used to find the determinant of a two-dimensional matrix.Syntaxscipy.linalg.det(x)Where x is a square matrix.Example 1Let us consider the following example −# Importing the required libraries from scipy import linalg import numpy as np # Initialize the matrix A A = np.array([[8, 5], [3, 4]]) print("Input Matrix :", A) # Find the determinant of matrix X x = linalg.det(A) print("Determinant Value of A:", x)OutputIt will generate the following output −Input Matrix : [[8 5] ... Read More

Python – scipy.special.logsumexp

Syed Abeed
Updated on 22-Dec-2021 09:54:51

2K+ Views

The scipy.special package contains a set of different functionalities that are used for mathematical physics. One of them is the logsumexp() function. This function is used to compute the log of the sum of exponentials of input elements. Let us take a couple of examples and see how to use this function.Syntaxscipy.special.logsumexp(x)where, x is the input value.Example 1Let us consider the following example −# Import logsumexp from scipy.special from scipy.special import logsumexp import numpy as np # Input array a = np.arange(10) print("Input Array:", a) # logsum() function res = logsumexp(a) print("logsumexp of a:", res)OutputIt will produce the ... Read More

How to check if an object is a PyTorch Tensor?

Shahid Akhtar Khan
Updated on 06-Dec-2021 12:44:53

8K+ Views

To check if an object is a tensor or not, we can use the torch.is_tensor() method. It returns True if the input is a tensor; False otherwise.Syntaxtorch.is_tensor(input)Parametersinput – The object to be checked, if it is a tensor or not .OutputIt returns True if the input is a tensor; else False.StepsImport the required library. The required library is torch.Define a tensor or other object.Check if the created object is a tensor or not using torch.is_tensor(input).Display the result.Example 1# import the required library import torch # create an object x x = torch.rand(4) print(x) # check if the above ... Read More

What does "with torch no_grad" do in PyTorch?

Shahid Akhtar Khan
Updated on 06-Dec-2021 12:35:44

6K+ Views

The use of "with torch.no_grad()" is like a loop where every tensor inside the loop will have requires_grad set to False. It means any tensor with gradient currently attached with the current computational graph is now detached from the current graph. We no longer be able to compute the gradients with respect to this tensor.A tensor is detached from the current graph until it is within the loop. As soon as it is out of the loop, it is again attached to the current graph if the tensor was defined with gradient.Let's take a couple of examples for a better ... Read More

What does backward() do in PyTorch?

Shahid Akhtar Khan
Updated on 06-Dec-2021 12:33:18

3K+ Views

The backward() method is used to compute the gradient during the backward pass in a neural network.The gradients are computed when this method is executed.These gradients are stored in the respective variables.The gradients are computed with respect to these variables, and the gradients are accessed using .grad.If we do not call the backward() method for computing the gradient, the gradients are not computed.And, if we access the gradients using .grad, the result is None.Let's have a couple of examples to demonstrate how it works.Example 1In this example, we attempt to access the gradients without calling the backward() method. We notice ... Read More

PyTorch – How to check if a tensor is contiguous or not?

Shahid Akhtar Khan
Updated on 06-Dec-2021 12:29:28

2K+ Views

A contiguous tensor is a tensor whose elements are stored in a contiguous order without leaving any empty space between them. A tensor created originally is always a contiguous tensor. A tensor can be viewed with different dimensions in contiguous manner.A transpose of a tensor creates a view of the original tensor which follows non-contiguous order. The transpose of a tensor is non-contiguous.SyntaxTensor.is_contiguous()It returns True if the Tensor is contiguous; False otherwise.Let's take a couple of example to demonstrate how to use this function to check if a tensor is contiguous or non-contiguous.Example 1# import torch library import torch ... Read More

How to find the transpose of a tensor in PyTorch?

Shahid Akhtar Khan
Updated on 06-Dec-2021 12:20:28

8K+ Views

To transpose a tensor, we need two dimensions to be transposed. If a tensor is 0-D or 1-D tensor, the transpose of the tensor is same as is. For a 2-D tensor, the transpose is computed using the two dimensions 0 and 1 as transpose(input, 0, 1).SyntaxTo find the transpose of a scalar, a vector or a matrix, we can apply the first syntax defined below.And for any dimensional tensor, we can apply the second syntax.For

Advertisements