INNER CODE UNIT · Python
top_k_logits
HendrikStrobelt/detecting-fake-text · backend/api.py:56
def top_k_logits(logits, k):
"""
Filters logits to only the top k choices
from https://github.com/huggingface/pytorch-pretrained-BERT/blob/master/examples/run_gpt2.py
"""
if k == 0:
return logits
values, _ = torch.topk(logits, k)
min_values = values[:, -1]
return torch.where(logits < min_values,
torch.ones_like(logits, dtype=logits.dtype) * -1e10,
logits)
@register_api(name='gpt-2-small')
class LM(AbstractLanguageChecker):
def __init__(self, model_name_or_path="gpt2"):
super(LM, self).__init__()