How to use single values of attributes within a custom loss function











up vote
0
down vote

favorite












I'm trying to build a neural network with Keras. Therefore, I need to implement a custom loss function that punishes a false prediction example based on another attribute from the example.
I don't really know if it is even possible to use single attribute values inside a custom loss function in keras.
Here is the header of my data set.



Ereignisart;KONTOPHASE;PRODUKTART;PRODUKT2;BAUSPAREINLAGE;BAUSPARSUMME;BEWERTUNGSZAHL;TREUEOPTIONSSTATUS;ZUTEILUNGSSTATUS;MEHRZUTEILUNG_BETRAG;SPARZINSSATZ;Marktzins;ZEITSCHEIBE;SALDO


The first attribute "Ereignisart" is the one I want to predict. With the last attribute "SALDO" I want to punish the prediction of "Ereignisart" weighted. I know how to write a custom loss function and give it the attribute "SALDO" ( i got this from here if someone is interested: https://datascience.stackexchange.com/questions/28440/custom-conditional-loss-function-in-keras) . But if I do it the way you can see below, every example of SALDO will be used and not only the given value of the example.content of SALDO in debugger



def customLoss(SALDO):
def loss(yTrue, yPred):
first_log=kBack.log(kBack.clip(yPred, kBack.epsilon(), None) + 1.)
second_log=kBack.log(kBack.clip(yTrue, kBack.epsilon(), None) + 1.)
return kBack.mean(kBack.square(first_log - "penalty dependent on SALDO" *second_log)), axis=-1)
return loss


In short, I guess I want to use the corresponding value of "SALDO" in my loss function for each pair of yTrue and yPred to punish a false prediction based on "SALDO".



tl;dr Is it possible to use single values of attributes in a custom Kears Loss function? If so, how?



Many thanks in advance



Fabian










share|improve this question




























    up vote
    0
    down vote

    favorite












    I'm trying to build a neural network with Keras. Therefore, I need to implement a custom loss function that punishes a false prediction example based on another attribute from the example.
    I don't really know if it is even possible to use single attribute values inside a custom loss function in keras.
    Here is the header of my data set.



    Ereignisart;KONTOPHASE;PRODUKTART;PRODUKT2;BAUSPAREINLAGE;BAUSPARSUMME;BEWERTUNGSZAHL;TREUEOPTIONSSTATUS;ZUTEILUNGSSTATUS;MEHRZUTEILUNG_BETRAG;SPARZINSSATZ;Marktzins;ZEITSCHEIBE;SALDO


    The first attribute "Ereignisart" is the one I want to predict. With the last attribute "SALDO" I want to punish the prediction of "Ereignisart" weighted. I know how to write a custom loss function and give it the attribute "SALDO" ( i got this from here if someone is interested: https://datascience.stackexchange.com/questions/28440/custom-conditional-loss-function-in-keras) . But if I do it the way you can see below, every example of SALDO will be used and not only the given value of the example.content of SALDO in debugger



    def customLoss(SALDO):
    def loss(yTrue, yPred):
    first_log=kBack.log(kBack.clip(yPred, kBack.epsilon(), None) + 1.)
    second_log=kBack.log(kBack.clip(yTrue, kBack.epsilon(), None) + 1.)
    return kBack.mean(kBack.square(first_log - "penalty dependent on SALDO" *second_log)), axis=-1)
    return loss


    In short, I guess I want to use the corresponding value of "SALDO" in my loss function for each pair of yTrue and yPred to punish a false prediction based on "SALDO".



    tl;dr Is it possible to use single values of attributes in a custom Kears Loss function? If so, how?



    Many thanks in advance



    Fabian










    share|improve this question


























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      I'm trying to build a neural network with Keras. Therefore, I need to implement a custom loss function that punishes a false prediction example based on another attribute from the example.
      I don't really know if it is even possible to use single attribute values inside a custom loss function in keras.
      Here is the header of my data set.



      Ereignisart;KONTOPHASE;PRODUKTART;PRODUKT2;BAUSPAREINLAGE;BAUSPARSUMME;BEWERTUNGSZAHL;TREUEOPTIONSSTATUS;ZUTEILUNGSSTATUS;MEHRZUTEILUNG_BETRAG;SPARZINSSATZ;Marktzins;ZEITSCHEIBE;SALDO


      The first attribute "Ereignisart" is the one I want to predict. With the last attribute "SALDO" I want to punish the prediction of "Ereignisart" weighted. I know how to write a custom loss function and give it the attribute "SALDO" ( i got this from here if someone is interested: https://datascience.stackexchange.com/questions/28440/custom-conditional-loss-function-in-keras) . But if I do it the way you can see below, every example of SALDO will be used and not only the given value of the example.content of SALDO in debugger



      def customLoss(SALDO):
      def loss(yTrue, yPred):
      first_log=kBack.log(kBack.clip(yPred, kBack.epsilon(), None) + 1.)
      second_log=kBack.log(kBack.clip(yTrue, kBack.epsilon(), None) + 1.)
      return kBack.mean(kBack.square(first_log - "penalty dependent on SALDO" *second_log)), axis=-1)
      return loss


      In short, I guess I want to use the corresponding value of "SALDO" in my loss function for each pair of yTrue and yPred to punish a false prediction based on "SALDO".



      tl;dr Is it possible to use single values of attributes in a custom Kears Loss function? If so, how?



      Many thanks in advance



      Fabian










      share|improve this question















      I'm trying to build a neural network with Keras. Therefore, I need to implement a custom loss function that punishes a false prediction example based on another attribute from the example.
      I don't really know if it is even possible to use single attribute values inside a custom loss function in keras.
      Here is the header of my data set.



      Ereignisart;KONTOPHASE;PRODUKTART;PRODUKT2;BAUSPAREINLAGE;BAUSPARSUMME;BEWERTUNGSZAHL;TREUEOPTIONSSTATUS;ZUTEILUNGSSTATUS;MEHRZUTEILUNG_BETRAG;SPARZINSSATZ;Marktzins;ZEITSCHEIBE;SALDO


      The first attribute "Ereignisart" is the one I want to predict. With the last attribute "SALDO" I want to punish the prediction of "Ereignisart" weighted. I know how to write a custom loss function and give it the attribute "SALDO" ( i got this from here if someone is interested: https://datascience.stackexchange.com/questions/28440/custom-conditional-loss-function-in-keras) . But if I do it the way you can see below, every example of SALDO will be used and not only the given value of the example.content of SALDO in debugger



      def customLoss(SALDO):
      def loss(yTrue, yPred):
      first_log=kBack.log(kBack.clip(yPred, kBack.epsilon(), None) + 1.)
      second_log=kBack.log(kBack.clip(yTrue, kBack.epsilon(), None) + 1.)
      return kBack.mean(kBack.square(first_log - "penalty dependent on SALDO" *second_log)), axis=-1)
      return loss


      In short, I guess I want to use the corresponding value of "SALDO" in my loss function for each pair of yTrue and yPred to punish a false prediction based on "SALDO".



      tl;dr Is it possible to use single values of attributes in a custom Kears Loss function? If so, how?



      Many thanks in advance



      Fabian







      python-2.7 tensorflow keras loss-function






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 19 at 14:01

























      asked Nov 19 at 13:19









      Fabian

      14




      14





























          active

          oldest

          votes











          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',
          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%2f53375512%2fhow-to-use-single-values-of-attributes-within-a-custom-loss-function%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown






























          active

          oldest

          votes













          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes
















          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.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • 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%2f53375512%2fhow-to-use-single-values-of-attributes-within-a-custom-loss-function%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

          Costa Masnaga

          Fotorealismo

          Sidney Franklin