torcheval.metrics.functional.mean_squared_error¶
- torcheval.metrics.functional.mean_squared_error(input: Tensor, target: Tensor, *, sample_weight: Tensor | None = None, multioutput: str = 'uniform_average') Tensor¶
计算均方误差,即input和target的误差平方的平均值 其类版本为
torcheval.metrics.MeanSquaredError。- Parameters:
input (Tensor) – 预测值的张量,形状为 (n_sample, n_output)。
target (Tensor) – 形状为 (n_sample, n_output) 的真实值张量。
sample_weight (可选) – 样本权重的张量,形状为 (n_sample, )。默认为 None。
multioutput (可选) –
'uniform_average'[默认]:返回所有输出的分数,并使用统一权重进行平均。
'raw_values':返回完整的分数集。
- Raises:
ValueError –
如果multioutput的值不存在于(
raw_values,uniform_average)中。 - 如果input或target的维度不是1D或2D。 - 如果input和target的大小不相同。 - 如果input、target和sample_weight的第一个维度不相同。
示例:
>>> import torch >>> from torcheval.metrics.function import mean_squared_error >>> input = torch.tensor([0.9, 0.5, 0.3, 0.5]) >>> target = torch.tensor([0.5, 0.8, 0.2, 0.8]) >>> mean_squared_error(input, target) tensor(0.0875) >>> input = torch.tensor([[0.9, 0.5], [0.3, 0.5]]) >>> target = torch.tensor([[0.5, 0.8], [0.2, 0.8]]) >>> mean_squared_error(input, target) tensor(0.0875) >>> input = torch.tensor([[0.9, 0.5], [0.3, 0.5]]) >>> target = torch.tensor([[0.5, 0.8], [0.2, 0.8]]) >>> mean_squared_error(input, target, multioutput="raw_values") tensor([0.0850, 0.0900]) >>> input = torch.tensor([[0.9, 0.5], [0.3, 0.5]]) >>> target = torch.tensor([[0.5, 0.8], [0.2, 0.8]]) >>> mean_squared_error(input, target, sample_weight=torch.tensor([0.2, 0.8])) tensor(0.0650)