| by Arround The Web | No comments

Perform Inverse Trigonometric Functions in PyTorch

PyTorch is an open-source framework for the Python programming language. We can process the data in PyTorch in the form of a Tensor.

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

torch.asin()

torch.asin() in PyTorch returns inverse sine values of all the elements in a tensor. It takes only one parameter.

Syntax:
torch.asin(tensor_object)

Parameter:
tensor_object is the input tensor

Example 1:

Let’s create a one-dimensional tensor – data1 and return inverse sine values by applying torch.asin() on it.

#import torch module
import torch
 
#create a 1D tensor - data1 with 5 numeric values.
data1 = torch.tensor([23,45,67,10,0])
 
#display
print("Tensor: ",data1)
 
#perform asin() on above tensor
print("Inverse sine values: ",torch.asin(data1))

Output:

Tensor:  tensor([23, 45, 67, 10,  0])
Inverse sine values:  tensor([nan, nan, nan, nan, 0.])

We can see that inverse sine values were returned.

Example 2:

Let’s create a two-dimensional tensor – data1 and return inverse sine values by applying torch.asin() on it.

#import torch module
import torch
 
#create a 2D tensor - data1 with 5 numeric values in each row.
data1 = torch.tensor([[23,45,67,10,0],[65,78,90,120,180]])
 
#display
print("Tensor: ",data1)
 
#perform asin() on above tensor
print("Inverse sine values: ",torch.asin(data1))

Output:

Tensor:  tensor([[ 23,  45,  67,  10,   0],
        [ 65,  78,  90, 120, 180]])
Inverse sine values:  tensor([[nan, nan, nan, nan, 0.],
        [nan, nan, nan, nan, nan]])

We can see that inverse sine values were returned.

torch.acos()

torch.acos() in PyTorch returns inverse cosine values of all the elements in a tensor. It takes only one parameter.

Syntax:
torch.acos(tensor_object)

Parameter:
tensor_object is the input tensor

Example 1:

Let’s create a one-dimensional tensor – data1 and return inverse cosine values by applying torch.acos() on it.

#import torch module
import torch
 
#create a 1D tensor - data1 with 5 numeric values.
data1 = torch.tensor([23,45,67,10,0])
 
#display
print("Tensor: ",data1)
 
#perform acos() on above tensor
print("Inverse cosine values: ",torch.acos(data1))

Output:

Tensor:  tensor([23, 45, 67, 10,  0])
Inverse cosine values:  tensor([   nan,    nan,    nan,    nan, 1.5708])

We can see that inverse cosine values were returned.

Example 2:

Let’s create a two-dimensional tensor – data1 and return inverse cosine values by applying torch.acos() on it.

#import torch module
import torch
 
#create a 2D tensor - data1 with 5 numeric values in each row.
data1 = torch.tensor([[23,45,67,10,0],[65,78,90,120,180]])
 
#display
print("Tensor: ",data1)
 
#perform acos() on above tensor
print("Inverse cosine values: ",torch.acos(data1))

Output:

Tensor:  tensor([[ 23,  45,  67,  10,   0],
        [ 65,  78,  90, 120, 180]])
Inverse cosine values:  tensor([[   nan,    nan,    nan,    nan, 1.5708],
        [   nan,    nan,    nan,    nan,    nan]])

We can see that inverse cosine values were returned.

torch.atan()

torch.atan() in PyTorch returns inverse tangent values of all the elements in a tensor. It takes only one parameter.

Syntax:
torch.atan(tensor_object)

Parameter:
tensor_object is the input tensor

Example 1:

Let’s create a one-dimensional tensor – data1 and return inverse tangent values by applying torch.atan() on it.

#import torch module
import torch
 
#create a 1D tensor - data1 with 5 numeric values.
data1 = torch.tensor([23,45,67,10,0])
 
#display
print("Tensor: ",data1)
 
#perform atan() on above tensor
print("Inverse tangent values: ",torch.atan(data1))

Output:

Tensor:  tensor([23, 45, 67, 10,  0])
Inverse tangent values:  tensor([1.5273, 1.5486, 1.5559, 1.4711, 0.0000])

We can see that inverse tangent values were returned.

Example 2:

Let’s create a two-dimensional tensor – data1 and return inverse tangent values by applying torch.atan() on it.

#import torch module
import torch
 
#create a 2D tensor - data1 with 5 numeric values in each row.
data1 = torch.tensor([[23,45,67,10,0],[65,78,90,120,180]])
 
#display
print("Tensor: ",data1)
 
#perform atan() on above tensor
print("Inverse Tangent  values: ",torch.atan(data1))

Output:

Tensor:  tensor([[ 23,  45,  67,  10,   0],
        [ 65,  78,  90, 120, 180]])
Inverse Tangent  values:  tensor([[1.5273, 1.5486, 1.5559, 1.4711, 0.0000],
        [1.5554, 1.5580, 1.5597, 1.5625, 1.5652]])

We can see that inverse tangent values were returned.

torch.asinh()

torch.asinh() in PyTorch returns inverse hyperbolic sine values of all the elements in a tensor. It takes only one parameter.

Syntax:
torch.asinh(tensor_object)

Parameter:
tensor_object is the input tensor

Example 1:

Let’s create a one-dimensional tensor – data1 and return inverse hyperbolic sine values by applying torch.asinh() on it.

#import torch module
import torch
 
#create a 1D tensor - data1 with 5 numeric values.
data1 = torch.tensor([0,1,45,10,23])
 
#display
print("Tensor: ",data1)
 
#perform asinh() on above tensor
print("Inverse hyperbolic sine values: ",torch.asinh(data1))

Output:

Tensor:  tensor([ 0,  1, 45, 10, 23])
Inverse hyperbolic sine values:  tensor([0.0000, 0.8814, 4.4999, 2.9982, 3.8291])

We can see that inverse hyperbolic sine values were returned.

Example 2:

Let’s create a two-dimensional tensor – data1 and return inverse hyperbolic sine values by applying torch.asinh() on it.

#import torch module
import torch
 
#create a 2D tensor - data1 with 5 numeric values in each row.
data1 = torch.tensor([[23,45,67,10,0],[65,78,90,120,180]])
 
#display
print("Tensor: ",data1)
 
#perform asinh() on above tensor
print("Inverse hyperbolic sine values: ",torch.asinh(data1))

Output:

Tensor:  tensor([[ 23,  45,  67,  10,   0],
        [ 65,  78,  90, 120, 180]])
Inverse hyperbolic sine values:  tensor([[3.8291, 4.4999, 4.8979, 2.9982, 0.0000],
        [4.8676, 5.0499, 5.1930, 5.4807, 5.8861]])

We can see that inverse hyperbolic sine values were returned.

torch.acosh()

torch.acosh() in PyTorch returns inverse hyperbolic cosine values of all the elements in a tensor. It takes only one parameter.

Syntax:
torch.acosh(tensor_object)

Parameter:
tensor_object is the input tensor

Example 1:

Let’s create a one-dimensional tensor – data1 and return inverse hyperbolic cosine values by applying torch.acosh() on it.

#import torch module
import torch
 
#create a 1D tensor - data1 with 5 numeric values.
data1 = torch.tensor([23,45,67,10,0])
 
#display
print("Tensor: ",data1)
 
#perform acosh() on above tensor
print("Inverse hyperbolic cosine values: ",torch.acosh(data1))

Output:

Tensor:  tensor([23, 45, 67, 10,  0])
Inverse hyperbolic cosine values:  tensor([3.8282, 4.4997, 4.8978, 2.9932,    nan])

We can see that inverse hyperbolic cosine values were returned.

Example 2:

Let’s create a two-dimensional tensor – data1 and return inverse hyperbolic cosine values by applying torch.acosh() on it.

#import torch module
import torch
 
#create a 2D tensor - data1 with 5 numeric values in each row.
data1 = torch.tensor([[23,45,67,10,0],[65,78,90,120,180]])
 
#display
print("Tensor: ",data1)
 
#perform acosh() on above tensor
print("Inverse hyperbolic cosine values: ",torch.acosh(data1))

Output:

Tensor:  tensor([[ 23,  45,  67,  10,   0],
        [ 65,  78,  90, 120, 180]])
Inverse hyperbolic cosine values:  tensor([[3.8282, 4.4997, 4.8978, 2.9932,    nan],
        [4.8675, 5.0498, 5.1929, 5.4806, 5.8861]])

We can see that inverse hyperbolic cosine values were returned.

torch.atanh()

torch.atanh() in PyTorch returns inverse hyperbolic tangent values of all the elements in a tensor. It takes only one parameter.

Syntax:
torch.atanh(tensor_object)

Parameter:
tensor_object is the input tensor

Example 1:

Let’s create a one-dimensional tensor – data1 and return inverse hyperbolic tangent values by applying torch.atanh() on it.

#import torch module
import torch
 
#create a 1D tensor - data1 with 5 numeric values.
data1 = torch.tensor([23,45,67,10,0])
 
#display
print("Tensor: ",data1)
 
#perform atanh() on above tensor
print("Inverse hyperbolic tangent values: ",torch.atanh(data1))

Output:

Tensor:  tensor([23, 45, 67, 10,  0])
Inverse hyperbolic tangent values:  tensor([nan, nan, nan, nan, 0.])

We can see that inverse hyperbolic tangent values were returned.

Example 2:

Let’s create a two-dimensional tensor – data1 and return inverse hyperbolic tangent values by applying torch.atanh() on it.

#import torch module
import torch
 
#create a 2D tensor - data1 with 5 numeric values in each row.
data1 = torch.tensor([[23,45,67,10,0],[65,78,90,120,180]])
 
#display
print("Tensor: ",data1)
 
#perform atanh() on above tensor
print("Inverse hyperbolic tangent values: ",torch.atanh(data1))

Output:

Tensor:  tensor([[ 23,  45,  67,  10,   0],
        [ 65,  78,  90, 120, 180]])
Inverse hyperbolic tangent values:  tensor([[nan, nan, nan, nan, 0.],
        [nan, nan, nan, nan, nan]])

We can see that inverse hyperbolic tangent values were returned.

Conclusion

In this PyTorch lesson, we saw how to perform Inverse Trigonometric functions in PyTorch. We discussed three types of inverse trigonometric functions – asin(),acos() and atan(). If you need to perform inverse hyperbolic functions, you can use asinh(),acosh() and atanh().

Share Button

Source: linuxhint.com

Leave a Reply