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

Change perplexity to be calculated with base e #242

Merged
merged 10 commits into from
Aug 15, 2022
Merged

Conversation

mathemakitten
Copy link
Contributor

@mathemakitten mathemakitten commented Aug 10, 2022

Merging with the open docs PR for perplexity, #238.

Closes #241.

@HuggingFaceDocBuilderDev
Copy link

HuggingFaceDocBuilderDev commented Aug 10, 2022

The documentation is not available anymore as the PR was closed or merged.

@mathemakitten mathemakitten marked this pull request as draft August 10, 2022 19:03
@mathemakitten
Copy link
Contributor Author

mathemakitten commented Aug 10, 2022

A comparison, for reference, on the sentence ['Hugging Face is a startup based in New York City and Paris']

Previously, base 2:

import evaluate
perplexity = evaluate.load("perplexity", module_type="metric")
input_texts = ['Hugging Face is a startup based in New York City and Paris']
results = perplexity.compute(model_id='gpt2',
                             add_start_token=False,
                             predictions=input_texts)
print(list(results.keys()))

ppl = 19.1218

Now, base e: ppl = 70.6083

Compare with the canonical example in transformers from here:

encodings = tokenizer(["Hugging Face is a startup based in New York City and Paris"], return_tensors="pt")

max_length = model.config.n_positions
stride = 512

nlls = []
for i in tqdm(range(0, encodings.input_ids.size(1), stride)):
    begin_loc = max(i + stride - max_length, 0)
    end_loc = min(i + stride, encodings.input_ids.size(1))
    trg_len = end_loc - i  # may be different from stride on last loop
    input_ids = encodings.input_ids[:, begin_loc:end_loc].to(device)
    target_ids = input_ids.clone()
    target_ids[:, :-trg_len] = -100

    with torch.no_grad():
        outputs = model(input_ids, labels=target_ids)
        neg_log_likelihood = outputs[0] * trg_len

    nlls.append(neg_log_likelihood)

ppl = torch.exp(torch.stack(nlls).sum() / end_loc)

ppl = 70.6075

And the usual:

model = GPT2LMHeadModel.from_pretrained('gpt2')
tokenizer = GPT2TokenizerFast.from_pretrained('gpt2')

loss = model(input_ids, labels=input_ids)[0]
print(np.exp(loss.cpu().detach().numpy()))

ppl = 70.60746

@mathemakitten mathemakitten marked this pull request as ready for review August 11, 2022 00:05
Copy link
Member

@lvwerra lvwerra left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for updating this and fixing the docs. Looks good to me, the only thing I would add is an explicit comment in the docstring (_DESCRIPTION) as well as at the very beginning of the readme that we compute ppl with base e.

@mathemakitten mathemakitten merged commit 940d6de into main Aug 15, 2022
@mathemakitten mathemakitten deleted the hn-base-e branch August 15, 2022 17:37
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

Successfully merging this pull request may close these issues.

Perplexity with torch.exp2 instead of torch.exp
3 participants