class_docstr: KGIN is a knowledge-aware recommendation model. It combines knowledge graph and the user-item interaction
  graph to a new graph called collaborative knowledge graph (CKG). This model explores intents behind a user-item
  interaction by using auxiliary item knowledge.
class_docstr_tok: ["KGIN","is","a","knowledge-aware","recommendation","model",".","It","combines","knowledge","graph","and","the","user-item","interaction","graph","to","a","new","graph","called","collaborative","knowledge","graph","(","CKG",")",".","This","model","explores","intents","behind","a","user-item","interaction","by","using","auxiliary","item","knowledge","."]
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_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
  
      user = interaction[self.USER_ID]
      pos_item = interaction[self.ITEM_ID]
      neg_item = interaction[self.NEG_ITEM_ID]
  
      (
          user_all_embeddings,
          entity_all_embeddings,
          cor_loss,
      ) = self.forward()
      u_embeddings = user_all_embeddings[user]
      pos_embeddings = entity_all_embeddings[pos_item]
      neg_embeddings = entity_all_embeddings[neg_item]
  
      pos_scores = torch.mul(u_embeddings, pos_embeddings).sum(dim=1)
      neg_scores = torch.mul(u_embeddings, neg_embeddings).sum(dim=1)
      mf_loss = self.mf_loss(pos_scores, neg_scores)
      reg_loss = self.reg_loss(
          u_embeddings, pos_embeddings, neg_embeddings
      )
      cor_loss = self.sim_decay * cor_loss
      loss = mf_loss + self.reg_weight * reg_loss + cor_loss
      return loss
  
imports: import numpy
  import torch
  import torch.nn
  import torch.nn.functional
  import scipy.sparse
  from recbole.model.abstract_recommender import KnowledgeRecommender
  from recbole.model.init import xavier_uniform_initialization
  from recbole.model.layers import SparseDropout
  from recbole.model.loss import BPRLoss, EmbLoss
  from recbole.utils import InputType
label_desc: Use image node_id 5 for calling the KGIN obj's underlying member method code with example usage: obj.calculate_loss(interaction) and returns: loss