How to apply class weight to a multi-output model?TensorFlow and Categorical variablesImport keras ErrorMaking Keras + Tensorflow code execution deterministic on a GPUTensorflow regression predicting 1 for all inputsKeras LSTM: use weights from Keras model to replicate predictions using numpyANN applied to Boston Housing dataset returns negative valuewith tf.device(DEVICE): model = modellib.MaskRCNN(mode = “inference”, model_dir = LOGS_DIR, config = config)Using categorial_crossentropy to train a model in kerasMulti-label compute class weight - unhashable typeIN CIFAR 10 DATASET

How can the Githyanki Supreme Commander move while insubstantial?

Don’t seats that recline flat defeat the purpose of having seatbelts?

What's the polite way to say "I need to urinate"?

Elements that can bond to themselves?

Is the claim "Employers won't employ people with no 'social media presence'" realistic?

Can I grease a crank spindle/bracket without disassembling the crank set?

"You've called the wrong number" or "You called the wrong number"

bldc motor, esc and battery draw, nominal vs peak

Why was the Spitfire's elliptical wing almost uncopied by other aircraft of World War 2?

How did Captain America manage to do this?

Is there any official lore on the Far Realm?

What is the most expensive material in the world that could be used to create Pun-Pun's lute?

How to fry ground beef so it is well-browned

Which big number is bigger?

What happens to Mjolnir (Thor's hammer) at the end of Endgame?

Betweenness centrality formula

can anyone help me with this awful query plan?

Why does nature favour the Laplacian?

On The Origin of Dissonant Chords

Does tea made with boiling water cool faster than tea made with boiled (but still hot) water?

Can we say “you can pay when the order gets ready”?

Is there really no use for MD5 anymore?

How could Tony Stark make this in Endgame?

a sore throat vs a strep throat vs strep throat



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


TensorFlow and Categorical variablesImport keras ErrorMaking Keras + Tensorflow code execution deterministic on a GPUTensorflow regression predicting 1 for all inputsKeras LSTM: use weights from Keras model to replicate predictions using numpyANN applied to Boston Housing dataset returns negative valuewith tf.device(DEVICE): model = modellib.MaskRCNN(mode = “inference”, model_dir = LOGS_DIR, config = config)Using categorial_crossentropy to train a model in kerasMulti-label compute class weight - unhashable typeIN CIFAR 10 DATASET













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 53 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 53 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 53 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 53 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

                    Францішак Багушэвіч Змест Сям'я | Біяграфія | Творчасць | Мова Багушэвіча | Ацэнкі дзейнасці | Цікавыя факты | Спадчына | Выбраная бібліяграфія | Ушанаванне памяці | У філатэліі | Зноскі | Літаратура | Спасылкі | НавігацыяЛяхоўскі У. Рупіўся дзеля Бога і людзей: Жыццёвы шлях Лявона Вітан-Дубейкаўскага // Вольскі і Памідораў з песняй пра немца Адвакат, паэт, народны заступнік Ашмянскі веснікВ Минске появится площадь Богушевича и улица Сырокомли, Белорусская деловая газета, 19 июля 2001 г.Айцец беларускай нацыянальнай ідэі паўстаў у бронзе Сяргей Аляксандравіч Адашкевіч (1918, Мінск). 80-я гады. Бюст «Францішак Багушэвіч».Яўген Мікалаевіч Ціхановіч. «Партрэт Францішка Багушэвіча»Мікола Мікалаевіч Купава. «Партрэт зачынальніка новай беларускай літаратуры Францішка Багушэвіча»Уладзімір Іванавіч Мелехаў. На помніку «Змагарам за родную мову» Барэльеф «Францішак Багушэвіч»Памяць пра Багушэвіча на Віленшчыне Страчаная сталіца. Беларускія шыльды на вуліцах Вільні«Krynica». Ideologia i przywódcy białoruskiego katolicyzmuФранцішак БагушэвічТворы на knihi.comТворы Францішка Багушэвіча на bellib.byСодаль Уладзімір. Францішак Багушэвіч на Лідчыне;Луцкевіч Антон. Жыцьцё і творчасьць Фр. Багушэвіча ў успамінах ягоных сучасьнікаў // Запісы Беларускага Навуковага таварыства. Вільня, 1938. Сшытак 1. С. 16-34.Большая российская1188761710000 0000 5537 633Xn9209310021619551927869394п

                    Беларусь Змест Назва Гісторыя Геаграфія Сімволіка Дзяржаўны лад Палітычныя партыі Міжнароднае становішча і знешняя палітыка Адміністрацыйны падзел Насельніцтва Эканоміка Культура і грамадства Сацыяльная сфера Узброеныя сілы Заўвагі Літаратура Спасылкі НавігацыяHGЯOiТоп-2011 г. (па версіі ej.by)Топ-2013 г. (па версіі ej.by)Топ-2016 г. (па версіі ej.by)Топ-2017 г. (па версіі ej.by)Нацыянальны статыстычны камітэт Рэспублікі БеларусьШчыльнасць насельніцтва па краінахhttp://naviny.by/rubrics/society/2011/09/16/ic_articles_116_175144/А. Калечыц, У. Ксяндзоў. Спробы засялення краю неандэртальскім чалавекам.І ў Менску былі мамантыА. Калечыц, У. Ксяндзоў. Старажытны каменны век (палеаліт). Першапачатковае засяленне тэрыторыіГ. Штыхаў. Балты і славяне ў VI—VIII стст.М. Клімаў. Полацкае княства ў IX—XI стст.Г. Штыхаў, В. Ляўко. Палітычная гісторыя Полацкай зямліГ. Штыхаў. Дзяржаўны лад у землях-княствахГ. Штыхаў. Дзяржаўны лад у землях-княствахБеларускія землі ў складзе Вялікага Княства ЛітоўскагаЛюблінская унія 1569 г."The Early Stages of Independence"Zapomniane prawdy25 гадоў таму было аб'яўлена, што Язэп Пілсудскі — беларус (фота)Наша вадаДакументы ЧАЭС: Забруджванне тэрыторыі Беларусі « ЧАЭС Зона адчужэнняСведения о политических партиях, зарегистрированных в Республике Беларусь // Министерство юстиции Республики БеларусьСтатыстычны бюлетэнь „Полаўзроставая структура насельніцтва Рэспублікі Беларусь на 1 студзеня 2012 года і сярэднегадовая колькасць насельніцтва за 2011 год“Индекс человеческого развития Беларуси — не было бы нижеБеларусь занимает первое место в СНГ по индексу развития с учетом гендерного факцёраНацыянальны статыстычны камітэт Рэспублікі БеларусьКанстытуцыя РБ. Артыкул 17Трансфармацыйныя задачы БеларусіВыйсце з крызісу — далейшае рэфармаванне Беларускі рубель — сусветны лідар па дэвальвацыяхПра змену коштаў у кастрычніку 2011 г.Бядней за беларусаў у СНД толькі таджыкіСярэдні заробак у верасні дасягнуў 2,26 мільёна рублёўЭканомікаГаласуем за ТОП-100 беларускай прозыСучасныя беларускія мастакіАрхитектура Беларуси BELARUS.BYА. Каханоўскі. Культура Беларусі ўсярэдзіне XVII—XVIII ст.Анталогія беларускай народнай песні, гуказапісы спеваўБеларускія Музычныя IнструментыБеларускі рок, які мы страцілі. Топ-10 гуртоў«Мясцовы час» — нязгаслая легенда беларускай рок-музыкіСЯРГЕЙ БУДКІН. МЫ НЯ ЗНАЕМ СВАЁЙ МУЗЫКІМ. А. Каладзінскі. НАРОДНЫ ТЭАТРМагнацкія культурныя цэнтрыПублічная дыскусія «Беларуская новая пьеса: без беларускай мовы ці беларуская?»Беларускія драматургі па-ранейшаму лепш ставяцца за мяжой, чым на радзіме«Працэс незалежнага кіно пайшоў, і дзяржаву турбуе яго непадкантрольнасць»Беларускія філосафы ў пошуках прасторыВсе идём в библиотекуАрхіваванаАб Нацыянальнай праграме даследавання і выкарыстання касмічнай прасторы ў мірных мэтах на 2008—2012 гадыУ космас — разам.У суседнім з Барысаўскім раёне пабудуюць Камандна-вымяральны пунктСвяты і абрады беларусаў«Мірныя бульбашы з малой краіны» — 5 непраўдзівых стэрэатыпаў пра БеларусьМ. Раманюк. Беларускае народнае адзеннеУ Беларусі скарачаецца колькасць злачынстваўЛукашэнка незадаволены мінскімі ўладамі Крадзяжы складаюць у Мінску каля 70% злачынстваў Узровень злачыннасці ў Мінскай вобласці — адзін з самых высокіх у краіне Генпракуратура аналізуе стан са злачыннасцю ў Беларусі па каэфіцыенце злачыннасці У Беларусі стабілізавалася крымінагеннае становішча, лічыць генпракурорЗамежнікі сталі здзяйсняць у Беларусі больш злачынстваўМУС Беларусі турбуе рост рэцыдыўнай злачыннасціЯ з ЖЭСа. Дазволіце вас абкрасці! Рэйтынг усіх службаў і падраздзяленняў ГУУС Мінгарвыканкама вырасАб КДБ РБГісторыя Аператыўна-аналітычнага цэнтра РБГісторыя ДКФРТаможняagentura.ruБеларусьBelarus.by — Афіцыйны сайт Рэспублікі БеларусьСайт урада БеларусіRadzima.org — Збор архітэктурных помнікаў, гісторыя Беларусі«Глобус Беларуси»Гербы и флаги БеларусиАсаблівасці каменнага веку на БеларусіА. Калечыц, У. Ксяндзоў. Старажытны каменны век (палеаліт). Першапачатковае засяленне тэрыторыіУ. Ксяндзоў. Сярэдні каменны век (мезаліт). Засяленне краю плямёнамі паляўнічых, рыбакоў і збіральнікаўА. Калечыц, М. Чарняўскі. Плямёны на тэрыторыі Беларусі ў новым каменным веку (неаліце)А. Калечыц, У. Ксяндзоў, М. Чарняўскі. Гаспадарчыя заняткі ў каменным векуЭ. Зайкоўскі. Духоўная культура ў каменным векуАсаблівасці бронзавага веку на БеларусіФарміраванне супольнасцей ранняга перыяду бронзавага векуФотографии БеларусиРоля беларускіх зямель ва ўтварэнні і ўмацаванні ВКЛВ. Фадзеева. З гісторыі развіцця беларускай народнай вышыўкіDMOZGran catalanaБольшая российскаяBritannica (анлайн)Швейцарскі гістарычны15325917611952699xDA123282154079143-90000 0001 2171 2080n9112870100577502ge128882171858027501086026362074122714179пппппп

                    Герб Смалявічаў Апісанне | Спасылкі | НавігацыяГерб города Смолевичип