class_docstr: Instance attention for bag-level relation extraction.
docstr: Args:
      bag: bag of sentences with the same entity pair
          [{
            'text' or 'token': ..., 
            'h': {'pos': [start, end], ...}, 
            't': {'pos': [start, end], ...}
          }]
  Return:
      (relation, score)
docstr_tok: ["Args",":","bag",":","bag","of","sentences","with","the","same","entity","pair","[","{","'text","'","or","'token","'",":","...",",","'","h","'",":","{","'pos","'",":","[","start",",","end","]",",","...","}",",","'t","'",":","{","'pos","'",":","[","start",",","end","]",",","...","}","}","]","Return",":","(","relation",",","score",")"]
code: def infer(self, bag):
      """
      Args:
          bag: bag of sentences with the same entity pair
              [{
                'text' or 'token': ...,
                'h': {'pos': [start, end], ...},
                't': {'pos': [start, end], ...}
              }]
      Return:
          (relation, score)
      """
      self.eval()
      tokens = []
      pos1s = []
      pos2s = []
      masks = []
      for item in bag:
          token, pos1, pos2, mask = self.sentence_encoder.tokenize(item)
          tokens.append(token)
          pos1s.append(pos1)
          pos2s.append(pos2)
          masks.append(mask)
      tokens = torch.cat(tokens, 0).unsqueeze(0)  # (n, L)
      pos1s = torch.cat(pos1s, 0).unsqueeze(0)
      pos2s = torch.cat(pos2s, 0).unsqueeze(0)
      masks = torch.cat(masks, 0).unsqueeze(0)
      scope = torch.tensor([[0, len(bag)]]).long()  # (1, 2)
      bag_logits = self.forward(
          None, scope, tokens, pos1s, pos2s, masks, train=False
      ).squeeze(
          0
      )  # (N) after softmax
      score, pred = bag_logits.max(0)
      score = score.item()
      pred = pred.item()
      rel = self.id2rel[pred]
      return (rel, score)
  
file_path: OpenNRE/opennre/model/bag_attention.py
imports: import torch
  from torch import nn, optim
  from .base_model import BagRE
label_desc: Use image node_id 2 for calling the BagAttention obj's underlying member method code with example usage: obj.infer(bag) and returns: rel,  score