How to weight classes in custom categorical crossentropy loss function?

Viewed 100

I have created weights for each of the classes so as to weight my loss function for each class. My loss function of choice is a custom categorical cross entropy. I have implemented in Tensorflow:

def class_weighted_categorical_loss(weights):
    def loss(y_true, y_pred):
        y_pred = tf.math.log(y_pred)
        suppresed_logs = tf.math.multiply(y_pred, y_true)
        loss =  -1*tf.math.multiply(suppresed_logs, weights)
        sum_loss = tf.math.reduce_sum(loss)
    
    return loss

The weights vector is of a shape (26,).

Here is the final part of my traceback:

C:\Users\kenne\miniconda3\envs\hoMachineLearning\lib\site-packages\tensorflow\python\framework\tensor_util.py:444 make_tensor_proto raise ValueError("None values not supported.")

ValueError: None values not supported.

The loss has a shape (None, 47, 26), and when I reduce it the sum_loss goes to a shape (). So it would be great if someone could come up with a class weighted categorical loss function.

0 Answers
Related