PyTorch – 如何計算張量的誤差函式?
要計算張量的誤差函式,我們使用 **torch.special.erf()** 方法。它返回一個包含已計算誤差函式的新張量。它接受任何維度的torch張量。它也稱為 **高斯誤差函式**
步驟
我們可以使用以下步驟來逐元素計算張量的誤差函式:
匯入所需的庫。在以下所有示例中,所需的Python庫是torch。確保您已安裝它。
import torch
定義一個torch張量。這裡我們定義一個包含隨機數的二維張量。
tensor = torch.randn(2,3,3)
使用 **torch.special.erf(tensor)** 計算上面定義的張量的誤差函式。可以選擇將此值賦給一個新的變數。
err = torch.special.erf(tensor)
列印計算出的誤差函式。
print("Entropy:", err)示例1
在這個例子中,我們計算一維張量的誤差函式。
# import necessary libraries
import torch
# define a 1D tensor
tensor1 = torch.tensor([-1,2,3,4,5])
# print above created tensor
print("Tensor:", tensor1)
# compute the error function of the tensor
err = torch.special.erf(tensor1)
# Display the computed error function
print("Error :", err)輸出
Tensor: tensor([-1.0000, 1.0000, 3.0000, 0.0000, 0.5000]) Error : tensor([-0.8427, 0.8427, 1.0000, 0.0000, 0.5205])
示例2
在這個例子中,我們計算二維張量的誤差函式
# import necessary libraries
import torch
# define a tensor of random numbers
tensor1 = torch.randn(2,3,3)
# print above created tensor
print("Tensor:
", tensor1)
# compute the error function of the tensor
err = torch.special.erf(tensor1)
# Display the computed error function
print("Error:
", err)輸出
Tensor: tensor([[[-1.0724, 0.3955, -0.3472], [-0.7336, -0.8110, 1.2624], [ 0.2334, -0.9200, -0.9879]], [[ 0.8636, 0.3452, -0.4742], [-0.6868, 0.8436, -0.4195], [ 1.0410, -0.4681, 1.6284]]]) Error: tensor([[[-0.8706, 0.4241, -0.3766], [-0.7005, -0.7486, 0.9258], [ 0.2586, -0.8068, -0.8376]], [[ 0.7780, 0.3746, -0.4975], [-0.6686, 0.7671, -0.4470], [ 0.8590, -0.4921, 0.9787]]])
廣告
資料結構
網路
關係型資料庫管理系統 (RDBMS)
作業系統
Java
iOS
HTML
CSS
Android
Python
C語言程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP