class_docstr: Bag-level relation extraction dataset. Note that relation of NA should be named as 'NA'.
docstr: Args:
      path: path of the input file
      rel2id: dictionary of relation->id mapping
      tokenizer: function of tokenizing
      entpair_as_bag: if True, bags are constructed based on same
          entity pairs instead of same relation facts (ignoring 
          relation labels)
docstr_tok: ["Args",":","path",":","path","of","the","input","file","rel2id",":","dictionary","of","relation-",">","id","mapping","tokenizer",":","function","of","tokenizing","entpair_as_bag",":","if","True",",","bags","are","constructed","based","on","same","entity","pairs","instead","of","same","relation","facts","(","ignoring","relation","labels",")"]
code: def __init__(
      self,
      path,
      rel2id,
      tokenizer,
      entpair_as_bag=False,
      bag_size=0,
      mode=None,
  ):
      """
      Args:
          path: path of the input file
          rel2id: dictionary of relation->id mapping
          tokenizer: function of tokenizing
          entpair_as_bag: if True, bags are constructed based on same
              entity pairs instead of same relation facts (ignoring
              relation labels)
      """
      super().__init__()
      self.tokenizer = tokenizer
      self.rel2id = rel2id
      self.entpair_as_bag = entpair_as_bag
      self.bag_size = bag_size
  
      # Load the file
      f = open(path)
      self.data = []
      for line in f:
          line = line.rstrip()
          if len(line) > 0:
              self.data.append(eval(line))
      f.close()
  
      # Construct bag-level dataset (a bag contains instances sharing the same relation fact)
      if mode == None:
          self.weight = np.ones((len(self.rel2id)), dtype=np.float32)
          self.bag_scope = []
          self.name2id = {}
          self.bag_name = []
          self.facts = {}
          for idx, item in enumerate(self.data):
              # Annotated test set
              if "anno_relation_list" in item:
                  for r in item["anno_relation_list"]:
                      fact = (item["h"]["id"], item["t"]["id"], r)
                      if r != "NA":
                          self.facts[fact] = 1
                  assert entpair_as_bag
                  name = (item["h"]["id"], item["t"]["id"])
              else:
                  fact = (
                      item["h"]["id"],
                      item["t"]["id"],
                      item["relation"],
                  )
                  if item["relation"] != "NA":
                      self.facts[fact] = 1
                  if entpair_as_bag:
                      name = (item["h"]["id"], item["t"]["id"])
                  else:
                      name = fact
              if name not in self.name2id:
                  self.name2id[name] = len(self.name2id)
                  self.bag_scope.append([])
                  self.bag_name.append(name)
              self.bag_scope[self.name2id[name]].append(idx)
              self.weight[self.rel2id[item["relation"]]] += 1.0
          self.weight = 1.0 / (self.weight**0.05)
          self.weight = torch.from_numpy(self.weight)
      else:
          pass
  
imports: import torch
  import torch.utils.data
  import os, random, json, logging
  import numpy
  import sklearn.metrics
label_desc: Use image node_id 1 to create a new BagREDataset object from inherited base classes: data with example: obj = BagREDataset(path, rel2id, tokenizer, entpair_as_bag, bag_size, mode)