Exemplo n.º 1
0
 def predictKTacticsWithLoss(
         self, in_data: TacticContext, k: int,
         correct: str) -> Tuple[List[Prediction], float]:
     with self._lock:
         distribution, hyp_var = self._predictDistribution(in_data)
         correct_stem = serapi_instance.get_stem(correct)
         if self._embedding.has_token(correct_stem):
             loss = self._criterion(
                 distribution.view(1, -1),
                 Variable(
                     LongTensor([
                         self._embedding.encode_token(correct_stem)
                     ]))).item()
         else:
             loss = float("+inf")
     indices, probabilities = list_topk(list(distribution), k)
     predictions: List[Prediction] = []
     for certainty, idx in zip(probabilities, indices):
         stem = self._embedding.decode_token(idx)
         if serapi_instance.tacticTakesHypArgs(stem):
             predictions.append(
                 Prediction(stem + " " + hyp_var + ".",
                            math.exp(certainty)))
         else:
             predictions.append(Prediction(stem + ".", math.exp(certainty)))
     return predictions, loss
Exemplo n.º 2
0
 def predictKTacticsWithLoss(self, in_data : TacticContext, k : int, correct : str) -> \
     Tuple[List[Prediction], float]:
     assert self.training_args
     assert self._embedding
     with self._lock:
         prediction_distribution = self._predictDistributions([in_data])[0]
     if k > self._embedding.num_tokens():
         k = self._embedding.num_tokens()
     correct_stem = serapi_instance.get_stem(correct)
     if self._embedding.has_token(correct_stem):
         output_var = maybe_cuda(
             Variable(
                 LongTensor([self._embedding.encode_token(correct_stem)])))
         loss = self._criterion(prediction_distribution.view(1, -1),
                                output_var).item()
     else:
         loss = 0
     if len(in_data.hypotheses) == 0:
         certainties, idxs = topk_with_filter(
             prediction_distribution.view(-1), k,
             lambda certainty, idx: not serapi_instance.tacticTakesHypArgs(
                 cast(Embedding, self._embedding).decode_token(idx)))
     else:
         certainties, idxs = prediction_distribution.view(-1).topk(k)
     results = [
         Prediction(
             self.add_arg(self._embedding.decode_token(stem_idx.item()),
                          in_data.goal, in_data.hypotheses,
                          self.training_args.max_length),
             math.exp(certainty.item()))
         for certainty, stem_idx in zip(certainties, idxs)
     ]
     return results, loss
Exemplo n.º 3
0
 def add_arg(self, tactic_stem: str, goal: str, hyps: List[str],
             max_length: int):
     if serapi_instance.tacticTakesHypArgs(tactic_stem):
         return tactic_stem + " " + \
             serapi_instance.get_first_var_in_hyp(get_closest_hyp(hyps, goal,
                                                                  max_length)) \
             + "."
     else:
         return tactic_stem + "."
Exemplo n.º 4
0
def args_vars_in_list(tactic : str,
                      context_list : List[str]) -> bool:
    stem, args_string  = serapi_instance.split_tactic(tactic)
    args = args_string[:-1].split()
    if not serapi_instance.tacticTakesHypArgs(stem) and len(args) > 0:
        return False
    var_names = serapi_instance.get_vars_in_hyps(context_list)
    for arg in args:
        if not arg in var_names:
            return False
    return True
Exemplo n.º 5
0
def args_vars_in_context(in_data: ContextData, tactic: str,
                         next_in_data: ContextData,
                         arg_values: argparse.Namespace) -> bool:
    stem, args_string = serapi_instance.split_tactic(tactic)
    args = args_string[:-1].split()
    if not serapi_instance.tacticTakesHypArgs(stem) and len(args) > 0:
        return False
    var_names = serapi_instance.get_vars_in_hyps(
        cast(List[str], in_data["hyps"]))
    for arg in args:
        if not arg in var_names:
            return False
    return True
Exemplo n.º 6
0
 def add_args(self, stem_predictions: List[Prediction], goal: str,
              hyps: List[str], max_length: int):
     possibilities: List[Prediction] = []
     for stem, stem_score in stem_predictions:
         if serapi_instance.tacticTakesHypArgs(stem):
             for hyp, hyp_score in get_closest_hyps(hyps, goal,
                                                    len(stem_predictions),
                                                    max_length):
                 possibilities.append(
                     Prediction(
                         stem + " " +
                         serapi_instance.get_first_var_in_hyp(hyp) + ".",
                         hyp_score * stem_score))
         else:
             possibilities.append(Prediction(stem + ".", stem_score * 0.5))
     return list(
         sorted(possibilities,
                key=lambda pred: pred.certainty,
                reverse=True)[:len(stem_predictions)])
Exemplo n.º 7
0
 def predictKTactics(self, in_data : TacticContext, k : int) \
     -> List[Prediction]:
     assert self.training_args
     assert self._embedding
     with self._lock:
         prediction_distribution = self._predictDistributions([in_data])[0]
     if k > self._embedding.num_tokens():
         k = self._embedding.num_tokens()
     if len(in_data.hypotheses) == 0:
         certainties, idxs = topk_with_filter(
             prediction_distribution.view(-1), k,
             lambda certainty, idx: not serapi_instance.tacticTakesHypArgs(
                 cast(Embedding, self._embedding).decode_token(idx)))
     else:
         certainties, idxs = prediction_distribution.view(-1).topk(k)
     results = self.add_args([
         Prediction(self._embedding.decode_token(stem_idx.item()),
                    math.exp(certainty.item()))
         for certainty, stem_idx in zip(certainties, idxs)
     ], in_data.goal, in_data.hypotheses, self.training_args.max_length)
     return results
Exemplo n.º 8
0
 def predictKTacticsWithLoss_batch(self,
                                   in_data : List[TacticContext],
                                   k : int, corrects : List[str]) -> \
                                   Tuple[List[List[Prediction]], float]:
     assert self._embedding
     assert self.training_args
     with self._lock:
         prediction_distributions = self._predictDistributions(in_data)
     correct_stems = [
         serapi_instance.get_stem(correct) for correct in corrects
     ]
     output_var = maybe_cuda(
         Variable(
             LongTensor([
                 self._embedding.encode_token(correct_stem)
                 if self._embedding.has_token(correct_stem) else 0
                 for correct_stem in correct_stems
             ])))
     loss = self._criterion(prediction_distributions, output_var).item()
     if k > self._embedding.num_tokens():
         k = self._embedding.num_tokens()
     certainties_and_idxs_list = \
         [single_distribution.view(-1).topk(k) if len(context.hypotheses) > 0 else
          topk_with_filter(single_distribution.view(-1), k,
                           lambda certainty, idx:
                           not serapi_instance.tacticTakesHypArgs(
                               cast(Embedding, self._embedding).decode_token(idx)))
          for single_distribution, context in
          zip(prediction_distributions, in_data)]
     results = [[
         Prediction(
             self.add_arg(self._embedding.decode_token(stem_idx.item()),
                          in_datum.goal, in_datum.hypotheses,
                          self.training_args.max_length),
             math.exp(certainty.item()))
         for certainty, stem_idx in zip(*certainties_and_idxs)
     ]
                for certainties_and_idxs, in_datum in zip(
                    certainties_and_idxs_list, in_data)]
     return results, loss