code: def triplet_prediction(args):
      import torch
      from .network import LinkPredictor
  
      torch.set_grad_enabled(False)
  
      (
          entity_embeddings,
          relation_embeddings,
          H,
          R,
          T,
          exclude_H,
          exclude_T,
          target,
          k,
          score_function,
          margin,
          device,
      ) = args
      num_entity = len(entity_embeddings)
      score_function = LinkPredictor(
          score_function,
          entity_embeddings,
          relation_embeddings,
          entity_embeddings,
          margin=margin,
      )
  
      if device != "cpu":
          try:
              score_function = score_function.to(device)
          except RuntimeError:
              logger.info(
                  "Model is too large for GPU evaluation with PyTorch. Switch to CPU evaluation."
              )
              device = "cpu"
          if device == "cpu":
              del score_function
              torch.cuda.empty_cache()
              score_function = LinkPredictor(
                  score_function,
                  entity_embeddings,
                  relation_embeddings,
                  entity_embeddings,
                  margin=margin,
              )
  
      one = torch.ones(num_entity, dtype=torch.long, device=device)
      all = torch.arange(num_entity, dtype=torch.long, device=device)
      results = []  # rankings or top-k recalls
  
      for h, r, t in zip(H, R, T):
          if target == "head" or target == "both":
              batch_h = all
              batch_r = r * one
              batch_t = t * one
              score = score_function(batch_h, batch_r, batch_t)
              if k:  # top-k recalls
                  score, index = torch.topk(score, k)
                  score = score.cpu().numpy()
                  index = index.cpu().numpy()
                  recall = list(zip(index, score))
                  results.append(recall)
              else:  # ranking
                  mask = torch.ones(
                      num_entity, dtype=torch.uint8, device=device
                  )
                  index = torch.tensor(
                      list(exclude_H[(t, r)]),
                      dtype=torch.long,
                      device=device,
                  )
                  mask[index] = 0
                  mask[h] = 1
                  ranking = torch.sum((score >= score[h]) * mask).item()
                  results.append(ranking)
  
          if target == "tail" or target == "both":
              batch_h = h * one
              batch_r = r * one
              batch_t = all
              score = score_function(batch_h, batch_r, batch_t)
              if k:  # top-k recalls
                  score, index = torch.topk(score, k)
                  score = score.cpu().numpy()
                  index = index.cpu().numpy()
                  recall = list(zip(index, score))
                  results.append(recall)
              else:  # ranking
                  mask = torch.ones(
                      num_entity, dtype=torch.uint8, device=device
                  )
                  index = torch.tensor(
                      list(exclude_T[(h, r)]),
                      dtype=torch.long,
                      device=device,
                  )
                  mask[index] = 0
                  mask[t] = 1
                  ranking = torch.sum((score >= score[t]) * mask).item()
                  results.append(ranking)
  
      if not k:  # ranking
          results = np.asarray(results)
      return results
  
imports: from __future__ import print_function, absolute_import, unicode_literals, division
  import os
  import re
  import pickle
  import logging
  import multiprocessing
  from collections import defaultdict
  from future.builtins import str, map, range
  from easydict import EasyDict
  import numpy
  from ..None import lib, cfg, auto
  from ..None import graph, solver
  from ..util import assert_in, monitor, SharedNDArray
label_desc: Use image node_id 2 for calling a global function with example usage: triplet_prediction(args) and returns: results