class_docstr: KGAT is a knowledge-based recommendation model. It combines knowledge graph and the user-item interaction
  graph to a new graph called collaborative knowledge graph (CKG). This model learns the representations of users and
  items by exploiting the structure of CKG. It adopts a GNN-based architecture and define the attention on the CKG.
class_docstr_tok: ["KGAT","is","a","knowledge-based","recommendation","model",".","It","combines","knowledge","graph","and","the","user-item","interaction","graph","to","a","new","graph","called","collaborative","knowledge","graph","(","CKG",")",".","This","model","learns","the","representations","of","users","and","items","by","exploiting","the","structure","of","CKG",".","It","adopts","a","GNN-based","architecture","and","define","the","attention","on","the","CKG","."]
docstr: Calculate the training loss for a batch data of KG.
  
  Args:
      interaction (Interaction): Interaction class of the batch.
  
  Returns:
      torch.Tensor: Training loss, shape: []
docstr_tok: ["Calculate","the","training","loss","for","a","batch","data","of","KG",".","Args",":","interaction","(","Interaction",")",":","Interaction","class","of","the","batch",".","Returns",":","torch.Tensor",":","Training","loss",",","shape",":","[","]"]
code: def calculate_kg_loss(self, interaction):
      r"""Calculate the training loss for a batch data of KG.
  
      Args:
          interaction (Interaction): Interaction class of the batch.
  
      Returns:
          torch.Tensor: Training loss, shape: []
      """
  
      if (
          self.restore_user_e is not None
          or self.restore_entity_e is not None
      ):
          self.restore_user_e, self.restore_entity_e = None, None
  
      # get loss for training kg
      h = interaction[self.HEAD_ENTITY_ID]
      r = interaction[self.RELATION_ID]
      pos_t = interaction[self.TAIL_ENTITY_ID]
      neg_t = interaction[self.NEG_TAIL_ENTITY_ID]
  
      h_e, r_e, pos_t_e, neg_t_e = self._get_kg_embedding(
          h, r, pos_t, neg_t
      )
      pos_tail_score = ((h_e + r_e - pos_t_e) ** 2).sum(dim=1)
      neg_tail_score = ((h_e + r_e - neg_t_e) ** 2).sum(dim=1)
      kg_loss = F.softplus(pos_tail_score - neg_tail_score).mean()
      kg_reg_loss = self.reg_loss(h_e, r_e, pos_t_e, neg_t_e)
      loss = kg_loss + self.reg_weight * kg_reg_loss
  
      return loss
  
imports: import numpy
  import scipy.sparse
  import torch
  import torch.nn
  import torch.nn.functional
  from recbole.model.abstract_recommender import KnowledgeRecommender
  from recbole.model.init import xavier_normal_initialization
  from recbole.model.loss import BPRLoss, EmbLoss
  from recbole.utils import InputType
label_desc: Use image node_id 7 for calling the KGAT obj's underlying member method code with example usage: obj.calculate_kg_loss(interaction) and returns: loss