Syntax error How to compute the inverse hyperbolic sine in PyTorch?

How to compute the inverse hyperbolic sine in PyTorch?



The torch.asinh() method computes the inverse hyperbolic sine of each element of the input tensor. It supports both real and complex-valued inputs. It supports any dimension of the input tensor.

Syntax

torch.asinh(input)

where input is the input tensor.

Output

It returns a tensor inverse hyperbolic sine of each element.

Steps

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

  • 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.

input = torch.randn(3,4)
print("Input Tensor:
", input)
  • Compute the inverse hyperbolic sine of each element in the input tensor using torch.asinh(input). Here input is the input tensor .

inv_hsi = torch.asinh(input)
  • Display the computed tensor with inverse hyperbolic sine values.

print("Inverse Hyperbolic Sine Tensor:
", inv_hsin)

Now, let's take a couple of examples to demonstrate how to compute the inverse hyperbolic sine.

Example 1

# Import the required library
import torch

# define an input tensor
input = torch.tensor([1.2, 3., 4., 4.2, -3.2])

# print the above defined tensor
print("Input Tensor:
", input) # compute the inverse hyperbolic sine inv_hsin = torch.asinh(input) # print the above computed tensor print("Inverse Hyperbolic Sine Tensor:
", inv_hsin) print("............................") # define a complex input tensor input = torch.tensor([1.2+2j, 3.+4.j, 4.2-3.2j]) # print the above defined tensor print("Input Tensor:
", input) # compute the inverse hyperbolic sine inv_hsin = torch.asinh(input) # print the above computed tensor print("Inverse Hyperbolic Sine Tensor:
", inv_hsin)

Output

Input Tensor:
   tensor([ 1.2000, 3.0000, 4.0000, 4.2000, -3.2000])
Inverse Hyperbolic Sine Tensor:
   tensor([ 1.0160, 1.8184, 2.0947, 2.1421, -1.8799])
............................
Input Tensor:
   tensor([1.2000+2.0000j, 3.0000+4.0000j, 4.2000-3.2000j])
Inverse Hyperbolic Sine Tensor:
   tensor([1.5205+0.9873j, 2.2999+0.9176j, 2.3596-0.6425j])

In the above program, we computed the inverse hyperbolic sine of each element of the both real and complex-valued input tensors.

Example 2

# Import the required library
import torch

# define an input tensor
input = torch.randn(4,4)

# print the above defined tensor
print("Input Tensor:
", input) # compute the inverse hyperbolic sine inv_hsin = torch.asinh(input) # print the above computed tensor print("Inverse Hyperbolic Sine Tensor:
", inv_hsin) print("............................") # define a complex input tensor real = torch.randn(2,3) imag = torch.randn(2,3) input = torch.complex(real, imag) # print the above defined tensor print("Input Tensor:
", input) # compute the inverse hyperbolic sine inv_hsin = torch.asinh(input) # print the above computed tensor print("Inverse Hyperbolic Sine Tensor:
", inv_hsin)

Output

Input Tensor:
   tensor([[ 0.4057, -1.8063, -0.5133, 0.3540],
      [-0.7180, -1.0896, 0.1832, 1.9867],
      [-0.6352, -0.1913, -0.0541, -0.3637],
      [-0.6229, 0.5518, -0.8876, 2.8466]])
Inverse Hyperbolic Sine Tensor:
   tensor([[ 0.3953, -1.3535, -0.4931, 0.3470],
      [-0.6673, -0.9433, 0.1822, 1.4377],
      [-0.5988, -0.1901, -0.0541, -0.3561],
      [-0.5884, 0.5270, -0.7996, 1.7688]])
............................
Input Tensor:
   tensor([[-0.7072+0.6690j, 0.2434-1.0732j, 1.2196-0.7483j],
      [-1.2849+0.1874j, -0.7717+1.3786j, 0.6163-0.0782j]])
Inverse Hyperbolic Sine Tensor:
   tensor([[-0.7525+0.5421j, 0.5764-1.1596j, 1.1148-0.4592j],
      [-1.0744+0.1149j, -1.1086+0.9624j, 0.5839-0.0666j]])

Note − In the above example, we have taken input tensors of randomly generated numbers. You may notice getting different elements.

Updated on: 2022-01-27T07:20:09+05:30

196 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements