如何在 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]])
注意 - 在上面的示例中,我们使用了随机生成的数字作为输入张量。您可能会注意到得到不同的元素。
广告