class_docstr: Abstract loss function for KG embeddings.
docstr: Computes loss with or without negative sampling.
  
  Args:
    model: tf.keras.Model KG embedding model.
    input_batch: Tensor of size batch_size x 3 containing input triples.
  
  Returns:
    Average loss within the input_batch.
docstr_tok: ["Computes","loss","with","or","without","negative","sampling",".","Args",":","model",":","tf.keras.Model","KG","embedding","model",".","input_batch",":","Tensor","of","size","batch_size","x","3","containing","input","triples",".","Returns",":","Average","loss","within","the","input_batch","."]
code: def calculate_loss(self, model, input_batch):
      """Computes loss with or without negative sampling.
  
      Args:
        model: tf.keras.Model KG embedding model.
        input_batch: Tensor of size batch_size x 3 containing input triples.
  
      Returns:
        Average loss within the input_batch.
      """
      labels = input_batch[:, 2]
      logits = model(input_batch, eval_mode=True)
      full_labels = tf.one_hot(
          labels, depth=self.n_entities, dtype=logits.dtype
      )
      if self.use_neg_sampling:
          # mask some values for negative sampling
          neg_sample_mask = self.get_neg_sample_mask(
              logits, full_labels
          )
          # mask logits to only keep target and negative examples' scores
          logits = logits + neg_sample_mask
      return self.loss_from_logits(logits, full_labels, labels)
  
file_path: neural-structured-learning/research/kg_hyp_emb/learning/losses.py
imports: import abc
  import tensorflow
label_desc: Use image node_id 4 for calling the LossFn obj's underlying member method code with example usage: obj.calculate_loss(model, input_batch) and returns: self