如何在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]])
廣告
資料結構
網路
關係型資料庫管理系統 (RDBMS)
作業系統
Java
iOS
HTML
CSS
Android
Python
C語言程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP