Regression Metrics

Regression Metrics

参考地址:Module metrics — PyTorch-Metrics 0.4.0 documentation (torchmetrics.readthedocs.io)

CosineSimilarity

计算target和preds之间的CosineSimilarity公式:

cos_{sim}(x,y) = \frac{x \cdot y}{||x|| \cdot ||y||} = \frac{\sum_{i=1}^n x_i y_i}{\sqrt{\sum_{i=1}^n x_i2}\sqrt{\sum_{i=1}n y_i^2}}

where y is a tensor of target values, and x is a tensor of predictions.

Forward accepts

  • preds (float tensor): (N,d)
  • target (float tensor): (N,d)
>>> from torchmetrics import CosineSimilarity
>>> target = torch.tensor([[0, 1], [1, 1]])
>>> preds = torch.tensor([[0, 1], [0, 1]])
>>> cosine_similarity = CosineSimilarity(reduction = 'mean')
>>> cosine_similarity(preds, target)
tensor(0.8536)

ExplainedVariance

ExplainedVariance计算公式:

\text{ExplainedVariance} = 1 - \frac{\text{Var}(y - \hat{y})}{\text{Var}(y)}

Where y is a tensor of target values, and \hat{y} is a tensor of predictions.

Forward accepts

  • preds (float tensor): (N,) or (N, ...) (multioutput)
  • target (long tensor): (N,) or (N, ...) (multioutput)

在多输出的情况下,默认情况下,方差将在附加维度上均匀平均。 请参阅参数 multioutput 以更改此行为。

Example

>>>

>>> from torchmetrics import ExplainedVariance
>>> target = torch.tensor([3, -0.5, 2, 7])
>>> preds = torch.tensor([2.5, 0.0, 2, 8])
>>> explained_variance = ExplainedVariance()
>>> explained_variance(preds, target)
tensor(0.9572)
>>> target = torch.tensor([[0.5, 1], [-1, 1], [7, -6]])
>>> preds = torch.tensor([[0, 2], [-1, 2], [8, -5]])
>>> explained_variance = ExplainedVariance(multioutput='raw_values')
>>> explained_variance(preds, target)
tensor([0.9677, 1.0000])

MeanAbsoluteError

MeanAbsoluteError(MAE)计算公式:

\text{MAE} = \frac{1}{N}\sum_i^N | y_i - \hat{y_i} |

Where y is a tensor of target values, and \hat{y} is a tensor of predictions.

>>> from torchmetrics import MeanAbsoluteError
>>> target = torch.tensor([3.0, -0.5, 2.0, 7.0])
>>> preds = torch.tensor([2.5, 0.0, 2.0, 8.0])
>>> mean_absolute_error = MeanAbsoluteError()
>>> mean_absolute_error(preds, target)
tensor(0.5000)

MeanAbsolutePercentageError

MeanAbsolutePercentageError(MAPE)计算公式:

\text{MAPE} = \frac{1}{n}\sum_1^n\frac{|   y_i - \hat{y_i} |}{\max(\epsilon, y_i)}

>>> from torchmetrics import MeanAbsolutePercentageError
>>> target = torch.tensor([1, 10, 1e6])
>>> preds = torch.tensor([0.9, 15, 1.2e6])
>>> mean_abs_percentage_error = MeanAbsolutePercentageError()
>>> mean_abs_percentage_error(preds, target)
tensor(0.2667)

MeanSquaredError

MeanSquaredError(MSE)计算公式:

\text{MSE} = \frac{1}{N}\sum_i^N(y_i - \hat{y_i})^2

Where y is a tensor of target values, and \hat{y} is a tensor of predictions.

>>> from torchmetrics import MeanSquaredError
>>> target = torch.tensor([2.5, 5.0, 4.0, 8.0])
>>> preds = torch.tensor([3.0, 5.0, 2.5, 7.0])
>>> mean_squared_error = MeanSquaredError()
>>> mean_squared_error(preds, target)
tensor(0.8750)

MeanSquaredLogError

MeanSquaredLogError(MSLE):

\text{MSLE} = \frac{1}{N}\sum_i^N (\log_e(1 + y_i) - \log_e(1 + \hat{y_i}))^2

Where y is a tensor of target values, and \hat{y} is a tensor of predictions.

>>> from torchmetrics import MeanSquaredLogError
>>> target = torch.tensor([2.5, 5, 4, 8])
>>> preds = torch.tensor([3, 5, 2.5, 7])
>>> mean_squared_log_error = MeanSquaredLogError()
>>> mean_squared_log_error(preds, target)
tensor(0.0397)

PearsonCorrcoef

PearsonCorrcoef计算公式:

P_{corr}(x,y) = \frac{cov(x,y)}{\sigma_x \sigma_y}

Where y is a tensor of target values, and x is a tensor of predictions.

Forward accepts

  • preds (float tensor): (N,)
  • target``(float tensor): ``(N,)
>>> from torchmetrics import PearsonCorrcoef
>>> target = torch.tensor([3, -0.5, 2, 7])
>>> preds = torch.tensor([2.5, 0.0, 2, 8])
>>> pearson = PearsonCorrcoef()
>>> pearson(preds, target)
tensor(0.9849)

PSNR

自 v0.4 版后已弃用:PSNR 已移至 torchmetrics.image.psnr。 它将在 v0.5 中删除。

初始化内部模块状态,由 nn.Module 和 ScriptModule 共享。s

R2Score

R2Score,计算 r2 分数也称为决定系数:

![R^2 = 1 - \frac{SS_res}{SS_tot}](https://torchmetrics.readthedocs.io/en/stable/_images/math/05595643373c668b229056e8bf5c746aaafe40bd.png

R^2 = 1 - \frac{SS_res}{SS_tot}

其中,SS_res=\sum_i (y_i - f(x_i))^2:残差平方和

SS_tot=\sum_i (y_i - \bar{y})^2:总平方和。

也可以计算调整后的 r2 分数由:

R^2_adj = 1 - \frac{(1-R^2)(n-1)}{n-k-1}

其中参数 k(独立回归量的数量)应作为调整后的参数提供。

Forward accepts

  • preds (float tensor): (N,) or (N, M) (multioutput)
  • target (float tensor): (N,) or (N, M) (multioutput)

在多输出的情况下,默认情况下,方差将在附加维度上均匀平均。 请参阅参数 multioutput 以更改此行为。

Example

>>>

>>> from torchmetrics import R2Score
>>> target = torch.tensor([3, -0.5, 2, 7])
>>> preds = torch.tensor([2.5, 0.0, 2, 8])
>>> r2score = R2Score()
>>> r2score(preds, target)
tensor(0.9486)

>>>

>>> target = torch.tensor([[0.5, 1], [-1, 1], [7, -6]])
>>> preds = torch.tensor([[0, 2], [-1, 2], [8, -5]])
>>> r2score = R2Score(num_outputs=2, multioutput='raw_values')
>>> r2score(preds, target)
tensor([0.9654, 0.9082])

SpearmanCorrcoef

计算 spearmans 等级相关系数。

其中 rg_x 和 rg_y 是与变量 x 和 y 关联的等级。 Spearmans 相关系数对应于对秩变量计算的标准 pearson 相关系数。

Example

>>>

>>> from torchmetrics import SpearmanCorrcoef
>>> target = torch.tensor([3, -0.5, 2, 7])
>>> preds = torch.tensor([2.5, 0.0, 2, 8])
>>> spearman = SpearmanCorrcoef()
>>> spearman(preds, target)
tensor(1.0000)

SSIM

自 v0.4 版后已弃用:SSIM 已移至 torchmetrics.image.ssim。 它将在 v0.5 中删除。

初始化内部模块状态,由 nn.Module 和 ScriptModule 共享。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值