keras: how to add weights to loss evaluation












0














Todo :




  • I would like to add a weight for each pattern loss in a given Keras loss function.


    For example:
    if the error on pattern i is l_i,
    I would like to consider, instead, an error l_i * c_i, where c_i is an input scalar.













share|improve this question





























    0














    Todo :




    • I would like to add a weight for each pattern loss in a given Keras loss function.


      For example:
      if the error on pattern i is l_i,
      I would like to consider, instead, an error l_i * c_i, where c_i is an input scalar.













    share|improve this question



























      0












      0








      0







      Todo :




      • I would like to add a weight for each pattern loss in a given Keras loss function.


        For example:
        if the error on pattern i is l_i,
        I would like to consider, instead, an error l_i * c_i, where c_i is an input scalar.













      share|improve this question















      Todo :




      • I would like to add a weight for each pattern loss in a given Keras loss function.


        For example:
        if the error on pattern i is l_i,
        I would like to consider, instead, an error l_i * c_i, where c_i is an input scalar.










      keras






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 13 '18 at 10:36









      Abhishek Kumar

      1,020216




      1,020216










      asked Nov 13 '18 at 10:27









      Filippo PorteraFilippo Portera

      206




      206
























          1 Answer
          1






          active

          oldest

          votes


















          0














          def customloss(y_true, y_pred):
          c_i = ...
          loss = ...(only use tensor operations on y_true and y_pred or use built in keras losses)
          return c_i*loss


          Now compile your model passing the loss function.



          model.compile(loss = customloss)





          share|improve this answer





















          • Sorry for my ignorance: are y_true and y_pred tensors (arrays in this case) covering all patterns? In other words, loss is the loss on a single pattern or the overall loss?
            – Filippo Portera
            Nov 13 '18 at 12:31










          • yes y_true and y_pred are tensors, when training your keras model will call this loss function to calculate the loss. Your loss function needs to return a single value tensor. So it needs to calculate overall loss.
            – Mete Han Kahraman
            Nov 13 '18 at 12:37












          • For my needs, I would consider inserting costs just inside the loss on a single pattern: loss_i * c_i
            – Filippo Portera
            Nov 13 '18 at 12:37










          • I mean: usually a loss is the sum of a function of y_true and y_pred for every pattern. I need to associate costs to a pattern level.
            – Filippo Portera
            Nov 13 '18 at 12:40










          • I think I've got your point!
            – Filippo Portera
            Nov 13 '18 at 12:41











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          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: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          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%2fstackoverflow.com%2fquestions%2f53278915%2fkeras-how-to-add-weights-to-loss-evaluation%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














          def customloss(y_true, y_pred):
          c_i = ...
          loss = ...(only use tensor operations on y_true and y_pred or use built in keras losses)
          return c_i*loss


          Now compile your model passing the loss function.



          model.compile(loss = customloss)





          share|improve this answer





















          • Sorry for my ignorance: are y_true and y_pred tensors (arrays in this case) covering all patterns? In other words, loss is the loss on a single pattern or the overall loss?
            – Filippo Portera
            Nov 13 '18 at 12:31










          • yes y_true and y_pred are tensors, when training your keras model will call this loss function to calculate the loss. Your loss function needs to return a single value tensor. So it needs to calculate overall loss.
            – Mete Han Kahraman
            Nov 13 '18 at 12:37












          • For my needs, I would consider inserting costs just inside the loss on a single pattern: loss_i * c_i
            – Filippo Portera
            Nov 13 '18 at 12:37










          • I mean: usually a loss is the sum of a function of y_true and y_pred for every pattern. I need to associate costs to a pattern level.
            – Filippo Portera
            Nov 13 '18 at 12:40










          • I think I've got your point!
            – Filippo Portera
            Nov 13 '18 at 12:41
















          0














          def customloss(y_true, y_pred):
          c_i = ...
          loss = ...(only use tensor operations on y_true and y_pred or use built in keras losses)
          return c_i*loss


          Now compile your model passing the loss function.



          model.compile(loss = customloss)





          share|improve this answer





















          • Sorry for my ignorance: are y_true and y_pred tensors (arrays in this case) covering all patterns? In other words, loss is the loss on a single pattern or the overall loss?
            – Filippo Portera
            Nov 13 '18 at 12:31










          • yes y_true and y_pred are tensors, when training your keras model will call this loss function to calculate the loss. Your loss function needs to return a single value tensor. So it needs to calculate overall loss.
            – Mete Han Kahraman
            Nov 13 '18 at 12:37












          • For my needs, I would consider inserting costs just inside the loss on a single pattern: loss_i * c_i
            – Filippo Portera
            Nov 13 '18 at 12:37










          • I mean: usually a loss is the sum of a function of y_true and y_pred for every pattern. I need to associate costs to a pattern level.
            – Filippo Portera
            Nov 13 '18 at 12:40










          • I think I've got your point!
            – Filippo Portera
            Nov 13 '18 at 12:41














          0












          0








          0






          def customloss(y_true, y_pred):
          c_i = ...
          loss = ...(only use tensor operations on y_true and y_pred or use built in keras losses)
          return c_i*loss


          Now compile your model passing the loss function.



          model.compile(loss = customloss)





          share|improve this answer












          def customloss(y_true, y_pred):
          c_i = ...
          loss = ...(only use tensor operations on y_true and y_pred or use built in keras losses)
          return c_i*loss


          Now compile your model passing the loss function.



          model.compile(loss = customloss)






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 13 '18 at 12:09









          Mete Han KahramanMete Han Kahraman

          40017




          40017












          • Sorry for my ignorance: are y_true and y_pred tensors (arrays in this case) covering all patterns? In other words, loss is the loss on a single pattern or the overall loss?
            – Filippo Portera
            Nov 13 '18 at 12:31










          • yes y_true and y_pred are tensors, when training your keras model will call this loss function to calculate the loss. Your loss function needs to return a single value tensor. So it needs to calculate overall loss.
            – Mete Han Kahraman
            Nov 13 '18 at 12:37












          • For my needs, I would consider inserting costs just inside the loss on a single pattern: loss_i * c_i
            – Filippo Portera
            Nov 13 '18 at 12:37










          • I mean: usually a loss is the sum of a function of y_true and y_pred for every pattern. I need to associate costs to a pattern level.
            – Filippo Portera
            Nov 13 '18 at 12:40










          • I think I've got your point!
            – Filippo Portera
            Nov 13 '18 at 12:41


















          • Sorry for my ignorance: are y_true and y_pred tensors (arrays in this case) covering all patterns? In other words, loss is the loss on a single pattern or the overall loss?
            – Filippo Portera
            Nov 13 '18 at 12:31










          • yes y_true and y_pred are tensors, when training your keras model will call this loss function to calculate the loss. Your loss function needs to return a single value tensor. So it needs to calculate overall loss.
            – Mete Han Kahraman
            Nov 13 '18 at 12:37












          • For my needs, I would consider inserting costs just inside the loss on a single pattern: loss_i * c_i
            – Filippo Portera
            Nov 13 '18 at 12:37










          • I mean: usually a loss is the sum of a function of y_true and y_pred for every pattern. I need to associate costs to a pattern level.
            – Filippo Portera
            Nov 13 '18 at 12:40










          • I think I've got your point!
            – Filippo Portera
            Nov 13 '18 at 12:41
















          Sorry for my ignorance: are y_true and y_pred tensors (arrays in this case) covering all patterns? In other words, loss is the loss on a single pattern or the overall loss?
          – Filippo Portera
          Nov 13 '18 at 12:31




          Sorry for my ignorance: are y_true and y_pred tensors (arrays in this case) covering all patterns? In other words, loss is the loss on a single pattern or the overall loss?
          – Filippo Portera
          Nov 13 '18 at 12:31












          yes y_true and y_pred are tensors, when training your keras model will call this loss function to calculate the loss. Your loss function needs to return a single value tensor. So it needs to calculate overall loss.
          – Mete Han Kahraman
          Nov 13 '18 at 12:37






          yes y_true and y_pred are tensors, when training your keras model will call this loss function to calculate the loss. Your loss function needs to return a single value tensor. So it needs to calculate overall loss.
          – Mete Han Kahraman
          Nov 13 '18 at 12:37














          For my needs, I would consider inserting costs just inside the loss on a single pattern: loss_i * c_i
          – Filippo Portera
          Nov 13 '18 at 12:37




          For my needs, I would consider inserting costs just inside the loss on a single pattern: loss_i * c_i
          – Filippo Portera
          Nov 13 '18 at 12:37












          I mean: usually a loss is the sum of a function of y_true and y_pred for every pattern. I need to associate costs to a pattern level.
          – Filippo Portera
          Nov 13 '18 at 12:40




          I mean: usually a loss is the sum of a function of y_true and y_pred for every pattern. I need to associate costs to a pattern level.
          – Filippo Portera
          Nov 13 '18 at 12:40












          I think I've got your point!
          – Filippo Portera
          Nov 13 '18 at 12:41




          I think I've got your point!
          – Filippo Portera
          Nov 13 '18 at 12:41


















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • 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.


          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%2fstackoverflow.com%2fquestions%2f53278915%2fkeras-how-to-add-weights-to-loss-evaluation%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

          List item for chat from Array inside array React Native

          Thiostrepton

          Caerphilly