File size: 419 Bytes
179891c
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

import torch, math

def compute_perplexity(model, tok, texts):
    losses=[]
    device="cpu"
    model.eval()
    with torch.no_grad():
        for t in texts:
            if not t.strip(): continue
            inp = tok(t, return_tensors="pt", truncation=True).to(device)
            out = model(**inp, labels=inp["input_ids"])
            losses.append(out.loss.item())
    return math.exp(sum(losses)/len(losses))