! [ -e /content ] && pip install -Uqq fastai # 在Colab上升级fastai
指标
from __future__ import annotations
from fastai.data.all import *
from fastai.optimizer import *
from fastai.learner import *
from nbdev.showdoc import *
::: {#cell-4 .cell 0=‘d’ 1=‘e’ 2=‘f’ 3=‘a’ 4=‘u’ 5=‘l’ 6=‘t’ 7=’_’ 8=‘e’ 9=‘x’ 10=‘p’ 11=’ ’ 12=‘指’ 13=‘标’}
### 默认类级别 3
:::
用于训练模型的指标定义
核心指标
这是定义将scikit-learn指标转换为fastai指标的函数的地方。除非你想了解fastai的内部实现,否则应跳过此部分。
import sklearn.metrics as skm
import scipy.stats as scs
'ActivationType', **{o:o.lower() for o in ['No', 'Sigmoid', 'Softmax', 'BinarySoftmax']},
mk_class(="All possible activation classes for `AccumMetric") doc
class AccumMetric(Metric):
"Stores predictions and targets on CPU in accumulate to perform final calculations with `func`."
def __init__(self, func, dim_argmax=None, activation=ActivationType.No, thresh=None, to_np=False,
=False, flatten=True, name=None, **kwargs):
invert_arg'func,dim_argmax,activation,thresh,flatten')
store_attr(self._name = ifnone(name, self.func.func.__name__ if hasattr(self.func, 'func') else self.func.__name__)
self.to_np,self.invert_args,self.kwargs = to_np,invert_arg,kwargs
def reset(self):
"Clear all targs and preds"
self.targs,self.preds = [],[]
def accumulate(self, learn):
"Store targs and preds from `learn`, using activation function and argmax as appropriate"
= learn.pred
pred if self.activation in [ActivationType.Softmax, ActivationType.BinarySoftmax]:
= F.softmax(pred, dim=self.dim_argmax)
pred if self.activation == ActivationType.BinarySoftmax: pred = pred[:, -1]
elif self.activation == ActivationType.Sigmoid: pred = torch.sigmoid(pred)
elif self.dim_argmax: pred = pred.argmax(dim=self.dim_argmax)
if self.thresh: pred = (pred >= self.thresh)
self.accum_values(pred,learn.y,learn)
def accum_values(self, preds, targs,learn=None):
"Store targs and preds"
= learn.to_detach if learn is not None else to_detach
to_d = to_d(preds),to_d(targs)
preds,targs if self.flatten: preds,targs = flatten_check(preds,targs)
self.preds.append(preds)
self.targs.append(targs)
def __call__(self, preds, targs):
"Calculate metric on one batch of data"
self.reset()
self.accum_values(preds,targs)
return self.value
@property
def value(self):
"Value of the metric using accumulated preds and targs"
if len(self.preds) == 0: return
= torch.cat(self.preds),torch.cat(self.targs)
preds,targs if self.to_np: preds,targs = preds.numpy(),targs.numpy()
return self.func(targs, preds, **self.kwargs) if self.invert_args else self.func(preds, targs, **self.kwargs)
@property
def name(self): return self._name
@name.setter
def name(self, value): self._name = value
func
仅在请求 value
属性时应用于累积的预测/目标(因此在验证/训练阶段结束时,与 Learner
和其 Recorder
一起使用)。func
的签名应该是 inp,targ
(其中 inp
是模型的预测,targ
是相应的标签)。
对于单标签分类问题,预测需要先通过 softmax 转换,然后再通过 argmax 进行比较。由于 softmax 不会改变数字的顺序,我们只需应用 argmax。将 dim_argmax
传递给 AccumMetric
以进行此操作(通常 -1 会很好用)。如果您需要将概率而不是预测传递给您的指标,请使用 softmax=True
。
对于多标签分类问题,或者如果您的目标是独热编码的,预测可能需要经过 sigmoid(如果它没有包含在您的模型中),然后与给定的阈值进行比较(以决定 0 和 1 之间的选择),如果您传递 sigmoid=True
和/或阈值的值,这将由 AccumMetric
完成。
如果您想使用 sklearn.metrics 中的指标函数,您需要将预测和标签转换为 numpy 数组,使用 to_np=True
。另外,scikit-learn 的指标采用的约定是 y_true
和 y_preds
,这与我们相反,因此您需要传递 invert_arg=True
以使 AccumMetric
为您进行反转。
#用于测试:一个虚假的学习者和一个非平均值的度量标准
@delegates()
class TstLearner(Learner):
def __init__(self,dls=None,model=None,**kwargs): self.pred,self.xb,self.yb = None,None,None
def _l2_mean(x,y): return torch.sqrt((x.float()-y.float()).pow(2).mean())
#通过不同批量大小的伪循环并计算元值
def compute_val(met, x1, x2):
met.reset()= [0,6,15,20]
vals = TstLearner()
learn for i in range(3):
= x1[vals[i]:vals[i+1]],(x2[vals[i]:vals[i+1]],)
learn.pred,learn.yb
met.accumulate(learn)return met.value
= torch.randn(20,5),torch.randn(20,5) x1,x2
= AccumMetric(_l2_mean)
tst
test_close(compute_val(tst, x1, x2), _l2_mean(x1, x2))-1))
test_eq(torch.cat(tst.preds), x1.view(-1))
test_eq(torch.cat(tst.targs), x2.view(
#测试 argmax
= torch.randn(20,5),torch.randint(0, 5, (20,))
x1,x2 = AccumMetric(_l2_mean, dim_argmax=-1)
tst =-1), x2))
test_close(compute_val(tst, x1, x2), _l2_mean(x1.argmax(dim
#测试阈值
= torch.randn(20,5),torch.randint(0, 2, (20,5)).bool()
x1,x2 = AccumMetric(_l2_mean, thresh=0.5)
tst >= 0.5), x2))
test_close(compute_val(tst, x1, x2), _l2_mean((x1
#测试Sigmoid函数
= torch.randn(20,5),torch.randn(20,5)
x1,x2 = AccumMetric(_l2_mean, activation=ActivationType.Sigmoid)
tst
test_close(compute_val(tst, x1, x2), _l2_mean(torch.sigmoid(x1), x2))
#测试转换为NumPy数组
= torch.randn(20,5),torch.randn(20,5)
x1,x2 = AccumMetric(lambda x,y: isinstance(x, np.ndarray) and isinstance(y, np.ndarray), to_np=True)
tst assert compute_val(tst, x1, x2)
#测试反转参数
= torch.randn(20,5),torch.randn(20,5)
x1,x2 = AccumMetric(lambda x,y: torch.sqrt(x.pow(2).mean()))
tst pow(2).mean()))
test_close(compute_val(tst, x1, x2), torch.sqrt(x1.= AccumMetric(lambda x,y: torch.sqrt(x.pow(2).mean()), invert_arg=True)
tst pow(2).mean())) test_close(compute_val(tst, x1, x2), torch.sqrt(x2.
def _l2_mean(x,y): return torch.sqrt((x.argmax(dim=-1).float()-y.float()).pow(2).mean())
= torch.randn(20,5),torch.randint(0, 5, (20,))
x1,x2 = AccumMetric(_l2_mean, dim_argmax=-1, flatten=False, activation=ActivationType.Softmax)
tst =-1), x2)) test_close(compute_val(tst, x1, x2), _l2_mean(F.softmax(x1, dim
def skm_to_fastai(func, is_class=True, thresh=None, axis=-1, activation=None, **kwargs):
"Convert `func` from sklearn.metrics to a fastai metric"
= axis if is_class and thresh is None else None
dim_argmax if activation is None:
= ActivationType.Sigmoid if (is_class and thresh is not None) else ActivationType.No
activation return AccumMetric(func, dim_argmax=dim_argmax, activation=activation, thresh=thresh,
=True, invert_arg=True, **kwargs) to_np
这是在fastai训练循环中使用scikit-learn度量的最快方法。is_class
表示您是否处于分类问题中。在这种情况下:
- 将
thresh
设为None
表示这是一个单标签分类问题,预测将在axis
上通过argmax处理后与目标进行比较 - 为
thresh
设置一个值表示这是一个多标签分类问题,预测将通过sigmoid处理(可以通过sigma=False
停用),然后与thresh
进行比较,再与目标进行比较
如果is_class=False
,则表示您处于回归问题中,预测将直接与目标进行比较,未经过任何修改。在所有情况下,kwargs
是传递给func
的额外关键字参数。
= skm_to_fastai(skm.precision_score)
tst_single = torch.randn(20,2),torch.randint(0, 2, (20,))
x1,x2 =-1))) test_close(compute_val(tst_single, x1, x2), skm.precision_score(x2, x1.argmax(dim
= skm_to_fastai(skm.precision_score, thresh=0.2)
tst_multi = torch.randn(20),torch.randint(0, 2, (20,))
x1,x2 >= 0.2))
test_close(compute_val(tst_multi, x1, x2), skm.precision_score(x2, torch.sigmoid(x1)
= skm_to_fastai(skm.precision_score, thresh=0.2, activation=ActivationType.No)
tst_multi = torch.randn(20),torch.randint(0, 2, (20,))
x1,x2 >= 0.2)) test_close(compute_val(tst_multi, x1, x2), skm.precision_score(x2, x1
= skm_to_fastai(skm.r2_score, is_class=False)
tst_reg = torch.randn(20,5),torch.randn(20,5)
x1,x2 -1).numpy(), x1.view(-1).numpy())) test_close(compute_val(tst_reg, x1, x2), skm.r2_score(x2.view(
-1).numpy(), x1.view(-1).numpy())) test_close(tst_reg(x1, x2), skm.r2_score(x2.view(
def optim_metric(f, argname, bounds, tol=0.01, do_neg=True, get_x=False):
"Replace metric `f` with a version that optimizes argument `argname`"
def _f(preds, targs):
def minfunc(x):
= {argname:x}
kwargs = f(preds, targs, **kwargs)
res return -res if do_neg else res
= scipy.optimize.minimize_scalar(minfunc, bounds=bounds, method='bounded',
optres ={'xatol':0.01})
options= -optres.fun if do_neg else optres.fun
fun return (fun,optres.x) if get_x else fun
__name__ = f'opt_{f.__name__}'
_f.return _f
单标签分类
本节中定义的所有函数旨在用于单标签分类和未进行独热编码的目标。对于多标签问题或独热编码的目标,请使用后缀为多标签的版本。
fastai中的许多度量实际上是sklearn功能的简化包装。然而,sklearn度量可以处理Python列表字符串等其他类型,而fastai度量只能与PyTorch一起使用,因此需要张量。传递给度量的参数是在所有变换之后,例如类别被转换为索引后,因此,当你传递度量的标签时,例如,必须传递索引,而不是字符串。这可以通过vocab.map_obj
进行转换。
def accuracy(inp, targ, axis=-1):
"Compute accuracy with `targ` when `pred` is bs * n_classes"
= flatten_check(inp.argmax(dim=axis), targ)
pred,targ return (pred == targ).float().mean()
#用于测试
def change_targ(targ, n, c):
= torch.randperm(len(targ))[:n]
idx = targ.clone()
res for i in idx: res[i] = (res[i]+random.randint(1,c-1))%c
return res
= torch.randn(4,5)
x = x.argmax(dim=1)
y 1)
test_eq(accuracy(x,y), = change_targ(y, 2, 5)
y1 0.5)
test_eq(accuracy(x,y1), 1).expand(4,2,5), torch.stack([y,y1], dim=1)), 0.75) test_eq(accuracy(x.unsqueeze(
def error_rate(inp, targ, axis=-1):
"1 - `accuracy`"
return 1 - accuracy(inp, targ, axis=axis)
= torch.randn(4,5)
x = x.argmax(dim=1)
y 0)
test_eq(error_rate(x,y), = change_targ(y, 2, 5)
y1 0.5)
test_eq(error_rate(x,y1), 1).expand(4,2,5), torch.stack([y,y1], dim=1)), 0.25) test_eq(error_rate(x.unsqueeze(
def top_k_accuracy(inp, targ, k=5, axis=-1):
"Computes the Top-k accuracy (`targ` is in the top `k` predictions of `inp`)"
= inp.topk(k=k, dim=axis)[1]
inp = targ.unsqueeze(dim=axis).expand_as(inp)
targ return (inp == targ).sum(dim=-1).float().mean()
= torch.randn(6,5)
x = torch.arange(0,6)
y 5],y[:5]), 1)
test_eq(top_k_accuracy(x[:5/6) test_eq(top_k_accuracy(x, y),
def APScoreBinary(axis=-1, average='macro', pos_label=1, sample_weight=None):
"Average Precision for single-label binary classification problems"
return skm_to_fastai(skm.average_precision_score, axis=axis, activation=ActivationType.BinarySoftmax,
=average, pos_label=pos_label, sample_weight=sample_weight) average
请查看scikit-learn文档以获取更多详细信息。
def BalancedAccuracy(axis=-1, sample_weight=None, adjusted=False):
"Balanced Accuracy for single-label binary classification problems"
return skm_to_fastai(skm.balanced_accuracy_score, axis=axis,
=sample_weight, adjusted=adjusted) sample_weight
请参阅scikit-learn文档以获取更多详细信息。
def BrierScore(axis=-1, sample_weight=None, pos_label=None):
"Brier score for single-label classification problems"
return skm_to_fastai(skm.brier_score_loss, axis=axis,
=sample_weight, pos_label=pos_label) sample_weight
请参阅scikit-learn文档以获取更多详细信息。
def CohenKappa(axis=-1, labels=None, weights=None, sample_weight=None):
"Cohen kappa for single-label classification problems"
return skm_to_fastai(skm.cohen_kappa_score, axis=axis, labels=labels, weights=weights,
=sample_weight) sample_weight
请参阅scikit-learn文档以获取更多详细信息。
def F1Score(axis=-1, labels=None, pos_label=1, average='binary', sample_weight=None):
"F1 score for single-label classification problems"
return skm_to_fastai(skm.f1_score, axis=axis,
=labels, pos_label=pos_label, average=average, sample_weight=sample_weight) labels
有关更多详细信息,请参阅 scikit-learn 文档。
def FBeta(beta, axis=-1, labels=None, pos_label=1, average='binary', sample_weight=None):
"FBeta score with `beta` for single-label classification problems"
return skm_to_fastai(skm.fbeta_score, axis=axis,
=beta, labels=labels, pos_label=pos_label, average=average, sample_weight=sample_weight) beta
请查看 scikit-learn 文档 获取更多详细信息。
def HammingLoss(axis=-1, sample_weight=None):
"Hamming loss for single-label classification problems"
return skm_to_fastai(skm.hamming_loss, axis=axis,
=sample_weight) sample_weight
请参阅scikit-learn 文档以获取更多详细信息。
def Jaccard(axis=-1, labels=None, pos_label=1, average='binary', sample_weight=None):
"Jaccard score for single-label classification problems"
return skm_to_fastai(skm.jaccard_score, axis=axis,
=labels, pos_label=pos_label, average=average, sample_weight=sample_weight) labels
请参阅scikit-learn 文档以获取更多详细信息。
def Precision(axis=-1, labels=None, pos_label=1, average='binary', sample_weight=None):
"Precision for single-label classification problems"
return skm_to_fastai(skm.precision_score, axis=axis,
=labels, pos_label=pos_label, average=average, sample_weight=sample_weight) labels
请参阅 scikit-learn 文档 以获取更多详细信息。
def Recall(axis=-1, labels=None, pos_label=1, average='binary', sample_weight=None):
"Recall for single-label classification problems"
return skm_to_fastai(skm.recall_score, axis=axis,
=labels, pos_label=pos_label, average=average, sample_weight=sample_weight) labels
请参阅scikit-learn文档以获取更多详细信息。
def RocAuc(axis=-1, average='macro', sample_weight=None, max_fpr=None, multi_class='ovr'):
"Area Under the Receiver Operating Characteristic Curve for single-label multiclass classification problems"
assert multi_class in ['ovr', 'ovo']
return skm_to_fastai(skm.roc_auc_score, axis=axis, activation=ActivationType.Softmax, flatten=False,
=average, sample_weight=sample_weight, max_fpr=max_fpr, multi_class=multi_class) average
请参阅 scikit-learn 文档 以获取更多详细信息。
def RocAucBinary(axis=-1, average='macro', sample_weight=None, max_fpr=None, multi_class='raise'):
"Area Under the Receiver Operating Characteristic Curve for single-label binary classification problems"
return skm_to_fastai(skm.roc_auc_score, axis=axis, activation=ActivationType.BinarySoftmax,
=average, sample_weight=sample_weight, max_fpr=max_fpr, multi_class=multi_class) average
有关更多详情,请参阅 scikit-learn 文档。
def MatthewsCorrCoef(sample_weight=None, **kwargs):
"Matthews correlation coefficient for single-label classification problems"
return skm_to_fastai(skm.matthews_corrcoef, sample_weight=sample_weight, **kwargs)
请参见scikit-learn文档以获取更多详细信息。
多标签分类
def accuracy_multi(inp, targ, thresh=0.5, sigmoid=True):
"Compute accuracy when `inp` and `targ` are the same size."
= flatten_check(inp,targ)
inp,targ if sigmoid: inp = inp.sigmoid()
return ((inp>thresh)==targ.bool()).float().mean()
#用于测试
def change_1h_targ(targ, n):
= torch.randperm(targ.numel())[:n]
idx = targ.clone().view(-1)
res for i in idx: res[i] = 1-res[i]
return res.view(targ.shape)
= torch.randn(4,5)
x = (torch.sigmoid(x) >= 0.5).byte()
y 1)
test_eq(accuracy_multi(x,y), 1-y), 0)
test_eq(accuracy_multi(x,= change_1h_targ(y, 5)
y1 0.75)
test_eq(accuracy_multi(x,y1),
#不同阈值
= (torch.sigmoid(x) >= 0.2).byte()
y =0.2), 1)
test_eq(accuracy_multi(x,y, thresh1-y, thresh=0.2), 0)
test_eq(accuracy_multi(x,= change_1h_targ(y, 5)
y1 =0.2), 0.75)
test_eq(accuracy_multi(x,y1, thresh
#无Sigmoid
= (x >= 0.5).byte()
y =False), 1)
test_eq(accuracy_multi(x,y, sigmoid1-y, sigmoid=False), 0)
test_eq(accuracy_multi(x,= change_1h_targ(y, 5)
y1 =False), 0.75) test_eq(accuracy_multi(x,y1, sigmoid
def APScoreMulti(sigmoid=True, average='macro', pos_label=1, sample_weight=None):
"Average Precision for multi-label classification problems"
= ActivationType.Sigmoid if sigmoid else ActivationType.No
activation return skm_to_fastai(skm.average_precision_score, activation=activation, flatten=False,
=average, pos_label=pos_label, sample_weight=sample_weight) average
请参阅scikit-learn文档以获取更多详细信息。
def BrierScoreMulti(thresh=0.5, sigmoid=True, sample_weight=None, pos_label=None):
"Brier score for multi-label classification problems"
= ActivationType.Sigmoid if sigmoid else ActivationType.No
activation return skm_to_fastai(skm.brier_score_loss, thresh=thresh, activation=activation, flatten=False,
=sample_weight, pos_label=pos_label) sample_weight
请参阅scikit-learn文档以获取更多详细信息。
def F1ScoreMulti(thresh=0.5, sigmoid=True, labels=None, pos_label=1, average='macro', sample_weight=None):
"F1 score for multi-label classification problems"
= ActivationType.Sigmoid if sigmoid else ActivationType.No
activation return skm_to_fastai(skm.f1_score, thresh=thresh, activation=activation, flatten=False,
=labels, pos_label=pos_label, average=average, sample_weight=sample_weight) labels
请参阅scikit-learn文档以获取更多详细信息。
def FBetaMulti(beta, thresh=0.5, sigmoid=True, labels=None, pos_label=1, average='macro', sample_weight=None):
"FBeta score with `beta` for multi-label classification problems"
= ActivationType.Sigmoid if sigmoid else ActivationType.No
activation return skm_to_fastai(skm.fbeta_score, thresh=thresh, activation=activation, flatten=False,
=beta, labels=labels, pos_label=pos_label, average=average, sample_weight=sample_weight) beta
有关更多详细信息,请参阅 scikit-learn 文档。
def HammingLossMulti(thresh=0.5, sigmoid=True, labels=None, sample_weight=None):
"Hamming loss for multi-label classification problems"
= ActivationType.Sigmoid if sigmoid else ActivationType.No
activation return skm_to_fastai(skm.hamming_loss, thresh=thresh, activation=activation, flatten=False,
=sample_weight) sample_weight
请参阅 scikit-learn 文档 了解更多详情。
def JaccardMulti(thresh=0.5, sigmoid=True, labels=None, pos_label=1, average='macro', sample_weight=None):
"Jaccard score for multi-label classification problems"
= ActivationType.Sigmoid if sigmoid else ActivationType.No
activation return skm_to_fastai(skm.jaccard_score, thresh=thresh, activation=activation, flatten=False,
=labels, pos_label=pos_label, average=average, sample_weight=sample_weight) labels
请参阅scikit-learn文档获取更多详细信息。
def MatthewsCorrCoefMulti(thresh=0.5, sigmoid=True, sample_weight=None):
"Matthews correlation coefficient for multi-label classification problems"
= ActivationType.Sigmoid if sigmoid else ActivationType.No
activation return skm_to_fastai(skm.matthews_corrcoef, thresh=thresh, activation=activation, flatten=False, sample_weight=sample_weight)
请参阅scikit-learn 文档以获取更多详细信息。
def PrecisionMulti(thresh=0.5, sigmoid=True, labels=None, pos_label=1, average='macro', sample_weight=None):
"Precision for multi-label classification problems"
= ActivationType.Sigmoid if sigmoid else ActivationType.No
activation return skm_to_fastai(skm.precision_score, thresh=thresh, activation=activation, flatten=False,
=labels, pos_label=pos_label, average=average, sample_weight=sample_weight) labels
查看scikit-learn文档以获取更多详细信息。
def RecallMulti(thresh=0.5, sigmoid=True, labels=None, pos_label=1, average='macro', sample_weight=None):
"Recall for multi-label classification problems"
= ActivationType.Sigmoid if sigmoid else ActivationType.No
activation return skm_to_fastai(skm.recall_score, thresh=thresh, activation=activation, flatten=False,
=labels, pos_label=pos_label, average=average, sample_weight=sample_weight) labels
请参阅scikit-learn文档以获取更多详细信息。
def RocAucMulti(sigmoid=True, average='macro', sample_weight=None, max_fpr=None):
"Area Under the Receiver Operating Characteristic Curve for multi-label binary classification problems"
= ActivationType.Sigmoid if sigmoid else ActivationType.No
activation return skm_to_fastai(skm.roc_auc_score, activation=activation, flatten=False,
=average, sample_weight=sample_weight, max_fpr=max_fpr) average
= RocAucMulti(sigmoid=False)
roc_auc_metric = torch.tensor([np.arange(start=0, stop=0.2, step=0.04)]*20), torch.tensor([0, 0, 1, 1]).repeat(5)
x,y assert compute_val(roc_auc_metric, x, y) == 0.5
/var/folders/ss/34z569j921v58v8n1n_8z7h40000gn/T/ipykernel_38355/1899176771.py:2: UserWarning: Creating a tensor from a list of numpy.ndarrays is extremely slow. Please consider converting the list to a single numpy.ndarray with numpy.array() before converting to a tensor. (Triggered internally at /Users/runner/work/_temp/anaconda/conda-bld/pytorch_1712608632396/work/torch/csrc/utils/tensor_new.cpp:277.)
x,y = torch.tensor([np.arange(start=0, stop=0.2, step=0.04)]*20), torch.tensor([0, 0, 1, 1]).repeat(5)
有关更多详细信息,请参阅scikit-learn文档。
回归
def mse(inp,targ):
"Mean squared error between `inp` and `targ`."
return F.mse_loss(*flatten_check(inp,targ))
= torch.randn(4,5),torch.randn(4,5)
x1,x2 -x2).pow(2).mean()) test_close(mse(x1,x2), (x1
def _rmse(inp, targ): return torch.sqrt(F.mse_loss(inp, targ))
= AccumMetric(_rmse)
rmse = "Root mean squared error" rmse.__doc__
="rmse") show_doc(rmse, name
= torch.randn(20,5),torch.randn(20,5)
x1,x2 test_eq(compute_val(rmse, x1, x2), torch.sqrt(F.mse_loss(x1,x2)))
def mae(inp,targ):
"Mean absolute error between `inp` and `targ`."
= flatten_check(inp,targ)
inp,targ return torch.abs(inp - targ).mean()
= torch.randn(4,5),torch.randn(4,5)
x1,x2 abs(x1-x2).mean()) test_eq(mae(x1,x2), torch.
def msle(inp, targ):
"Mean squared logarithmic error between `inp` and `targ`."
= flatten_check(inp,targ)
inp,targ return F.mse_loss(torch.log(1 + inp), torch.log(1 + targ))
= torch.randn(4,5),torch.randn(4,5)
x1,x2 = torch.relu(x1),torch.relu(x2)
x1,x2 +1)-torch.log(x2+1)).pow(2).mean()) test_close(msle(x1,x2), (torch.log(x1
def _exp_rmspe(inp,targ):
= torch.exp(inp),torch.exp(targ)
inp,targ return torch.sqrt(((targ - inp)/targ).pow(2).mean())
= AccumMetric(_exp_rmspe)
exp_rmspe = "Root mean square percentage error of the exponential of predictions and targets" exp_rmspe.__doc__
="exp_rmspe") show_doc(exp_rmspe, name
exp_rmspe
exp_rmspe (preds, targs)
Root mean square percentage error of the exponential of predictions and targets
= torch.randn(20,5),torch.randn(20,5)
x1,x2 - torch.exp(x1))/torch.exp(x2))**2).mean())) test_eq(compute_val(exp_rmspe, x1, x2), torch.sqrt((((torch.exp(x2)
def ExplainedVariance(sample_weight=None):
"Explained variance between predictions and targets"
return skm_to_fastai(skm.explained_variance_score, is_class=False, sample_weight=sample_weight)
请查看 scikit-learn 文档 以获取更多详情。
def R2Score(sample_weight=None):
"R2 score between predictions and targets"
return skm_to_fastai(skm.r2_score, is_class=False, sample_weight=sample_weight)
请参阅scikit-learn文档以获取更多详细信息。
@delegates(AccumMetric)
def PearsonCorrCoef(dim_argmax=None, **kwargs):
"Pearson correlation coefficient for regression problem"
def pearsonr(x,y): return scs.pearsonr(x,y)[0]
return AccumMetric(pearsonr, invert_arg=False, dim_argmax=dim_argmax, **kwargs)
请参阅scipy文档获取更多详细信息。
= torch.randint(-999, 999,(20,))
x = torch.randint(-999, 999,(20,))
y -1), y.view(-1))[0]) test_eq(compute_val(PearsonCorrCoef(), x, y), scs.pearsonr(x.view(
@delegates(AccumMetric)
def SpearmanCorrCoef(dim_argmax=None, axis=0, nan_policy='propagate', **kwargs):
"Spearman correlation coefficient for regression problem"
def spearmanr(a,b=None,**kwargs): return scs.spearmanr(a,b,**kwargs)[0]
return AccumMetric(partial(spearmanr, axis=axis, nan_policy=nan_policy),
=False, dim_argmax=dim_argmax, **kwargs) invert_arg
请参阅 scipy 文档 以获取更多详情。
= torch.randint(-999, 999,(20,))
x = torch.randint(-999, 999,(20,))
y -1), y.view(-1))[0]) test_eq(compute_val(SpearmanCorrCoef(), x, y), scs.spearmanr(x.view(
分割
from fastai.vision.all import *
= resnet34() model
= cast(torch.rand(1,3,128,128), TensorImage) x
type(model(x))
fastai.torch_core.TensorImage
def foreground_acc(inp, targ, bkg_idx=0, axis=1):
"Computes non-background accuracy for multiclass segmentation"
= cast(targ.squeeze(1), TensorBase)
targ = targ != bkg_idx
mask return (inp.argmax(dim=axis)[mask]==targ[mask]).float().mean()
= cast(torch.randn(4,5,3,3), TensorImage)
x = cast(x, TensorMask).argmax(dim=1)[:,None]
y 1)
test_eq(foreground_acc(x,y), 0] = 0 #0被忽略,因此我们得到相同的值
y[1) test_eq(foreground_acc(x,y),
class Dice(Metric):
"Dice coefficient metric for binary target in segmentation"
def __init__(self, axis=1): self.axis = axis
def reset(self): self.inter,self.union = 0,0
def accumulate(self, learn):
= flatten_check(learn.pred.argmax(dim=self.axis), learn.y)
pred,targ self.inter += (pred*targ).float().sum().item()
self.union += (pred+targ).float().sum().item()
@property
def value(self): return 2. * self.inter/self.union if self.union > 0 else None
= cast(torch.randn(20,2,3,3), TensorImage)
x1 = cast(torch.randint(0, 2, (20, 3, 3)), TensorMask)
x2 = x1.argmax(1)
pred = (pred*x2).float().sum().item()
inter = (pred+x2).float().sum().item()
union 2*inter/union) test_eq(compute_val(Dice(), x1, x2),
class DiceMulti(Metric):
"Averaged Dice metric (Macro F1) for multiclass target in segmentation"
def __init__(self, axis=1): self.axis = axis
def reset(self): self.inter,self.union = {},{}
def accumulate(self, learn):
= flatten_check(learn.pred.argmax(dim=self.axis), learn.y)
pred,targ for c in range(learn.pred.shape[self.axis]):
= torch.where(pred == c, 1, 0)
p = torch.where(targ == c, 1, 0)
t = (p*t).float().sum().item()
c_inter = (p+t).float().sum().item()
c_union if c in self.inter:
self.inter[c] += c_inter
self.union[c] += c_union
else:
self.inter[c] = c_inter
self.union[c] = c_union
@property
def value(self):
= np.array([])
binary_dice_scores for c in self.inter:
= np.append(binary_dice_scores, 2.*self.inter[c]/self.union[c] if self.union[c] > 0 else np.nan)
binary_dice_scores return np.nanmean(binary_dice_scores)
DiceMulti方法实现了这篇出版物中描述的“平均F1:对调和平均的算术平均”,该出版物链接为:https://arxiv.org/pdf/1911.03347.pdf
= torch.ones(20,1,1,1)
x1a = torch.clone(x1a)*0.5
x1b = torch.clone(x1a)*0.3
x1c = torch.cat((x1a,x1b,x1c),dim=1) # 预测:20倍于0类
x1 = torch.zeros(20,1,1) # 目标:20倍类0
x2 1.)
test_eq(compute_val(DiceMulti(), x1, x2),
= torch.ones(20,1,1) # 目标:20倍类1
x2 0.)
test_eq(compute_val(DiceMulti(), x1, x2),
= torch.zeros(10,1,1)
x2a = torch.ones(5,1,1)
x2b = torch.ones(5,1,1) * 2
x2c = torch.cat((x2a,x2b,x2c),dim=0) # 目标:10个类别0,5个类别1,5个类别2
x2 = (2*10)/(2*10+10) # Dice系数:2*TP/(2*TP+FP+FN)
dice1 = 0
dice2 = 0
dice3 +dice2+dice3)/3) test_eq(compute_val(DiceMulti(), x1, x2), (dice1
class JaccardCoeff(Dice):
"Implementation of the Jaccard coefficient that is lighter in RAM"
@property
def value(self): return self.inter/(self.union-self.inter) if self.union > 0 else None
= cast(torch.randn(20,2,3,3), TensorImage)
x1 = cast(torch.randint(0, 2, (20, 3, 3)), TensorMask)
x2 = x1.argmax(1)
pred = (pred*x2).float().sum().item()
inter = (pred+x2).float().sum().item()
union /(union-inter)) test_eq(compute_val(JaccardCoeff(), x1, x2), inter
class JaccardCoeffMulti(DiceMulti):
"Averaged Jaccard coefficient metric (mIoU) for multiclass target in segmentation"
@property
def value(self):
= np.array([])
binary_jaccard_scores for c in self.inter:
= np.append(binary_jaccard_scores, self.inter[c]/(self.union[c]-self.inter[c]) if self.union[c] > 0 else np.nan)
binary_jaccard_scores return np.nanmean(binary_jaccard_scores)
= torch.ones(20,1,1,1)
x1a = torch.clone(x1a)*0.5
x1b = torch.clone(x1a)*0.3
x1c = torch.cat((x1a,x1b,x1c), dim=1) # 预测:20倍于第0类
x1 = torch.zeros(20,1,1) # 目标:20倍类0
x2 1.)
test_eq(compute_val(JaccardCoeffMulti(), x1, x2),
= torch.ones(20,1,1) # 目标:20倍一级
x2 0.)
test_eq(compute_val(JaccardCoeffMulti(), x1, x2),
= torch.zeros(10,1,1)
x2a = torch.ones(5,1,1)
x2b = torch.ones(5,1,1) * 2
x2c = torch.cat((x2a,x2b,x2c), dim=0) # 目标:10个类别0,5个类别1,5个类别2
x2 = 10/(10+10) # Jaccard系数:TP/(TP+FP+FN)
jcrd1 = 0
jcrd2 = 0
jcrd3 +jcrd2+jcrd3)/3) test_eq(compute_val(JaccardCoeffMulti(), x1, x2), (jcrd1
自然语言处理
class CorpusBLEUMetric(Metric):
def __init__(self, vocab_sz=5000, axis=-1):
"BLEU Metric calculated over the validation corpus"
self.metric_name = 'CorpusBLEU'
self.axis, self.vocab_sz = axis, vocab_sz
self.pred_len,self.targ_len,self.samp_idx,self.corrects,self.counts, = 0,0,0,[0]*4,[0]*4
def reset(self):
self.pred_len,self.targ_len,self.corrects,self.counts = 0,0,[0]*4,[0]*4
class NGram():
def __init__(self, ngram, max_n=5000): self.ngram,self.max_n = ngram,max_n
def __eq__(self, other):
if len(self.ngram) != len(other.ngram): return False
return np.all(np.array(self.ngram) == np.array(other.ngram))
def __hash__(self): return int(sum([o * self.max_n**i for i,o in enumerate(self.ngram)]))
def get_grams(self, x, n, max_n=5000):
return x if n==1 else [self.NGram(x[i:i+n], max_n=max_n) for i in range(len(x)-n+1)]
def get_correct_ngrams(self, pred, targ, n, max_n=5000):
= self.get_grams(pred, n, max_n=max_n),self.get_grams(targ, n, max_n=max_n)
pred_grams,targ_grams = Counter(pred_grams),Counter(targ_grams)
pred_cnt,targ_cnt return sum([min(c, targ_cnt[g]) for g,c in pred_cnt.items()]),len(pred_grams)
def accumulate(self, learn):
if learn.training: return None
else:
= learn.pred.argmax(dim=self.axis)
last_output = learn.y
last_target for pred,targ in zip(last_output.cpu().numpy(),last_target.cpu().numpy()):
self.pred_len += len(pred)
self.targ_len += len(targ)
= 1
smooth_mteval for i in range(4):
= self.get_correct_ngrams(pred, targ, i+1, max_n=self.vocab_sz)
c,t if c == 0:
*= 2
smooth_mteval = 1 / smooth_mteval # 指数平滑法,摘自http://acl2014.org/acl2014/W14-33/pdf/W14-3346.pdf中的方法3
c self.corrects[i] += c
self.counts[i] += t
@property
def value(self):
if self.counts == 0: return None
elif max(self.corrects) == 0: return 0.0
else:
= [c/t for c,t in zip(self.corrects,self.counts)]
precs = math.exp(1 - self.targ_len/self.pred_len) if self.pred_len < self.targ_len else 1
len_penalty return len_penalty * ((precs[0]*precs[1]*precs[2]*precs[3]) ** 0.25)
def create_vcb_emb(pred, targ):
# create vocab "embedding" for predictions
= max(torch.unique(torch.cat([pred, targ])))+1
vcb_sz =torch.zeros(pred.size()[0], pred.size()[1] ,vcb_sz)
pred_embfor i,v in enumerate(pred):
1, v.view(len(v),1),1)
pred_emb[i].scatter_(return pred_emb
def compute_bleu_val(met, x1, x2):
met.reset()= TstLearner()
learn =False
learn.trainingfor i in range(len(x1)):
= x1, (x2,)
learn.pred,learn.yb
met.accumulate(learn)return met.value
= torch.tensor([[1,2,3,4,5,6,1,7,8]])
targ = torch.tensor([[1,9,3,4,5,6,1,10,8]])
pred = create_vcb_emb(pred, targ)
pred_emb 0.48549)
test_close(compute_bleu_val(CorpusBLEUMetric(), pred_emb, targ),
= torch.tensor([[1,2,3,4,5,6,1,7,8],[1,2,3,4,5,6,1,7,8]])
targ = torch.tensor([[1,9,3,4,5,6,1,10,8],[1,9,3,4,5,6,1,10,8]])
pred = create_vcb_emb(pred, targ)
pred_emb 0.48549) test_close(compute_bleu_val(CorpusBLEUMetric(), pred_emb, targ),
BLEU指标在这篇文章中提出,用以评估翻译模型的性能。它基于你预测中的n-gram与目标的精确度。有关BLEU的更详细描述,请参见fastai NLP课程的BLEU笔记。
在精确度计算中使用的平滑技术与SacreBLEU中的相同,而后者实际上是Chen & Cherry,2014论文中的“方法3”。
class Perplexity(AvgLoss):
"Perplexity (exponential of cross-entropy loss) for Language Models"
@property
def value(self): return torch.exp(self.total/self.count) if self.count != 0 else None
@property
def name(self): return "perplexity"
= Perplexity() perplexity
= torch.randn(20,5),torch.randint(0, 5, (20,))
x1,x2 = perplexity
tst
tst.reset()= [0,6,15,20]
vals = TstLearner()
learn for i in range(3):
= (x2[vals[i]:vals[i+1]],)
learn.yb = F.cross_entropy(x1[vals[i]:vals[i+1]],x2[vals[i]:vals[i+1]])
learn.loss
tst.accumulate(learn) test_close(tst.value, torch.exp(F.cross_entropy(x1,x2)))
损失指标 -
class LossMetric(AvgMetric):
"Create a metric from `loss_func.attr` named `nm`"
def __init__(self, attr, nm=None): store_attr('attr,nm')
def accumulate(self, learn):
= find_bs(learn.yb)
bs self.total += learn.to_detach(getattr(learn.loss_func, self.attr, 0))*bs
self.count += bs
@property
def name(self): return self.attr if self.nm is None else self.nm
def LossMetrics(attrs, nms=None):
"List of `LossMetric` for each of `attrs` and `nms`"
if isinstance(attrs, str): attrs = attrs.split(',')
= attrs if nms is None else nms.split(',') if isinstance(nms, str) else nms
nms return [LossMetric(a, n) for a,n in zip(attrs,nms)]
from fastai.test_utils import *
class CombineL1L2(Module):
def forward(self, out, targ):
self.l1 = F.l1_loss(out, targ)
self.l2 = F.mse_loss(out, targ)
return self.l1+self.l2
= synth_learner(metrics=LossMetrics('l1,l2'))
learn = CombineL1L2()
learn.loss_func 2) learn.fit(
epoch | train_loss | valid_loss | l1 | l2 | time |
---|---|---|---|---|---|
0 | 15.296746 | 12.515826 | 3.019884 | 9.495943 | 00:00 |
1 | 13.290909 | 8.719325 | 2.454751 | 6.264574 | 00:00 |
导出 -
from nbdev import nbdev_export
nbdev_export()
Converted 00_torch_core.ipynb.
Converted 01_layers.ipynb.
Converted 01a_losses.ipynb.
Converted 02_data.load.ipynb.
Converted 03_data.core.ipynb.
Converted 04_data.external.ipynb.
Converted 05_data.transforms.ipynb.
Converted 06_data.block.ipynb.
Converted 07_vision.core.ipynb.
Converted 08_vision.data.ipynb.
Converted 09_vision.augment.ipynb.
Converted 09b_vision.utils.ipynb.
Converted 09c_vision.widgets.ipynb.
Converted 10_tutorial.pets.ipynb.
Converted 10b_tutorial.albumentations.ipynb.
Converted 11_vision.models.xresnet.ipynb.
Converted 12_optimizer.ipynb.
Converted 13_callback.core.ipynb.
Converted 13a_learner.ipynb.
Converted 13b_metrics.ipynb.
Converted 14_callback.schedule.ipynb.
Converted 14a_callback.data.ipynb.
Converted 15_callback.hook.ipynb.
Converted 15a_vision.models.unet.ipynb.
Converted 16_callback.progress.ipynb.
Converted 17_callback.tracker.ipynb.
Converted 18_callback.fp16.ipynb.
Converted 18a_callback.training.ipynb.
Converted 18b_callback.preds.ipynb.
Converted 19_callback.mixup.ipynb.
Converted 20_interpret.ipynb.
Converted 20a_distributed.ipynb.
Converted 20b_tutorial.distributed.ipynb.
Converted 21_vision.learner.ipynb.
Converted 22_tutorial.imagenette.ipynb.
Converted 23_tutorial.vision.ipynb.
Converted 24_tutorial.image_sequence.ipynb.
Converted 24_tutorial.siamese.ipynb.
Converted 24_vision.gan.ipynb.
Converted 30_text.core.ipynb.
Converted 31_text.data.ipynb.
Converted 32_text.models.awdlstm.ipynb.
Converted 33_text.models.core.ipynb.
Converted 34_callback.rnn.ipynb.
Converted 35_tutorial.wikitext.ipynb.
Converted 37_text.learner.ipynb.
Converted 38_tutorial.text.ipynb.
Converted 39_tutorial.transformers.ipynb.
Converted 40_tabular.core.ipynb.
Converted 41_tabular.data.ipynb.
Converted 42_tabular.model.ipynb.
Converted 43_tabular.learner.ipynb.
Converted 44_tutorial.tabular.ipynb.
Converted 45_collab.ipynb.
Converted 46_tutorial.collab.ipynb.
Converted 50_tutorial.datablock.ipynb.
Converted 60_medical.imaging.ipynb.
Converted 61_tutorial.medical_imaging.ipynb.
Converted 65_medical.text.ipynb.
Converted 70_callback.wandb.ipynb.
Converted 70a_callback.tensorboard.ipynb.
Converted 70b_callback.neptune.ipynb.
Converted 70c_callback.captum.ipynb.
Converted 70d_callback.comet.ipynb.
Converted 74_huggingface.ipynb.
Converted 97_test_utils.ipynb.
Converted 99_pytorch_doc.ipynb.
Converted dev-setup.ipynb.
Converted app_examples.ipynb.
Converted camvid.ipynb.
Converted distributed_app_examples.ipynb.
Converted migrating_catalyst.ipynb.
Converted migrating_ignite.ipynb.
Converted migrating_lightning.ipynb.
Converted migrating_pytorch.ipynb.
Converted migrating_pytorch_verbose.ipynb.
Converted ulmfit.ipynb.
Converted index.ipynb.
Converted quick_start.ipynb.
Converted tutorial.ipynb.