How to compute the inverse cosine and inverse hyperbolic cosine in PyTorch?

PyTorchServer Side ProgrammingProgramming

The torch.acos() method computes the inverse cosine of each element of an input tensor. It supports both real and complex-valued inputs. It supports any dimension of the input tensor. The elements of the input tensor must be in the range [-1,1], as the inverse cosine function has its domain as [-1,1].

The torch.acosh() method computes the inverse hyperbolic cosine of each element of the input tensor. It also supports both real and complex-valued inputs of any dimension. The elements of the input tensor must be any number greater or equal to 1, as the inverse cosine function has its domain as [1, +∞].

Syntax

torch.acos(input)
torch.acosh(input)

Steps

To compute the inverse cosine and inverse hyperbolic cosine of each element in the input tensor, you could follow the below steps −

  • Import the required library. In all the following examples, the required Python library is torch. Make sure you have already installed it.

import torch
  • Create a torch tensor and print it. To compute the inverse cosine we ensure that input tensor elements are in range [-1,1] by applying uniform_(-1,1) to generate random numbers.

a = torch.randn(4).uniform_(-1, 1)
print("Input Tensor:\n", input)
  • Compute the inverse cosine or inverse hyperbolic cosine of each element in the input tensor using torch.acos(input) or torch.acosh(input). Here input is the input tensor.

inv_cos = torch.acos(input)
inv_cosh = torch.acosh(input)
  • Display the computed tensors with inverse cosine or inverse hyperbolic cosine values.

print("Inverse Cosine Tensor:\n", inv_cos)
print("Inverse Hyperbolic Cosine Tensor:\n", inv_cosh)

Example 1

import torch

# define a tensor with values in range [-1, 1]
a = torch.randn(4).uniform_(-1, 1)

# print the above defined tensors
print("Tensor:",a)

# compute inverse cosine of elements of the above tensor
inv_cos = torch.acos(a)

# print the tensor with inverse cosine values
print("Inverse Cosine:", inv_cos)

Output

Tensor: tensor([ 0.2127, 0.8572, -0.3944, -0.9310])
Inverse Cosine: tensor([1.3565, 0.5409, 1.9762, 2.7679])

Example 2

import torch

# define a tensor with values in range [-1, 1]
a = torch.randn(5,5).uniform_(-1, 1)

# print the above defined tensors
print("Tensor:\n",a)

# compute inverse cosine of elements of the above tensor
inv_cos = torch.acos(a)

# print the tensor with inverse cosine values
print("Inverse Cosine:\n", inv_cos)

Output

Tensor:
   tensor([[ 0.6149, -0.6334, 0.8994, 0.6377, -0.2348],
      [-0.1458, -0.2893, 0.7044, -0.9379, 0.3848],
      [-0.9450, 0.0991, -0.8826, 0.8640, 0.7513],
      [ 0.0019, -0.2069, 0.6228, 0.8062, -0.9137],
      [-0.8181, 0.4544, -0.8216, -0.7370, 0.9821]])
Inverse Cosine:
   tensor([[0.9085, 2.2568, 0.4525, 0.8792, 1.8078],
      [1.7171, 1.8643, 0.7892, 2.7874, 1.1758],
      [2.8085, 1.4715, 2.6521, 0.5277, 0.7207],
      [1.5689, 1.7792, 0.8984, 0.6331, 2.7230],
      [2.5288, 1.0991, 2.5350, 2.3995, 0.1895]])

Example 3

import torch

# define a tensor with values in range [1, 9]
# the upper limit may be any number
a = torch.randn(4).uniform_(1, 9)
print("Tensor:",a)

# compute inverse hyperbolic cosine of above tensor
inv_cosh = torch.acosh(a)

# print the tensor with inverse hyperbolic cosine values
print("Inverse Hyperbolic Cosine:", inv_cosh)

Output

Tensor: tensor([4.7254, 8.4879, 7.2126, 3.6867])
Inverse Hyperbolic Cosine: tensor([2.2347, 2.8283, 2.6641, 1.9790])

Example 4

import torch

# define a tensor with values in range [1, 9]
# the upper limit may be any number
a = torch.randn(4,4).uniform_(1, 9)
print("Tensor:\n",a)

# compute inverse hyperbolic cosine of above tensor
inv_cosh = torch.acosh(a)

# print the tensor with inverse hyperbolic cosine values
print("Inverse Hyperbolic Cosine:\n", inv_cosh)

Output

Tensor:
   tensor([[3.2647, 4.1873, 3.1671, 2.4577],
      [4.4552, 8.1373, 1.2274, 5.5870],
      [2.6106, 1.5915, 2.5918, 7.5412],
      [7.6130, 8.9626, 4.6831, 1.1207]])
Inverse Hyperbolic Cosine:
   tensor([[1.8520, 2.1106, 1.8200, 1.5481],
      [2.1744, 2.7858, 0.6623, 2.4055],
      [1.6138, 1.0402, 1.6060, 2.7091],
      [2.7187, 2.8831, 2.2255, 0.4865]])
raja
Updated on 27-Jan-2022 06:53:56

Advertisements