code: def calculate_loss(self, interaction):
      masked_item_seq = interaction[self.MASK_ITEM_SEQ]
      pos_items = interaction[self.POS_ITEMS]
      neg_items = interaction[self.NEG_ITEMS]
      masked_index = interaction[self.MASK_INDEX]
  
      seq_output = self.forward(masked_item_seq)
      pred_index_map = self.multi_hot_embed(
          masked_index, masked_item_seq.size(-1)
      )  # [B*mask_len max_len]
      # [B mask_len] -> [B mask_len max_len] multi hot
      pred_index_map = pred_index_map.view(
          masked_index.size(0), masked_index.size(1), -1
      )  # [B mask_len max_len]
      # [B mask_len max_len] * [B max_len H] -> [B mask_len H]
      # only calculate loss for masked position
      seq_output = torch.bmm(
          pred_index_map, seq_output
      )  # [B mask_len H]
  
      if self.loss_type == "BPR":
          pos_items_emb = self.item_embedding(
              pos_items
          )  # [B mask_len H]
          neg_items_emb = self.item_embedding(
              neg_items
          )  # [B mask_len H]
          pos_score = (
              torch.sum(seq_output * pos_items_emb, dim=-1)
              + self.output_bias[pos_items]
          )  # [B mask_len]
          neg_score = (
              torch.sum(seq_output * neg_items_emb, dim=-1)
              + self.output_bias[neg_items]
          )  # [B mask_len]
          targets = (masked_index > 0).float()
          loss = -torch.sum(
              torch.log(1e-14 + torch.sigmoid(pos_score - neg_score))
              * targets
          ) / torch.sum(targets)
          return loss
  
      elif self.loss_type == "CE":
          loss_fct = nn.CrossEntropyLoss(reduction="none")
          test_item_emb = self.item_embedding.weight[
              : self.n_items
          ]  # [item_num H]
          logits = (
              torch.matmul(seq_output, test_item_emb.transpose(0, 1))
              + self.output_bias
          )  # [B mask_len item_num]
          targets = (masked_index > 0).float().view(-1)  # [B*mask_len]
  
          loss = torch.sum(
              loss_fct(
                  logits.view(-1, test_item_emb.size(0)),
                  pos_items.view(-1),
              )
              * targets
          ) / torch.sum(targets)
          return loss
      else:
          raise NotImplementedError(
              "Make sure 'loss_type' in ['BPR', 'CE']!"
          )
  
file_path: RecBole/recbole/model/sequential_recommender/bert4rec.py
imports: import random
  import torch
  from torch import nn
  from recbole.model.abstract_recommender import SequentialRecommender
  from recbole.model.layers import TransformerEncoder
label_desc: Use image node_id 6 for calling the BERT4Rec obj's underlying member method code with example usage: obj.calculate_loss(interaction) and returns: loss, loss