INNER CODE UNIT · Python
get_self_information
liyucheng09/Selective_Context · app/app.py:79
def get_self_information(self, text: str) -> Tuple[List[str], List[float]]:
# it takes text as input, and return a list of words and a list of self-information scores
raise NotImplementedError
def _get_self_info_via_gpt2(self, text: str) -> Tuple[List[str], List[float]]:
if self.lang == 'en':
text = f"<|endoftext|>{text}"
elif self.lang == 'zh':
text = f"[CLS]{text}"
with torch.no_grad():
encoding = self.tokenizer(text, add_special_tokens=False, return_tensors='pt')
encoding = encoding.to(DEVICE)
outputs = self.model(**encoding)
logits = outputs.logits
probs = torch.softmax(logits, dim=-1)
self_info = -torch.log(probs)
input_ids = encoding['input_ids']