Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add geometric mean of per-token-Perplexities #551

Open
LuggiStruggi opened this issue Feb 23, 2024 · 0 comments
Open

Add geometric mean of per-token-Perplexities #551

LuggiStruggi opened this issue Feb 23, 2024 · 0 comments

Comments

@LuggiStruggi
Copy link

LuggiStruggi commented Feb 23, 2024

Hi I'm new to this and might to be overlooking smth, but it seems to me that more often than the arithmetic mean over perplexity per batch, the geometric mean over all per-token-Perplexities is used. I.e here in huggingface's Transformers docs. So basically just what you calculate as a single perplexity per batch but for the whole dataset. Would be helpful to have that returned here additionally.
So smth along the lines of (just to understand what i mean. Didn't test the code):

ppls = []
nll_sum = 0
total_tokens = 0

for start_index in logging.tqdm(range(0, len(encoded_texts), batch_size)):
          
          [...]
            
           negative_log_likelihood_batch = loss_fct(shift_logits.transpose(1, 2), shift_labels) * shift_attention_mask_batch).sum(1)
          
           perplexity_batch = torch.exp(negative_log_likelihood_batch / shift_attention_mask_batch.sum(1))

           ppls += perplexity_batch.tolist()
           nll_sum += negative_log_likelihood_batch.sum().item()
           total_tokens += shift_attention_mask_batch.sum().item()

        return {"perplexities": ppls,
                "mean_perplexity": np.mean(ppls),
                "geometric_mean_perplexity": torch.exp(nll_sum / total_tokens)}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant