Python

# PyTorch – float_power()

PyTorch is an open-source framework for the Python programming language.

A tensor is a multidimensional array that is used to store data. So 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 data is a multi-dimensional array.

## torch.float_power()

It raises the elements to the power of the exponent in a tensor and returns all the elements in a tensor with double precision. It takes two parameters.

Syntax:
torch.float_power(tensor_object,exponent)

Parameters:

1. It takes a tensor object as the first parameter.
2. An exponent raises the values in a tensor.

Example 1:
Let’s create a 1D tensor that has five elements and raises the elements to the power of four.

#Let's import torch module
import torch

#create 1D tensor matrix
data1 = torch.tensor([1,2,3,4,5])

#display
print("Actual Tensor: ")
print(data1)

print("Raise to power-4: ")
#power of 4
print(torch.float_power(data1,4))

Output:

Actual Tensor:
tensor([1, 2, 3, 4, 5])
Raise to power-4:
tensor([  1.,  16.,  81., 256., 625.], dtype=torch.float64)

Working:
1^4=1
2^4=16
3^4=81
4^4=256
5^4=625

Example 2:
Let’s create a 2D tensor that has five elements in each row and raises the elements to the power of two.

#Let's import torch module
import torch

#create 1D tensor matrix
data1 = torch.tensor([[1,2,3,4,5],[0,0,0,0,0]])

#display
print("Actual Tensor: ")
print(data1)

print("Raise to power-2: ")
#power of 2
print(torch.float_power(data1,2))

Output:

Actual Tensor:
tensor([[1, 2, 3, 4, 5],
[0, 0, 0, 0, 0]])
Raise to power-2:
tensor([[ 1.,  4.,  9., 16., 25.],
[ 0.,  0.,  0.,  0.,  0.]], dtype=torch.float64)

Working:
1^2=1, 0^2=0
2^2=4, 0^2=0
3^2=9, 0^2=0
4^2=16, 0^2=0
5^2=25, 0^2=0

## Work with CPU

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

At this time, when we are creating a tensor, we can use the cpu() function.

Syntax:
torch.tensor(data).cpu()

Example 1:
Let’s create a 1D tensor that has five elements on the CPU and raises the elements to the power of four.

#Let's import torch module
import torch

#create 1D tensor matrix
data1 = torch.tensor([1,2,3,4,5]).cpu()

#display
print("Actual Tensor: ")
print(data1)

print("Raise to power-4: ")
#power of 4
print(torch.float_power(data1,4))

Output:

Actual Tensor:
tensor([1, 2, 3, 4, 5])
Raise to power-4:
tensor([  1.,  16.,  81., 256., 625.], dtype=torch.float64)

Working:
1^4=1
2^4=16
3^4=81
4^4=256
5^4=625

Example 2:
Let’s create a 2D tensor that has five elements on the CPU in each row and raises the elements to the power of two.

#Let's import torch module
import torch

#create 1D tensor matrix
data1 = torch.tensor([[1,2,3,4,5],[0,0,0,0,0]]).cpu()

#display
print("Actual Tensor: ")
print(data1)

print("Raise to power-2: ")
#power of 2
print(torch.float_power(data1,2))

Output:

Actual Tensor:
tensor([[1, 2, 3, 4, 5],
[0, 0, 0, 0, 0]])
Raise to power-2:
tensor([[ 1.,  4.,  9., 16., 25.],
[ 0.,  0.,  0.,  0.,  0.]], dtype=torch.float64)

Working:
1^2=1, 0^2=0
2^2=4, 0^2=0
3^2=9, 0^2=0
4^2=16, 0^2=0
5^2=25, 0^2=0

## Conclusion

In this PyTorch lesson, we discussed the float_power() function. It raises the elements to the power of the exponent in a tensor and returns all the elements in a tensor with double precision. We saw two different examples and also worked these examples on a CPU machine.