Python

# PyTorch – Sqrt()

We will return the square root of all elements in the tensor using the sqrt() method in this PyTorch tutorial.

PyTorch is an open-source framework available with a Python programming language. Tensor is a multidimensional array that is used to store the data. To use a tensor, we have to import the torch module. To create a tensor, the method used is tensor().

Syntax:

torch.tensor(data)

Where the data is a multi-dimensional array.

## Torch.sqrt()

Sqrt() in PyTorch returns the square root of every element in the PyTorch tensor. It takes one parameter.

Syntax:

torch.sqrt(tensor_object)

Parameter:

tensor_object is a tensor

## Example 1:

In this example, we will create a tensor with one dimension that has 5 elements and return the square roots of these 5 elements in a tensor.

#import torch module

import torch

#create a tensor

data1 = torch.tensor([12,34,56,1,10])

#display

print("Actual Tensor: ")

print(data1)

print("Square Root: ")

print(torch.sqrt(data1))

Output:

Actual Tensor:

tensor([12, 34, 56, 1, 10])

Square Root:

tensor([3.4641, 5.8310, 7.4833, 1.0000, 3.1623])

Working:

1. √12 =3.4641
2. √34 = 5.8310
3. √56 =7.4833
4. √1 =1.0000
5. √10 =3.1623

## Example 2:

In this example, we will create a tensor with two dimensions that has 5 elements in each row and return the square root of elements.

#import torch module

import torch

#create a 2D tensor

data1=torch.tensor([[45,67,21,23,2],[2,3,4,5,6]])

#display

print("Actual Tensor: ")

print(data1)

print("Square Root:")

print(torch.sqrt(data1))

Output:

Actual Tensor:

tensor([[45, 67, 21, 23, 2],

[ 2, 3, 4, 5, 6]])

Square Root:

tensor([[6.7082, 8.1854, 4.5826, 4.7958, 1.4142],

[1.4142, 1.7321, 2.0000, 2.2361, 2.4495]])

Working:

1. √45 = 6.7082, √2 = 1.4142
2. √67 = 8.1854,√3=1.7321
3. √21 = 4.5826,√4=2.0000
4. √23 = 4.7958,√5=2.2361
5. √2 = 1.4142,√6=2.4495

### Work with CPU

If you want to run a sqrt() function on the CPU, we have to create a tensor with a cpu() function. This will run on a CPU machine.

When we create a tensor, this time, we can use the cpu() function.

Syntax:

torch.tensor(data).cpu()

## Example 1:

In this example, we will create a tensor with one dimension that has 5 elements on the cpu and return the square roots of these 5 elements in a tensor.

#import torch module

import torch

#create a tensor

data1 = torch.tensor([12,34,56,1,10]).cpu()

#display

print("Actual Tensor: ")

print(data1)

print("Square Root: ")

print(torch.sqrt(data1))

Output:

Actual Tensor:

tensor([12, 34, 56, 1, 10])

Square Root:

tensor([3.4641, 5.8310, 7.4833, 1.0000, 3.1623])

Working:

1. √12 =3.4641
2. √34 = 5.8310
3. √56 =7.4833
4. √1 =1.0000
5. √10 =3.1623

## Example 2:

In this example, we will create a tensor with two dimensions that has 5 elements on the cpu in each row and return the square root of elements.

#import torch module

import torch

#create a 2D tensor

data1=torch.tensor([[45,67,21,23,2],[2,3,4,5,6]]).cpu()

#display

print("Actual Tensor: ")

print(data1)

print("Square Root:")

print(torch.sqrt(data1))

Output:

Actual Tensor:

tensor([[45, 67, 21, 23, 2],

[ 2, 3, 4, 5, 6]])

Square Root:

tensor([[6.7082, 8.1854, 4.5826, 4.7958, 1.4142],

[1.4142, 1.7321, 2.0000, 2.2361, 2.4495]])

Working:

1. √45 = 6.7082, √2 = 1.4142
2. √67 = 8.1854,√3=1.7321
3. √21 = 4.5826,√4=2.0000
4. √23 = 4.7958,√5=2.2361
5. √2 = 1.4142,√6=2.4495

## Conclusion

In this PyTorch lesson, we discussed about the sqrt() function. It returns the square root of every element in the PyTorch tensor. We discussed the two examples with the different dimensional tensors to perform the sqrt() function.