How to apply class weight to a multi-output model?












1












$begingroup$


I have a model with 2 categorical outputs.

The first output layer can predict 2 classes: [0, 1]

and the second output layer can predict 3 classes: [0, 1, 2].



How can I apply different class weight dictionaries for each of the outputs?



For example, how could I apply the dictionary {0: 1, 1: 10} to the first output,

and {0: 5, 1: 1, 2: 10} to the second output?



I've tried to use the following class weights dictionary
weight_class={'output1': {0: 1, 1: 10}, 'output2': {0: 5, 1: 1, 2: 10}}

But the code fails with an error.



My script also runs normally when i remove the class_weight parameter



Code Example



I've created a minimal example that reproduces the error



from tensorflow.python.keras.models import Model
from tensorflow.python.keras.layers import Input, Dense
from tensorflow.python.data import Dataset
import tensorflow as tf
import numpy as np


def preprocess_sample(features, labels):
label1, label2 = labels
label1 = tf.one_hot(label1, 2)
label2 = tf.one_hot(label2, 3)
return features, (label1, label2)


batch_size = 32

num_samples = 1000
num_features = 10

features = np.random.rand(num_samples, num_features)
labels1 = np.random.randint(2, size=num_samples)
labels2 = np.random.randint(3, size=num_samples)

train = Dataset.from_tensor_slices((features, (labels1, labels2))).map(preprocess_sample).batch(batch_size).repeat()

# Model
inputs = Input(shape=(num_features, ))
output1 = Dense(2, activation='softmax', name='output1')(inputs)
output2 = Dense(3, activation='softmax', name='output2')(inputs)
model = Model(inputs, [output1, output2])

model.compile(loss='categorical_crossentropy', optimizer='adam')
class_weights = {'output1': {0: 1, 1: 10}, 'output2': {0: 5, 1: 1, 2: 10}}
model.fit(train, epochs=10, steps_per_epoch=num_samples // batch_size,
# class_weight=class_weights
)


This code runs successfully without the class_weight parameter.

But when you add the class_weight parameter by uncommenting the line
# class_weight=class_weights than the script fails with the following error:



Traceback (most recent call last):
File "test.py", line 35, in <module>
class_weight=class_weights
File "venv/lib/python3.6/site-packages/tensorflow/python/keras/engine/training.py", line 1536, in fit
validation_split=validation_split)
File "venv/lib/python3.6/site-packages/tensorflow/python/keras/engine/training.py", line 992, in _standardize_user_data
class_weight, batch_size)
File "venv/lib/python3.6/site-packages/tensorflow/python/keras/engine/training.py", line 1165, in _standardize_weights
feed_sample_weight_modes)
File "venv/lib/python3.6/site-packages/tensorflow/python/keras/engine/training.py", line 1164, in <listcomp>
for (ref, sw, cw, mode) in zip(y, sample_weights, class_weights,
File "venv/lib/python3.6/site-packages/tensorflow/python/keras/engine/training_utils.py", line 717, in standardize_weights
y_classes = np.argmax(y, axis=1)
File "venv/lib/python3.6/site-packages/numpy/core/fromnumeric.py", line 1004, in argmax
return _wrapfunc(a, 'argmax', axis=axis, out=out)
File "venv/lib/python3.6/site-packages/numpy/core/fromnumeric.py", line 62, in _wrapfunc
return _wrapit(obj, method, *args, **kwds)
File "venv/lib/python3.6/site-packages/numpy/core/fromnumeric.py", line 42, in _wrapit
result = getattr(asarray(obj), method)(*args, **kwds)
numpy.core._internal.AxisError: axis 1 is out of bounds for array of dimension 1


Edit



I've also opened an issue in the Keras github page, but i wanted to ask the same question here to see if perhaps i'm missing something and doing something wrong.










share|improve this question











$endgroup$




bumped to the homepage by Community 44 mins ago


This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.




















    1












    $begingroup$


    I have a model with 2 categorical outputs.

    The first output layer can predict 2 classes: [0, 1]

    and the second output layer can predict 3 classes: [0, 1, 2].



    How can I apply different class weight dictionaries for each of the outputs?



    For example, how could I apply the dictionary {0: 1, 1: 10} to the first output,

    and {0: 5, 1: 1, 2: 10} to the second output?



    I've tried to use the following class weights dictionary
    weight_class={'output1': {0: 1, 1: 10}, 'output2': {0: 5, 1: 1, 2: 10}}

    But the code fails with an error.



    My script also runs normally when i remove the class_weight parameter



    Code Example



    I've created a minimal example that reproduces the error



    from tensorflow.python.keras.models import Model
    from tensorflow.python.keras.layers import Input, Dense
    from tensorflow.python.data import Dataset
    import tensorflow as tf
    import numpy as np


    def preprocess_sample(features, labels):
    label1, label2 = labels
    label1 = tf.one_hot(label1, 2)
    label2 = tf.one_hot(label2, 3)
    return features, (label1, label2)


    batch_size = 32

    num_samples = 1000
    num_features = 10

    features = np.random.rand(num_samples, num_features)
    labels1 = np.random.randint(2, size=num_samples)
    labels2 = np.random.randint(3, size=num_samples)

    train = Dataset.from_tensor_slices((features, (labels1, labels2))).map(preprocess_sample).batch(batch_size).repeat()

    # Model
    inputs = Input(shape=(num_features, ))
    output1 = Dense(2, activation='softmax', name='output1')(inputs)
    output2 = Dense(3, activation='softmax', name='output2')(inputs)
    model = Model(inputs, [output1, output2])

    model.compile(loss='categorical_crossentropy', optimizer='adam')
    class_weights = {'output1': {0: 1, 1: 10}, 'output2': {0: 5, 1: 1, 2: 10}}
    model.fit(train, epochs=10, steps_per_epoch=num_samples // batch_size,
    # class_weight=class_weights
    )


    This code runs successfully without the class_weight parameter.

    But when you add the class_weight parameter by uncommenting the line
    # class_weight=class_weights than the script fails with the following error:



    Traceback (most recent call last):
    File "test.py", line 35, in <module>
    class_weight=class_weights
    File "venv/lib/python3.6/site-packages/tensorflow/python/keras/engine/training.py", line 1536, in fit
    validation_split=validation_split)
    File "venv/lib/python3.6/site-packages/tensorflow/python/keras/engine/training.py", line 992, in _standardize_user_data
    class_weight, batch_size)
    File "venv/lib/python3.6/site-packages/tensorflow/python/keras/engine/training.py", line 1165, in _standardize_weights
    feed_sample_weight_modes)
    File "venv/lib/python3.6/site-packages/tensorflow/python/keras/engine/training.py", line 1164, in <listcomp>
    for (ref, sw, cw, mode) in zip(y, sample_weights, class_weights,
    File "venv/lib/python3.6/site-packages/tensorflow/python/keras/engine/training_utils.py", line 717, in standardize_weights
    y_classes = np.argmax(y, axis=1)
    File "venv/lib/python3.6/site-packages/numpy/core/fromnumeric.py", line 1004, in argmax
    return _wrapfunc(a, 'argmax', axis=axis, out=out)
    File "venv/lib/python3.6/site-packages/numpy/core/fromnumeric.py", line 62, in _wrapfunc
    return _wrapit(obj, method, *args, **kwds)
    File "venv/lib/python3.6/site-packages/numpy/core/fromnumeric.py", line 42, in _wrapit
    result = getattr(asarray(obj), method)(*args, **kwds)
    numpy.core._internal.AxisError: axis 1 is out of bounds for array of dimension 1


    Edit



    I've also opened an issue in the Keras github page, but i wanted to ask the same question here to see if perhaps i'm missing something and doing something wrong.










    share|improve this question











    $endgroup$




    bumped to the homepage by Community 44 mins ago


    This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.


















      1












      1








      1


      0



      $begingroup$


      I have a model with 2 categorical outputs.

      The first output layer can predict 2 classes: [0, 1]

      and the second output layer can predict 3 classes: [0, 1, 2].



      How can I apply different class weight dictionaries for each of the outputs?



      For example, how could I apply the dictionary {0: 1, 1: 10} to the first output,

      and {0: 5, 1: 1, 2: 10} to the second output?



      I've tried to use the following class weights dictionary
      weight_class={'output1': {0: 1, 1: 10}, 'output2': {0: 5, 1: 1, 2: 10}}

      But the code fails with an error.



      My script also runs normally when i remove the class_weight parameter



      Code Example



      I've created a minimal example that reproduces the error



      from tensorflow.python.keras.models import Model
      from tensorflow.python.keras.layers import Input, Dense
      from tensorflow.python.data import Dataset
      import tensorflow as tf
      import numpy as np


      def preprocess_sample(features, labels):
      label1, label2 = labels
      label1 = tf.one_hot(label1, 2)
      label2 = tf.one_hot(label2, 3)
      return features, (label1, label2)


      batch_size = 32

      num_samples = 1000
      num_features = 10

      features = np.random.rand(num_samples, num_features)
      labels1 = np.random.randint(2, size=num_samples)
      labels2 = np.random.randint(3, size=num_samples)

      train = Dataset.from_tensor_slices((features, (labels1, labels2))).map(preprocess_sample).batch(batch_size).repeat()

      # Model
      inputs = Input(shape=(num_features, ))
      output1 = Dense(2, activation='softmax', name='output1')(inputs)
      output2 = Dense(3, activation='softmax', name='output2')(inputs)
      model = Model(inputs, [output1, output2])

      model.compile(loss='categorical_crossentropy', optimizer='adam')
      class_weights = {'output1': {0: 1, 1: 10}, 'output2': {0: 5, 1: 1, 2: 10}}
      model.fit(train, epochs=10, steps_per_epoch=num_samples // batch_size,
      # class_weight=class_weights
      )


      This code runs successfully without the class_weight parameter.

      But when you add the class_weight parameter by uncommenting the line
      # class_weight=class_weights than the script fails with the following error:



      Traceback (most recent call last):
      File "test.py", line 35, in <module>
      class_weight=class_weights
      File "venv/lib/python3.6/site-packages/tensorflow/python/keras/engine/training.py", line 1536, in fit
      validation_split=validation_split)
      File "venv/lib/python3.6/site-packages/tensorflow/python/keras/engine/training.py", line 992, in _standardize_user_data
      class_weight, batch_size)
      File "venv/lib/python3.6/site-packages/tensorflow/python/keras/engine/training.py", line 1165, in _standardize_weights
      feed_sample_weight_modes)
      File "venv/lib/python3.6/site-packages/tensorflow/python/keras/engine/training.py", line 1164, in <listcomp>
      for (ref, sw, cw, mode) in zip(y, sample_weights, class_weights,
      File "venv/lib/python3.6/site-packages/tensorflow/python/keras/engine/training_utils.py", line 717, in standardize_weights
      y_classes = np.argmax(y, axis=1)
      File "venv/lib/python3.6/site-packages/numpy/core/fromnumeric.py", line 1004, in argmax
      return _wrapfunc(a, 'argmax', axis=axis, out=out)
      File "venv/lib/python3.6/site-packages/numpy/core/fromnumeric.py", line 62, in _wrapfunc
      return _wrapit(obj, method, *args, **kwds)
      File "venv/lib/python3.6/site-packages/numpy/core/fromnumeric.py", line 42, in _wrapit
      result = getattr(asarray(obj), method)(*args, **kwds)
      numpy.core._internal.AxisError: axis 1 is out of bounds for array of dimension 1


      Edit



      I've also opened an issue in the Keras github page, but i wanted to ask the same question here to see if perhaps i'm missing something and doing something wrong.










      share|improve this question











      $endgroup$




      I have a model with 2 categorical outputs.

      The first output layer can predict 2 classes: [0, 1]

      and the second output layer can predict 3 classes: [0, 1, 2].



      How can I apply different class weight dictionaries for each of the outputs?



      For example, how could I apply the dictionary {0: 1, 1: 10} to the first output,

      and {0: 5, 1: 1, 2: 10} to the second output?



      I've tried to use the following class weights dictionary
      weight_class={'output1': {0: 1, 1: 10}, 'output2': {0: 5, 1: 1, 2: 10}}

      But the code fails with an error.



      My script also runs normally when i remove the class_weight parameter



      Code Example



      I've created a minimal example that reproduces the error



      from tensorflow.python.keras.models import Model
      from tensorflow.python.keras.layers import Input, Dense
      from tensorflow.python.data import Dataset
      import tensorflow as tf
      import numpy as np


      def preprocess_sample(features, labels):
      label1, label2 = labels
      label1 = tf.one_hot(label1, 2)
      label2 = tf.one_hot(label2, 3)
      return features, (label1, label2)


      batch_size = 32

      num_samples = 1000
      num_features = 10

      features = np.random.rand(num_samples, num_features)
      labels1 = np.random.randint(2, size=num_samples)
      labels2 = np.random.randint(3, size=num_samples)

      train = Dataset.from_tensor_slices((features, (labels1, labels2))).map(preprocess_sample).batch(batch_size).repeat()

      # Model
      inputs = Input(shape=(num_features, ))
      output1 = Dense(2, activation='softmax', name='output1')(inputs)
      output2 = Dense(3, activation='softmax', name='output2')(inputs)
      model = Model(inputs, [output1, output2])

      model.compile(loss='categorical_crossentropy', optimizer='adam')
      class_weights = {'output1': {0: 1, 1: 10}, 'output2': {0: 5, 1: 1, 2: 10}}
      model.fit(train, epochs=10, steps_per_epoch=num_samples // batch_size,
      # class_weight=class_weights
      )


      This code runs successfully without the class_weight parameter.

      But when you add the class_weight parameter by uncommenting the line
      # class_weight=class_weights than the script fails with the following error:



      Traceback (most recent call last):
      File "test.py", line 35, in <module>
      class_weight=class_weights
      File "venv/lib/python3.6/site-packages/tensorflow/python/keras/engine/training.py", line 1536, in fit
      validation_split=validation_split)
      File "venv/lib/python3.6/site-packages/tensorflow/python/keras/engine/training.py", line 992, in _standardize_user_data
      class_weight, batch_size)
      File "venv/lib/python3.6/site-packages/tensorflow/python/keras/engine/training.py", line 1165, in _standardize_weights
      feed_sample_weight_modes)
      File "venv/lib/python3.6/site-packages/tensorflow/python/keras/engine/training.py", line 1164, in <listcomp>
      for (ref, sw, cw, mode) in zip(y, sample_weights, class_weights,
      File "venv/lib/python3.6/site-packages/tensorflow/python/keras/engine/training_utils.py", line 717, in standardize_weights
      y_classes = np.argmax(y, axis=1)
      File "venv/lib/python3.6/site-packages/numpy/core/fromnumeric.py", line 1004, in argmax
      return _wrapfunc(a, 'argmax', axis=axis, out=out)
      File "venv/lib/python3.6/site-packages/numpy/core/fromnumeric.py", line 62, in _wrapfunc
      return _wrapit(obj, method, *args, **kwds)
      File "venv/lib/python3.6/site-packages/numpy/core/fromnumeric.py", line 42, in _wrapit
      result = getattr(asarray(obj), method)(*args, **kwds)
      numpy.core._internal.AxisError: axis 1 is out of bounds for array of dimension 1


      Edit



      I've also opened an issue in the Keras github page, but i wanted to ask the same question here to see if perhaps i'm missing something and doing something wrong.







      neural-network keras multiclass-classification beginner weighted-data






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 26 '18 at 14:18







      Gal Avineri

















      asked Nov 26 '18 at 9:32









      Gal AvineriGal Avineri

      667




      667





      bumped to the homepage by Community 44 mins ago


      This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.







      bumped to the homepage by Community 44 mins ago


      This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.
























          1 Answer
          1






          active

          oldest

          votes


















          0












          $begingroup$

          I wansn't able to use the class_weight parameter yet, but in the mean time i've found another way to apply class weighting to each output layer.



          Current solution



          In this keras issue they have supplied an easy method to apply class weights via a custom loss that implements the required class weighing.



          def weighted_categorical_crossentropy(y_true, y_pred, weights):
          nb_cl = len(weights)
          final_mask = K.zeros_like(y_pred[:, 0])
          y_pred_max = K.max(y_pred, axis=1)
          y_pred_max = K.reshape(y_pred_max, (K.shape(y_pred)[0], 1))
          y_pred_max_mat = K.cast(K.equal(y_pred, y_pred_max), K.floatx())
          for c_p, c_t in product(range(nb_cl), range(nb_cl)):
          final_mask += (weights[c_t, c_p] * y_pred_max_mat[:, c_p] * y_true[:, c_t])
          return K.categorical_crossentropy(y_pred, y_true) * final_mask


          where weights is a CxC matrix (where C is the number of classes) that defines the class weights.

          More precisely, weights[i, j] defines the weight for an example of class i which was falsely classified as class j.



          So how do we use it?



          Keras allows to assign a loss function for each output.

          so we could assign each output a loss fucntion with the correct weights matrix.



          For example, to satisfy the request i made in the question we could suggest the following code.



          # Define the weight matrices
          w1 = np.ones((2, 2))
          w1[1, 0] = 10
          w1[1, 1] = 10

          w2 = np.ones((3, 3))
          w2[0, 0] = 5
          w2[0, 1] = 5
          w2[0, 2] = 5
          w2[2, 0] = 10
          w2[2, 1] = 10
          w2[2, 2] = 10

          # Define the weighted loss functions
          from functools import partial
          loss1 = partial(weighted_categorical_crossentropy, weights=w1)
          loss2 = partial(weighted_categorical_crossentropy, weights=w2)

          # Finally, apply the loss functions to the outputs
          model.compile(loss={'output1': loss1, 'output2': loss2}, optimizer='adam')


          And that accomplishes the request :)



          Edit



          There is a small edition that needs to be made.

          The loss functions must have a name, so we can supply this with the following:



          loss1.__name__ = 'loss1'
          loss2.__name__ = 'loss2'





          share|improve this answer









          $endgroup$














            Your Answer








            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "557"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: false,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: null,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fdatascience.stackexchange.com%2fquestions%2f41698%2fhow-to-apply-class-weight-to-a-multi-output-model%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            0












            $begingroup$

            I wansn't able to use the class_weight parameter yet, but in the mean time i've found another way to apply class weighting to each output layer.



            Current solution



            In this keras issue they have supplied an easy method to apply class weights via a custom loss that implements the required class weighing.



            def weighted_categorical_crossentropy(y_true, y_pred, weights):
            nb_cl = len(weights)
            final_mask = K.zeros_like(y_pred[:, 0])
            y_pred_max = K.max(y_pred, axis=1)
            y_pred_max = K.reshape(y_pred_max, (K.shape(y_pred)[0], 1))
            y_pred_max_mat = K.cast(K.equal(y_pred, y_pred_max), K.floatx())
            for c_p, c_t in product(range(nb_cl), range(nb_cl)):
            final_mask += (weights[c_t, c_p] * y_pred_max_mat[:, c_p] * y_true[:, c_t])
            return K.categorical_crossentropy(y_pred, y_true) * final_mask


            where weights is a CxC matrix (where C is the number of classes) that defines the class weights.

            More precisely, weights[i, j] defines the weight for an example of class i which was falsely classified as class j.



            So how do we use it?



            Keras allows to assign a loss function for each output.

            so we could assign each output a loss fucntion with the correct weights matrix.



            For example, to satisfy the request i made in the question we could suggest the following code.



            # Define the weight matrices
            w1 = np.ones((2, 2))
            w1[1, 0] = 10
            w1[1, 1] = 10

            w2 = np.ones((3, 3))
            w2[0, 0] = 5
            w2[0, 1] = 5
            w2[0, 2] = 5
            w2[2, 0] = 10
            w2[2, 1] = 10
            w2[2, 2] = 10

            # Define the weighted loss functions
            from functools import partial
            loss1 = partial(weighted_categorical_crossentropy, weights=w1)
            loss2 = partial(weighted_categorical_crossentropy, weights=w2)

            # Finally, apply the loss functions to the outputs
            model.compile(loss={'output1': loss1, 'output2': loss2}, optimizer='adam')


            And that accomplishes the request :)



            Edit



            There is a small edition that needs to be made.

            The loss functions must have a name, so we can supply this with the following:



            loss1.__name__ = 'loss1'
            loss2.__name__ = 'loss2'





            share|improve this answer









            $endgroup$


















              0












              $begingroup$

              I wansn't able to use the class_weight parameter yet, but in the mean time i've found another way to apply class weighting to each output layer.



              Current solution



              In this keras issue they have supplied an easy method to apply class weights via a custom loss that implements the required class weighing.



              def weighted_categorical_crossentropy(y_true, y_pred, weights):
              nb_cl = len(weights)
              final_mask = K.zeros_like(y_pred[:, 0])
              y_pred_max = K.max(y_pred, axis=1)
              y_pred_max = K.reshape(y_pred_max, (K.shape(y_pred)[0], 1))
              y_pred_max_mat = K.cast(K.equal(y_pred, y_pred_max), K.floatx())
              for c_p, c_t in product(range(nb_cl), range(nb_cl)):
              final_mask += (weights[c_t, c_p] * y_pred_max_mat[:, c_p] * y_true[:, c_t])
              return K.categorical_crossentropy(y_pred, y_true) * final_mask


              where weights is a CxC matrix (where C is the number of classes) that defines the class weights.

              More precisely, weights[i, j] defines the weight for an example of class i which was falsely classified as class j.



              So how do we use it?



              Keras allows to assign a loss function for each output.

              so we could assign each output a loss fucntion with the correct weights matrix.



              For example, to satisfy the request i made in the question we could suggest the following code.



              # Define the weight matrices
              w1 = np.ones((2, 2))
              w1[1, 0] = 10
              w1[1, 1] = 10

              w2 = np.ones((3, 3))
              w2[0, 0] = 5
              w2[0, 1] = 5
              w2[0, 2] = 5
              w2[2, 0] = 10
              w2[2, 1] = 10
              w2[2, 2] = 10

              # Define the weighted loss functions
              from functools import partial
              loss1 = partial(weighted_categorical_crossentropy, weights=w1)
              loss2 = partial(weighted_categorical_crossentropy, weights=w2)

              # Finally, apply the loss functions to the outputs
              model.compile(loss={'output1': loss1, 'output2': loss2}, optimizer='adam')


              And that accomplishes the request :)



              Edit



              There is a small edition that needs to be made.

              The loss functions must have a name, so we can supply this with the following:



              loss1.__name__ = 'loss1'
              loss2.__name__ = 'loss2'





              share|improve this answer









              $endgroup$
















                0












                0








                0





                $begingroup$

                I wansn't able to use the class_weight parameter yet, but in the mean time i've found another way to apply class weighting to each output layer.



                Current solution



                In this keras issue they have supplied an easy method to apply class weights via a custom loss that implements the required class weighing.



                def weighted_categorical_crossentropy(y_true, y_pred, weights):
                nb_cl = len(weights)
                final_mask = K.zeros_like(y_pred[:, 0])
                y_pred_max = K.max(y_pred, axis=1)
                y_pred_max = K.reshape(y_pred_max, (K.shape(y_pred)[0], 1))
                y_pred_max_mat = K.cast(K.equal(y_pred, y_pred_max), K.floatx())
                for c_p, c_t in product(range(nb_cl), range(nb_cl)):
                final_mask += (weights[c_t, c_p] * y_pred_max_mat[:, c_p] * y_true[:, c_t])
                return K.categorical_crossentropy(y_pred, y_true) * final_mask


                where weights is a CxC matrix (where C is the number of classes) that defines the class weights.

                More precisely, weights[i, j] defines the weight for an example of class i which was falsely classified as class j.



                So how do we use it?



                Keras allows to assign a loss function for each output.

                so we could assign each output a loss fucntion with the correct weights matrix.



                For example, to satisfy the request i made in the question we could suggest the following code.



                # Define the weight matrices
                w1 = np.ones((2, 2))
                w1[1, 0] = 10
                w1[1, 1] = 10

                w2 = np.ones((3, 3))
                w2[0, 0] = 5
                w2[0, 1] = 5
                w2[0, 2] = 5
                w2[2, 0] = 10
                w2[2, 1] = 10
                w2[2, 2] = 10

                # Define the weighted loss functions
                from functools import partial
                loss1 = partial(weighted_categorical_crossentropy, weights=w1)
                loss2 = partial(weighted_categorical_crossentropy, weights=w2)

                # Finally, apply the loss functions to the outputs
                model.compile(loss={'output1': loss1, 'output2': loss2}, optimizer='adam')


                And that accomplishes the request :)



                Edit



                There is a small edition that needs to be made.

                The loss functions must have a name, so we can supply this with the following:



                loss1.__name__ = 'loss1'
                loss2.__name__ = 'loss2'





                share|improve this answer









                $endgroup$



                I wansn't able to use the class_weight parameter yet, but in the mean time i've found another way to apply class weighting to each output layer.



                Current solution



                In this keras issue they have supplied an easy method to apply class weights via a custom loss that implements the required class weighing.



                def weighted_categorical_crossentropy(y_true, y_pred, weights):
                nb_cl = len(weights)
                final_mask = K.zeros_like(y_pred[:, 0])
                y_pred_max = K.max(y_pred, axis=1)
                y_pred_max = K.reshape(y_pred_max, (K.shape(y_pred)[0], 1))
                y_pred_max_mat = K.cast(K.equal(y_pred, y_pred_max), K.floatx())
                for c_p, c_t in product(range(nb_cl), range(nb_cl)):
                final_mask += (weights[c_t, c_p] * y_pred_max_mat[:, c_p] * y_true[:, c_t])
                return K.categorical_crossentropy(y_pred, y_true) * final_mask


                where weights is a CxC matrix (where C is the number of classes) that defines the class weights.

                More precisely, weights[i, j] defines the weight for an example of class i which was falsely classified as class j.



                So how do we use it?



                Keras allows to assign a loss function for each output.

                so we could assign each output a loss fucntion with the correct weights matrix.



                For example, to satisfy the request i made in the question we could suggest the following code.



                # Define the weight matrices
                w1 = np.ones((2, 2))
                w1[1, 0] = 10
                w1[1, 1] = 10

                w2 = np.ones((3, 3))
                w2[0, 0] = 5
                w2[0, 1] = 5
                w2[0, 2] = 5
                w2[2, 0] = 10
                w2[2, 1] = 10
                w2[2, 2] = 10

                # Define the weighted loss functions
                from functools import partial
                loss1 = partial(weighted_categorical_crossentropy, weights=w1)
                loss2 = partial(weighted_categorical_crossentropy, weights=w2)

                # Finally, apply the loss functions to the outputs
                model.compile(loss={'output1': loss1, 'output2': loss2}, optimizer='adam')


                And that accomplishes the request :)



                Edit



                There is a small edition that needs to be made.

                The loss functions must have a name, so we can supply this with the following:



                loss1.__name__ = 'loss1'
                loss2.__name__ = 'loss2'






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 27 '18 at 17:36









                Gal AvineriGal Avineri

                667




                667






























                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Data Science Stack Exchange!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    Use MathJax to format equations. MathJax reference.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fdatascience.stackexchange.com%2fquestions%2f41698%2fhow-to-apply-class-weight-to-a-multi-output-model%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    How to label and detect the document text images

                    Vallis Paradisi

                    Tabula Rosettana