class_docstr: DGL Knowledge Embedding Model.
  
  Parameters
  ----------
  args:
      Global configs.
  model_name : str
      Which KG model to use, including 'TransE_l1', 'TransE_l2', 'TransR',
      'RESCAL', 'DistMult', 'ComplEx', 'RotatE', 'SimplE'
  n_entities : int
      Num of entities.
  n_relations : int
      Num of relations.
  hidden_dim : int
      Dimension size of embedding.
  gamma : float
      Gamma for score function.
  double_entity_emb : bool
      If True, entity embedding size will be 2 * hidden_dim.
      Default: False
  double_relation_emb : bool
      If True, relation embedding size will be 2 * hidden_dim.
      Default: False
docstr: Calculate the negative score.
  
  Parameters
  ----------
  pos_g : DGLGraph
      Graph holding positive edges.
  neg_g : DGLGraph
      Graph holding negative edges.
  to_device : func
      Function to move data into device.
  gpu_id : int
      Which gpu to move data to.
  trace : bool
      If True, trace the computation. This is required in training.
      If False, do not trace the computation.
      Default: False
  neg_deg_sample : bool
      If True, we use the head and tail nodes of the positive edges to
      construct negative edges.
      Default: False
  
  Returns
  -------
  tensor
      The negative score
docstr_tok: ["Calculate","the","negative","score",".","Parameters","--","--","--","--","--","pos_g",":","DGLGraph","Graph","holding","positive","edges",".","neg_g",":","DGLGraph","Graph","holding","negative","edges",".","to_device",":","func","Function","to","move","data","into","device",".","gpu_id",":","int","Which","gpu","to","move","data","to",".","trace",":","bool","If","True",",","trace","the","computation",".","This","is","required","in","training",".","If","False",",","do","not","trace","the","computation",".","Default",":","False","neg_deg_sample",":","bool","If","True",",","we","use","the","head","and","tail","nodes","of","the","positive","edges","to","construct","negative","edges",".","Default",":","False","Returns","--","--","--","-","tensor","The","negative","score"]
code: def predict_neg_score(
      self,
      pos_g,
      neg_g,
      to_device=None,
      gpu_id=-1,
      trace=False,
      neg_deg_sample=False,
  ):
      """Calculate the negative score.
  
      Parameters
      ----------
      pos_g : DGLGraph
          Graph holding positive edges.
      neg_g : DGLGraph
          Graph holding negative edges.
      to_device : func
          Function to move data into device.
      gpu_id : int
          Which gpu to move data to.
      trace : bool
          If True, trace the computation. This is required in training.
          If False, do not trace the computation.
          Default: False
      neg_deg_sample : bool
          If True, we use the head and tail nodes of the positive edges to
          construct negative edges.
          Default: False
  
      Returns
      -------
      tensor
          The negative score
      """
      num_chunks = neg_g.num_chunks
      chunk_size = neg_g.chunk_size
      neg_sample_size = neg_g.neg_sample_size
      mask = F.ones(
          (num_chunks, chunk_size * (neg_sample_size + chunk_size)),
          dtype=F.float32,
          ctx=F.context(pos_g.ndata["emb"]),
      )
      if neg_g.neg_head:
          neg_head_ids = neg_g.ndata["id"][neg_g.head_nid]
          if self.encoder_model_name == "roberta":
              neg_head = self.transform_net.embed_entity(
                  self.entity_feat(neg_head_ids, gpu_id, False)
              )
          elif self.encoder_model_name == "shallow":
              neg_head = self.entity_emb(neg_head_ids, gpu_id, trace)
          elif self.encoder_model_name == "concat":
              neg_head = self.transform_net.embed_entity(
                  torch.cat(
                      [
                          self.entity_feat(neg_head_ids, gpu_id, False),
                          self.entity_emb(neg_head_ids, gpu_id, trace),
                      ],
                      -1,
                  )
              )
  
          head_ids, tail_ids = pos_g.all_edges(order="eid")
          if to_device is not None and gpu_id >= 0:
              tail_ids = to_device(tail_ids, gpu_id)
          tail = pos_g.ndata["emb"][tail_ids]
          rel = pos_g.edata["emb"]
  
          # When we train a batch, we could use the head nodes of the positive edges to
          # construct negative edges. We construct a negative edge between a positive head
          # node and every positive tail node.
          # When we construct negative edges like this, we know there is one positive
          # edge for a positive head node among the negative edges. We need to mask
          # them.
          if neg_deg_sample:
              head = pos_g.ndata["emb"][head_ids]
              head = head.reshape(num_chunks, chunk_size, -1)
              neg_head = neg_head.reshape(
                  num_chunks, neg_sample_size, -1
              )
              neg_head = F.cat([head, neg_head], 1)
              neg_sample_size = chunk_size + neg_sample_size
              mask[:, 0 :: (neg_sample_size + 1)] = 0
          neg_head = neg_head.reshape(num_chunks * neg_sample_size, -1)
          neg_head, tail = self.head_neg_prepare(
              pos_g.edata["id"],
              num_chunks,
              neg_head,
              tail,
              gpu_id,
              trace,
          )
          neg_score = self.head_neg_score(
              neg_head,
              rel,
              tail,
              num_chunks,
              chunk_size,
              neg_sample_size,
          )
      else:
          neg_tail_ids = neg_g.ndata["id"][neg_g.tail_nid]
          if self.encoder_model_name == "roberta":
              neg_tail = self.transform_net.embed_entity(
                  self.entity_feat(neg_tail_ids, gpu_id, False)
              )
          elif self.encoder_model_name == "shallow":
              neg_tail = self.entity_emb(neg_tail_ids, gpu_id, trace)
          elif self.encoder_model_name == "concat":
              neg_tail = self.transform_net.embed_entity(
                  torch.cat(
                      [
                          self.entity_feat(neg_tail_ids, gpu_id, False),
                          self.entity_emb(neg_tail_ids, gpu_id, trace),
                      ],
                      -1,
                  )
              )
  
          head_ids, tail_ids = pos_g.all_edges(order="eid")
          if to_device is not None and gpu_id >= 0:
              head_ids = to_device(head_ids, gpu_id)
          head = pos_g.ndata["emb"][head_ids]
          rel = pos_g.edata["emb"]
  
          # This is negative edge construction similar to the above.
          if neg_deg_sample:
              tail = pos_g.ndata["emb"][tail_ids]
              tail = tail.reshape(num_chunks, chunk_size, -1)
              neg_tail = neg_tail.reshape(
                  num_chunks, neg_sample_size, -1
              )
              neg_tail = F.cat([tail, neg_tail], 1)
              neg_sample_size = chunk_size + neg_sample_size
              mask[:, 0 :: (neg_sample_size + 1)] = 0
          neg_tail = neg_tail.reshape(num_chunks * neg_sample_size, -1)
          head, neg_tail = self.tail_neg_prepare(
              pos_g.edata["id"],
              num_chunks,
              head,
              neg_tail,
              gpu_id,
              trace,
          )
          neg_score = self.tail_neg_score(
              head,
              rel,
              neg_tail,
              num_chunks,
              chunk_size,
              neg_sample_size,
          )
  
      if neg_deg_sample:
          neg_g.neg_sample_size = neg_sample_size
          mask = mask.reshape(num_chunks, chunk_size, neg_sample_size)
          return neg_score * mask
      else:
          return neg_score
  
imports: import os
  import numpy
  import math
  import dgl.backend
  import pdb
  import torch
  import torch.nn
  from .pytorch.tensor_models import logsigmoid
  from .pytorch.tensor_models import abs
  from .pytorch.tensor_models import masked_select
  from .pytorch.tensor_models import get_device, get_dev
  from .pytorch.tensor_models import norm
  from .pytorch.tensor_models import get_scalar
  from .pytorch.tensor_models import reshape
  from .pytorch.tensor_models import cuda
  from .pytorch.tensor_models import ExternalEmbedding
  from .pytorch.tensor_models import InferEmbedding
  from .pytorch.score_fun import *
  from .pytorch.loss import LossGenerator
label_desc: Use image node_id 7 for calling the KEModel obj's underlying member method code with example usage: obj.predict_neg_score(pos_g, neg_g, to_device, gpu_id, trace, neg_deg_sample) and returns: unknown, neg_score