Hi, thanks for the reply.
I assume your provided code is for the case of balanced classes. How would it work if we have weights for the different classes. Is the following code correct?
def custom_categorical_cross_entropy(y_pred, y_true, weights):

    weights = weights.view(1,-1)
        
    y_true = F.one_hot(y_true, num_classes=weights.shape[-1])    
    y_pred = torch.clamp(y_pred, 1e-9, 1 - 1e-9)
    
    #nominator = (-y_true * torch.log(y_pred) * weights).sum()
    nominator = (-y_true * torch.log(y_pred)).sum()
    denominator = (y_true * weights).sum()
    
    return nominator / denominator

I am not sure of is whether I should multiply the loss with the class weights?
I am also wondering how this weight is taken into account in Keras, since for my reference model it is passed as argument in model.fit, while I check in keras source code, the weights are not taken into account there. Any idea?
model.compile(optimizer='Nadam',  loss='categorical_crossentropy', metrics=['accuracy'], sample_weight_mode=None)

model.fit_generator(generator_train, steps_per_epoch = steps_per_epoch, class_weight = weight, epochs = 1, verbose=1,  callbacks=[history], initial_epoch=0 )