How to compute the inverse hyperbolic sine in PyTorch?

PyTorchServer Side ProgrammingProgramming

<p>The <strong>torch.asinh()</strong> 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.</p><h3>Syntax</h3><pre class="just-code notranslate language-python" data-lang="python">torch.asinh(input)</pre><p>where <strong>input</strong> is the input tensor.</p><h3>Output</h3><p>It returns a tensor inverse hyperbolic sine of each element.</p><h3>Steps</h3><p>To compute the inverse hyperbolic sine of each element in the input tensor, you could follow the steps given below &minus;</p><ul class="list"><li><p>Import the required library. In all the following examples, the required Python library is <strong>torch</strong>. Make sure you have already installed it.</p></li></ul><pre class="just-code notranslate language-python" data-lang="python">import torch</pre><ul class="list"><li><p>Create a torch tensor and print it.</p></li></ul><pre class="just-code notranslate language-python" data-lang="python">input = torch.randn(3,4) print(&quot;Input Tensor: &quot;, input)</pre><ul class="list"><li><p>Compute the inverse hyperbolic sine of each element in the input tensor using <strong>torch.asinh(input)</strong>. Here input is the input tensor .</p></li></ul><pre class="just-code notranslate language-python" data-lang="python">inv_hsi = torch.asinh(input)</pre><ul class="list"><li><p>Display the computed tensor with inverse hyperbolic sine values.</p></li></ul><pre class="just-code notranslate language-python" data-lang="python">print(&quot;Inverse Hyperbolic Sine Tensor: &quot;, inv_hsin)</pre><p>Now, let&#39;s take a couple of examples to demonstrate how to compute the inverse hyperbolic sine.</p><h2>Example 1</h2><pre class="just-code notranslate language-python" data-lang="python"># 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(&quot;Input Tensor: &quot;, input) # compute the inverse hyperbolic sine inv_hsin = torch.asinh(input) # print the above computed tensor print(&quot;Inverse Hyperbolic Sine Tensor: &quot;, inv_hsin) print(&quot;............................&quot;) # define a complex input tensor input = torch.tensor([1.2+2j, 3.+4.j, 4.2-3.2j]) # print the above defined tensor print(&quot;Input Tensor: &quot;, input) # compute the inverse hyperbolic sine inv_hsin = torch.asinh(input) # print the above computed tensor print(&quot;Inverse Hyperbolic Sine Tensor: &quot;, inv_hsin)</pre><h2>Output</h2><pre class="result notranslate" style="">Input Tensor: &nbsp; &nbsp;tensor([ 1.2000, 3.0000, 4.0000, 4.2000, -3.2000]) Inverse Hyperbolic Sine Tensor: &nbsp; &nbsp;tensor([ 1.0160, 1.8184, 2.0947, 2.1421, -1.8799]) ............................ Input Tensor: &nbsp; &nbsp;tensor([1.2000+2.0000j, 3.0000+4.0000j, 4.2000-3.2000j]) Inverse Hyperbolic Sine Tensor: &nbsp; &nbsp;tensor([1.5205+0.9873j, 2.2999+0.9176j, 2.3596-0.6425j])</pre><p>In the above program, we computed the inverse hyperbolic sine of each element of the both real and complex-valued input tensors.</p><h2>Example 2</h2><pre class="just-code notranslate language-python" data-lang="python"># Import the required library import torch # define an input tensor input = torch.randn(4,4) # print the above defined tensor print(&quot;Input Tensor: &quot;, input) # compute the inverse hyperbolic sine inv_hsin = torch.asinh(input) # print the above computed tensor print(&quot;Inverse Hyperbolic Sine Tensor: &quot;, inv_hsin) print(&quot;............................&quot;) # 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(&quot;Input Tensor: &quot;, input) # compute the inverse hyperbolic sine inv_hsin = torch.asinh(input) # print the above computed tensor print(&quot;Inverse Hyperbolic Sine Tensor: &quot;, inv_hsin)</pre><h2>Output</h2><pre class="result notranslate">Input Tensor: &nbsp; &nbsp;tensor([[ 0.4057, -1.8063, -0.5133, 0.3540], &nbsp; &nbsp; &nbsp; [-0.7180, -1.0896, 0.1832, 1.9867], &nbsp; &nbsp; &nbsp; [-0.6352, -0.1913, -0.0541, -0.3637], &nbsp; &nbsp; &nbsp; [-0.6229, 0.5518, -0.8876, 2.8466]]) Inverse Hyperbolic Sine Tensor: &nbsp; &nbsp;tensor([[ 0.3953, -1.3535, -0.4931, 0.3470], &nbsp; &nbsp; &nbsp; [-0.6673, -0.9433, 0.1822, 1.4377], &nbsp; &nbsp; &nbsp; [-0.5988, -0.1901, -0.0541, -0.3561], &nbsp; &nbsp; &nbsp; [-0.5884, 0.5270, -0.7996, 1.7688]]) ............................ Input Tensor: &nbsp; &nbsp;tensor([[-0.7072+0.6690j, 0.2434-1.0732j, 1.2196-0.7483j], &nbsp; &nbsp; &nbsp; [-1.2849+0.1874j, -0.7717+1.3786j, 0.6163-0.0782j]]) Inverse Hyperbolic Sine Tensor: &nbsp; &nbsp;tensor([[-0.7525+0.5421j, 0.5764-1.1596j, 1.1148-0.4592j], &nbsp; &nbsp; &nbsp; [-1.0744+0.1149j, -1.1086+0.9624j, 0.5839-0.0666j]])</pre><p><strong>Note</strong> &minus; In the above example, we have taken input tensors of randomly generated numbers. You may notice getting different elements.</p>
raja
Updated on 27-Jan-2022 07:20:09

Advertisements