1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.
  2. Anuncie Aqui
    Anuncie aqui você Também: fdantas@4each.com.br

[Python] Keras: class weights (class_weight) for one-hot encoding

Discussão em 'Python' iniciado por Stack, Setembro 17, 2025.

  1. Stack

    Stack Membro Participativo

    I'd like to use class_weight argument in keras model.fit to handle the imbalanced training data. By looking at some documents, I understood we can pass a dictionary like this:

    class_weight = {0 : 1,
    1: 1,
    2: 5}


    (In this example, class-2 will get higher penalty in the loss function.)

    The problem is that my network's output has one-hot encoding i.e. class-0 = (1, 0, 0), class-1 = (0, 1, 0), and class-3 = (0, 0, 1).

    How can we use the class_weight for one-hot encoded output?

    By looking at some codes in Keras, it looks like _feed_output_names contain a list of output classes, but in my case, model.output_names/model._feed_output_names returns ['dense_1']

    Related: How to set class weights for imbalanced classes in Keras?

    Continue reading...

Compartilhe esta Página