code: def get_hrt(
      self, sequence_output: Tensor, attention: Tensor, entity_pos: List
  ) -> Tuple[Tensor, Tensor, Tensor]:
      _, h, _, max_sequence_length = attention.size()
      hss, tss, rss = [], [], []
      for i in range(
          len(entity_pos)
      ):  # for each training sample (= doc)
          entity_embs, entity_atts = [], []
          for e in entity_pos[
              i
          ]:  # for each entity (= list of entity mentions)
              if len(e) == 0:
                  continue
              if len(e) > 1:
                  e_emb, e_att = [], []
                  for (
                      start,
                      end,
                  ) in e:  # for start and end position of each mention
                      # skip the entity pair if the entity mention is truncated due to limited max seq length.
                      if start + 1 < max_sequence_length:
                          e_emb.append(sequence_output[i, start + 1])
                          e_att.append(attention[i, :, start + 1])
                  if len(e_emb) > 0:
                      e_emb = torch.logsumexp(
                          torch.stack(e_emb, dim=0), dim=0
                      )
                      e_att = torch.stack(e_att, dim=0).mean(0)
                  else:
                      e_emb = torch.zeros(self.hidden_size).to(
                          sequence_output
                      )
                      e_att = torch.zeros(h, max_sequence_length).to(
                          attention
                      )
              else:
                  start, end = e[0]
                  if start + 1 < max_sequence_length:
                      e_emb = sequence_output[i, start + 1]
                      e_att = attention[i, :, start + 1]
                  else:
                      e_emb = torch.zeros(self.hidden_size).to(
                          sequence_output
                      )
                      e_att = torch.zeros(h, max_sequence_length).to(
                          attention
                      )
              entity_embs.append(e_emb)  # get an embedding of an entity
              entity_atts.append(e_att)  # get attention of an entity
  
          entity_embs = torch.stack(
              entity_embs, dim=0
          )  # [n_e, d]           # entity embeddings for each document
          entity_atts = torch.stack(
              entity_atts, dim=0
          )  # [n_e, h, seq_len]
  
          hs = torch.index_select(
              entity_embs, 0, torch.tensor([0]).to(self.device)
          )  # embeddings of the first entity
          ts = torch.index_select(
              entity_embs, 0, torch.tensor([1]).to(self.device)
          )  # embeddings of the second entity
  
          h_att = torch.index_select(
              entity_atts, 0, torch.tensor([0]).to(self.device)
          )
          t_att = torch.index_select(
              entity_atts, 0, torch.tensor([1]).to(self.device)
          )
          ht_att = (h_att * t_att).mean(1)
          ht_att = ht_att / (ht_att.sum(1, keepdim=True) + 1e-5)
          rs = contract(
              "ld,rl->rd", sequence_output[i], ht_att
          )  # ht_i.shape[0] x sequence_output.shape[2]
          hss.append(hs)
          tss.append(ts)
          rss.append(rs)
  
      hss = torch.cat(hss, dim=0)
      tss = torch.cat(tss, dim=0)
      rss = torch.cat(rss, dim=0)
  
      return hss, rss, tss
  
file_path: DeepPavlov/deeppavlov/models/classifiers/re_bert.py
imports: import logging
  from pathlib import Path
  from typing import Tuple, Union, Any, List
  import torch
  from torch import Tensor
  import torch.nn
  from opt_einsum import contract
  from transformers import AutoConfig, BertModel, BertTokenizer
  from deeppavlov.core.commands.utils import expand_path
  from deeppavlov.core.common.errors import ConfigError
  from deeppavlov.models.relation_extraction.losses import ATLoss
label_desc: Use image node_id 3 for calling the BertWithAdaThresholdLocContextPooling obj's underlying member method code with example usage: obj.get_hrt(sequence_output, attention, entity_pos) and returns: hss,  rss,  tss