Upload metrics.py with huggingface_hub
Browse files3 unmodified lines456789101349 unmodified lines13601361136213631364136513661367136836 unmodified lines140514061407140814091410141114121413141414154 unmodified lines142014211422142314241425142614273 unmodified linesfrom abc import ABC, abstractmethodfrom collections import Counterfrom dataclasses import fieldfrom statistics import meanfrom typing import Any, Dict, Generator, List, Optional, Tupleimport evaluate1349 unmodified linesinstance_scores_list.append(scores[index])index += 1instance_scores["reference_scores"] = instance_scores_listinstance_scores[self.main_score] = mean(instance_scores_list)instance_scores[self.main_score] = mean(instance_scores_list)all_instances_scores.append(instance_scores)return all_instances_scores36 unmodified linestokens_source, tokens_target)# the model returns mean over all batch. We run the CE again without reduction# and extarct the mean for each documentloss_fct = torch.nn.CrossEntropyLoss(ignore_index=-100, reduction="none")loss = loss_fct(logits.view(-1, logits.size(-1)), labels.view(-1))4 unmodified lineslabels > 0, dim=1)# append the batch scores to the list of all scoresscores.append(batch_loss)return torch.cat(scores, dim=0).tolist()3 unmodified lines4567891349 unmodified lines13591360136113621363136413651366136713681369137036 unmodified lines1407140814091410141114121413141414151416141714181419142014211422142314244 unmodified lines142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614573 unmodified linesfrom abc import ABC, abstractmethodfrom collections import Counterfrom dataclasses import fieldfrom typing import Any, Dict, Generator, List, Optional, Tupleimport evaluate1349 unmodified linesinstance_scores_list.append(scores[index])index += 1instance_scores["reference_scores"] = instance_scores_list# max seems more useful than mean for common use cases like# context relevance, where what we want to know is if there# is at least one good result in the context. Using mean will# bring the score down due to bad contexts at the tail.instance_scores[self.main_score] = max(instance_scores_list)all_instances_scores.append(instance_scores)return all_instances_scores36 unmodified linestokens_source, tokens_target)# logits is a tensor of size: batch_size * len(target) * vocab_size# because for each example in the batch, the model predicted the# logit at every position in the target, for every vocab item.# the model returns mean over all batch. We run the CE again without reduction# and extract the mean for each documentloss_fct = torch.nn.CrossEntropyLoss(ignore_index=-100, reduction="none")# logits.size(-1) = the dimension of the vocabulary# labels.view(-1) = flattens the labels tensor to 1dloss = loss_fct(logits.view(-1, logits.size(-1)), labels.view(-1))4 unmodified lineslabels > 0, dim=1)# e^-average(cross-entropy-loss(logits) == geometric mean of the probabilities# proof:# * CE-loss of logits is computed by transforming the logits to# probabilities by softmax, and then -log(p) is returned, where# p is the probability of the gold label.# * Averaging the CE loss is computed by summing over -log(p) and# then dividing by the length of the gold labels.# * Thus, pr_score = (-log(p_1) + ... + -log(p_n)) / n# = -log(p_1 * ... * p_n) * 1/n# * Therefore,# e^(-pr_score) = e^(log(p_1 * ... * p_n) * 1/n)# = (e^(log(p_1 * ... * p_n))) ^ 1/n# = p_1 * ... * p_n) ^ 1/n# = geometric mean of [p_1, ..., p_n]## in principle we could have computed the geometric mean directly over the# probabilities instead of e^(average cross entropy loss of the logits),# but the current approach is more stable numerically. See for example:# https://stackoverflow.com/questions/59722983/how-to-calculate-geometric-mean-in-a-differentiable-waygeometric_mean = (-batch_loss).exp()# append the batch scores to the list of all scoresscores.append(geometric_mean)return torch.cat(scores, dim=0).tolist()
metrics.py+35-5