(self, partial_out)
return dist
Return probability distribution over next words given an input and
        partial true output. This is used to calculate the per-word perplexity.
Return probability distribution over next words given an input and
        partial true output. This is used to calculate the per-word perplexity.
Return probability distribution over next words given an input and partial true output . This is used to calculate the per - word perplexity .
def next_word_probability(self, partial_out):
        """Return probability distribution over next words given an input and
        partial true output. This is used to calculate the per-word perplexity.
        """
        partial_out_ids = self.tokenizer.encode(' '.join(partial_out))
        instance = build_input_from_segments(self.persona, self.history, partial_out_ids,
                                             self.tokenizer, with_eos=False)

        input_ids = torch.tensor(instance["input_ids"], device=self.args.device).unsqueeze(0)
        token_type_ids = torch.tensor(instance["token_type_ids"], device=self.args.device).unsqueeze(0)

        with torch.no_grad():
            logits = self.model_checkpoint(input_ids, token_type_ids=token_type_ids)

        if isinstance(logits, tuple):  # for gpt2 and maybe others
            logits = logits[0]
        probs = F.softmax(logits[0, -1], dim=0)

        dist = {}
        for prefix_id, words in self.prefix2words.items():
            for word, ratio in words.items():
                dist[word] = probs[prefix_id].item() * ratio
        return dist
def next_word_probability ( self , partial_out ) : partial_out_ids = self . tokenizer . encode ( ' ' . join ( partial_out ) ) instance = build_input_from_segments ( self . persona , self . history , partial_out_ids , self . tokenizer , with_eos = False ) input_ids = torch . tensor ( instance [ "input_ids" ] , device = self . args . device ) . unsqueeze ( 0 ) token_type_ids = torch . tensor ( instance [ "token_type_ids" ] , device = self . args . device ) . unsqueeze ( 0 ) with torch . no_grad ( ) : logits = self . model_checkpoint ( input_ids , token_type_ids = token_type_ids ) if isinstance ( logits , tuple ) : # for gpt2 and maybe others logits = logits [ 0 ] probs = F . softmax ( logits [ 0 , - 1 ] , dim = 0 ) dist = { } for prefix_id , words in self . prefix2words . items ( ) : for word , ratio in words . items ( ) : dist [ word ] = probs [ prefix_id ] . item ( ) * ratio return dist