class_docstr: This code was developed by jbpatel@mit.edu.
      
class_docstr_tok: ["This","code","was","developed","by","jbpatel","@","mit.edu","."]
code: def w_categorical_crossentropy_function(self, y_true, y_pred):
      from keras import backend as K
      from keras.layers import concatenate, Lambda
  
      nb_cl = len(self.weights)
      final_mask = K.zeros_like(y_pred[..., 0])
      y_pred_max = K.max(y_pred, axis=-1)
      y_pred_max = K.expand_dims(y_pred_max, axis=-1)
      y_pred_max_mat = K.equal(y_pred, y_pred_max)
  
      # change y_true into one-hot encoding
      y_true_complement = Lambda(lambda arg: (K.ones_like(arg) - arg))(
          y_true
      )
      y_true_onehot = concatenate([y_true_complement, y_true], axis=-1)
  
      for c_p, c_t in itertools.product(range(nb_cl), range(nb_cl)):
          w = K.cast(self.weights[c_t, c_p], K.floatx())
          y_p = K.cast(y_pred_max_mat[..., c_p], K.floatx())
          y_t = K.cast(y_true_onehot[..., c_t], K.floatx())
          final_mask += w * y_p * y_t
  
      loss = (
          K.categorical_crossentropy(y_true_onehot, y_pred) * final_mask
      )
      loss_flat = K.flatten(loss)
      self.metric = K.sum(loss_flat)
      return loss
  
imports: import numpy
  import itertools
label_desc: Use image node_id 5 for calling the WeightedCategoricalCrossEntropy obj's underlying member method code with example usage: obj.w_categorical_crossentropy_function(y_true, y_pred) and returns: loss