如何在 PyTorch 中計算反雙曲正弦?


torch.asinh() 方法計算輸入張量中每個元素的反雙曲正弦。它支援實數和複數輸入。它支援輸入張量的任何維度。

語法

torch.asinh(input)

其中 input 是輸入張量。

輸出

它返回一個張量,包含每個元素的反雙曲正弦。

步驟

要計算輸入張量中每個元素的反雙曲正弦,您可以按照以下步驟操作:

  • 匯入所需的庫。在以下所有示例中,所需的 Python 庫是 torch。確保您已安裝它。

import torch
  • 建立一個 torch 張量並列印它。

input = torch.randn(3,4)
print("Input Tensor:
", input)
  • 使用 torch.asinh(input) 計算輸入張量中每個元素的反雙曲正弦。這裡 input 是輸入張量。

inv_hsi = torch.asinh(input)
  • 顯示計算出的包含反雙曲正弦值的張量。

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

現在,讓我們通過幾個示例來演示如何計算反雙曲正弦。

示例 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)

輸出

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])

在上面的程式中,我們計算了實數和複數輸入張量的每個元素的反雙曲正弦。

示例 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)

輸出

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]])

注意 - 在上面的示例中,我們使用了隨機生成的數字作為輸入張量。您可能會注意到得到不同的元素。

更新於: 2022年1月27日

146 次檢視

開啟您的 職業生涯

透過完成課程獲得認證

開始學習
廣告