class_docstr: Class for ranking of paths in subgraph
class_docstr_tok: ["Class","for","ranking","of","paths","in","subgraph"]
code: def rank_rels(
      self, question: str, candidate_rels: List[str]
  ) -> List[Tuple[str, Any]]:
      rels_with_scores = []
      if question is not None:
          questions, rels_labels, rels = [], [], []
          for candidate_rel in candidate_rels:
              if candidate_rel in self.rel_q2name:
                  cur_rels_labels = self.rel_q2name[candidate_rel]
                  if isinstance(cur_rels_labels, str):
                      cur_rels_labels = [cur_rels_labels]
                  for cur_rel in cur_rels_labels:
                      questions.append(question)
                      rels.append(candidate_rel)
                      rels_labels.append(cur_rel)
          if questions:
              n_batches = len(rels) // self.batch_size + int(
                  len(rels) % self.batch_size > 0
              )
              for i in range(n_batches):
                  if self.nll_rel_ranking:
                      probas = self.ranker(
                          [questions[0]],
                          [
                              rels_labels[
                                  i
                                  * self.batch_size : (i + 1)
                                  * self.batch_size
                              ]
                          ],
                      )
                      probas = probas[0]
                  else:
                      probas = self.ranker(
                          questions[
                              i
                              * self.batch_size : (i + 1)
                              * self.batch_size
                          ],
                          rels_labels[
                              i
                              * self.batch_size : (i + 1)
                              * self.batch_size
                          ],
                      )
                      probas = [
                          proba[self.pos_class_num] for proba in probas
                      ]
                  for j, rel in enumerate(
                      rels[
                          i
                          * self.batch_size : (i + 1)
                          * self.batch_size
                      ]
                  ):
                      rels_with_scores.append((rel, probas[j]))
          if self.softmax:
              scores = [score for rel, score in rels_with_scores]
              softmax_scores = softmax(scores)
              rels_with_scores = [
                  (rel, softmax_score)
                  for (rel, score), softmax_score in zip(
                      rels_with_scores, softmax_scores
                  )
              ]
          rels_with_scores_dict = {}
          for rel, score in rels_with_scores:
              if rel not in rels_with_scores_dict:
                  rels_with_scores_dict[rel] = []
              rels_with_scores_dict[rel].append(score)
          rels_with_scores = [
              (rel, max(scores))
              for rel, scores in rels_with_scores_dict.items()
          ]
          rels_with_scores = sorted(
              rels_with_scores, key=lambda x: x[1], reverse=True
          )
      return rels_with_scores
  
file_path: DeepPavlov/deeppavlov/models/kbqa/rel_ranking_infer.py
imports: from logging import getLogger
  from typing import Tuple, List, Any, Optional
  from scipy.special import softmax
  from deeppavlov.core.common.chainer import Chainer
  from deeppavlov.core.common.file import load_pickle, read_json
  from deeppavlov.core.common.registry import register
  from deeppavlov.core.models.component import Component
  from deeppavlov.core.models.serializable import Serializable
  from deeppavlov.models.kbqa.sentence_answer import sentence_answer
  from deeppavlov.models.kbqa.wiki_parser import WikiParser
label_desc: Use image node_id 6 for calling the RelRankerInfer obj's underlying member method code with example usage: obj.rank_rels(question, candidate_rels) and returns: rels_with_scores