Hi!
Since, as far as I know, the following common metrics are not implemented by default in fastai, I am trying to write them myself and I would like to hear some feedback from you guys.
The names of the functions are self-explanatory:
def top_k_precision(input, targs, k=5):
input = input.topk(k=k, dim=-1)[1]
batch_size = targs.size(0)
targs = targs.gather(dim=-1, index=input)
return (targs.sum(dim=-1) / k).mean()
def top_k_recall(input, targs, k=5):
k_input = input.topk(k=k, dim=-1)[1]
k_targs = targs.gather(dim=-1, index=k_input)
return (k_targs.sum(dim=-1) / targs.sum(dim=-1)).mean()
def top_k_f1_score(input, targs, k=5, eps=1e-9):
k_input = input.topk(k=k, dim=-1)[1]
k_targs = targs.gather(dim=-1, index=k_input)
prec = k_targs.sum(dim=-1) / k
rec = k_targs.sum(dim=-1) / targs.sum(dim=-1)
return (2 * prec * rec / (prec + rec + eps)).mean()
`