如何在PyTorch中计算反余弦和反双曲余弦?
torch.acos() 方法计算输入张量每个元素的反余弦。它支持实数和复数值输入,并支持任何维度的输入张量。输入张量的元素必须在 [-1,1] 范围内,因为反余弦函数的定义域为 [-1,1]。
torch.acosh() 方法计算输入张量每个元素的反双曲余弦。它也支持任何维度的实数和复数值输入。输入张量的元素必须大于等于 1,因为反双曲余弦函数的定义域为 [1, +∞]。
语法
torch.acos(input) torch.acosh(input)
步骤
要计算输入张量中每个元素的反余弦和反双曲余弦,您可以按照以下步骤操作:
导入所需的库。在以下所有示例中,所需的 Python 库是 torch。请确保您已安装它。
import torch
创建一个 torch 张量并打印它。为了计算反余弦,我们通过应用 uniform_(-1,1) 生成随机数来确保输入张量元素在 [-1,1] 范围内。
a = torch.randn(4).uniform_(-1, 1) print("Input Tensor:
", input)
使用 torch.acos(input) 或 torch.acosh(input) 计算输入张量中每个元素的反余弦或反双曲余弦。这里 input 是输入张量。
inv_cos = torch.acos(input) inv_cosh = torch.acosh(input)
显示计算出的包含反余弦或反双曲余弦值的张量。
print("Inverse Cosine Tensor:
", inv_cos) print("Inverse Hyperbolic Cosine Tensor:
", inv_cosh)
示例 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)
输出
Tensor: tensor([ 0.2127, 0.8572, -0.3944, -0.9310]) Inverse Cosine: tensor([1.3565, 0.5409, 1.9762, 2.7679])
示例 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:
",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)
输出
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]])
示例 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)
输出
Tensor: tensor([4.7254, 8.4879, 7.2126, 3.6867]) Inverse Hyperbolic Cosine: tensor([2.2347, 2.8283, 2.6641, 1.9790])
示例 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:
",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)
输出
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]])
广告