如何在 PyTorch 中计算均方误差(平方 L2 范数)?
均方误差计算的是输入和目标(预测值和实际值)之间的平方差的平均值。要在 PyTorch 中计算均方误差,我们可以应用 **torch.nn** 模块提供的 **MSELoss()** 函数。它创建一个用于度量均方误差的标准。它也称为 **平方 L2 范数**。
实际值和预测值都是具有相同元素数量的张量。这两个张量可以具有任意数量的维度。此函数返回一个标量值的张量。它是 **torch.nn** 模块提供的损失函数的一种。损失函数用于通过最小化损失来优化深度神经网络。
语法
torch.nn.MSELoss()
步骤
要测量均方误差,可以按照以下步骤操作
导入所需的库。在以下所有示例中,所需的 Python 库为 **torch**。确保您已安装它。
import torch
创建输入和目标张量并打印它们。
input = torch.tensor([0.10, 0.20, 0.40, 0.50]) target = torch.tensor([0.09, 0.2, 0.38, 0.52])
创建一个标准来测量均方误差
mse = nn.MSELoss()
计算均方误差(损失)并打印它。
output = mse(input, target)
print("MSE loss:", output)示例 1
在此程序中,我们测量输入和目标张量之间的均方误差。输入和目标张量都是 1D 张量。
# Import the required libraries
import torch
import torch.nn as nn
# define the input and target tensors
input = torch.tensor([0.10, 0.20, 0.40, 0.50])
target = torch.tensor([0.09, 0.2, 0.38, 0.52])
# print input and target tensors
print("Input Tensor:
", input)
print("Target Tensor:
", target)
# create a criterion to measure the mean squared error
mse = nn.MSELoss()
# compute the loss (mean squared error)
output = mse(input, target)
# output.backward()
print("MSE loss:", output)输出
Input Tensor: tensor([0.1000, 0.2000, 0.4000, 0.5000]) Target Tensor: tensor([0.0900, 0.2000, 0.3800, 0.5200]) MSE loss: tensor(0.0002)
请注意,均方误差是一个标量值。
示例 2
在此程序中,我们测量输入和目标张量之间的均方误差。输入和目标张量都是 2D 张量。
# Import the required libraries
import torch
import torch.nn as nn
# define the input and target tensors
input = torch.randn(3, 4)
target = torch.randn(3, 4)
# print input and target tensors
print("Input Tensor:
", input)
print("Target Tensor:
", target)
# create a criterion to measure the mean squared error
mse = nn.MSELoss()
# compute the loss (mean squared error)
output = mse(input, target)
# output.backward()
print("MSE loss:", output)输出
Input Tensor: tensor([[-1.6413, 0.8950, -1.0392, 0.2382], [-0.3868, 0.2483, 0.9811, -0.9260], [-0.0263, -0.0911, -0.6234, 0.6360]]) Target Tensor: tensor([[-1.6068, 0.7233, -0.0925, -0.3140], [-0.4978, 1.3121, -1.4910, -1.4643], [-2.2589, 0.3073, 0.2038, -1.5656]]) MSE loss: tensor(1.6209)
示例 3
在此程序中,我们测量输入和目标张量之间的均方误差。输入和目标张量都是 2D 张量。输入张量采用参数 **requires_grad=true**。因此,我们还计算此函数相对于输入张量的梯度。
# Import the required libraries
import torch
import torch.nn as nn
# define the input and target tensors
input = torch.randn(4, 5, requires_grad = True)
target = torch.randn(4, 5)
# print input and target tensors
print("Input Tensor:
", input)
print("Target Tensor:
", target)
# create a criterion to measure the mean squared error
loss = nn.MSELoss()
# compute the loss (mean squared error)
output = loss(input, target)
output.backward()
print("MSE loss:", output)
print("input.grad:
", input.grad)输出
Input Tensor: tensor([[ 0.1813, 0.4199, 1.1768, -0.7068, 0.2960], [ 0.7950, 0.0945, -0.0954, -1.0170, -0.1471], [ 1.2264, 1.7573, 0.9099, 1.3720, -0.9087], [-1.0122, -0.8649, -0.7797, -0.7787, 0.9944]], requires_grad=True) Target Tensor: tensor([[-0.6370, -0.8421, 1.2474, 0.4363, -0.1481], [-0.1500, -1.3141, 0.7349, 0.1184, -2.7065], [-1.0776, 1.3530, 0.6939, -1.3191, 0.7406], [ 0.2058, 0.4765, 0.0695, 1.2146, 1.1519]]) MSE loss: tensor(1.9330, grad_fn=<MseLossBackward>) input.grad: tensor([[ 0.0818, 0.1262, -0.0071, -0.1143, 0.0444], [ 0.0945, 0.1409, -0.0830, -0.1135, 0.2559], [ 0.2304, 0.0404, 0.0216, 0.2691, -0.1649], [-0.1218, -0.1341, -0.0849, -0.1993, -0.0158]])
广告
数据结构
网络
关系型数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C 语言编程
C++
C#
MongoDB
MySQL
Javascript
PHP