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:
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:
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
#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:
tensor([12, 34, 56, 1, 10])
Square Root:
tensor([3.4641, 5.8310, 7.4833, 1.0000, 3.1623])
Working:
- √12 =3.4641
- √34 = 5.8310
- √56 =7.4833
- √1 =1.0000
- √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
#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:
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:
- √45 = 6.7082, √2 = 1.4142
- √67 = 8.1854,√3=1.7321
- √21 = 4.5826,√4=2.0000
- √23 = 4.7958,√5=2.2361
- √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:
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
#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:
tensor([12, 34, 56, 1, 10])
Square Root:
tensor([3.4641, 5.8310, 7.4833, 1.0000, 3.1623])
Working:
- √12 =3.4641
- √34 = 5.8310
- √56 =7.4833
- √1 =1.0000
- √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
#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:
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:
- √45 = 6.7082, √2 = 1.4142
- √67 = 8.1854,√3=1.7321
- √21 = 4.5826,√4=2.0000
- √23 = 4.7958,√5=2.2361
- √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.