Use both sample_weight and class_weight simultaneously












3















My dataset already has weighted examples. And in this binary classification I also have far more of the first class compared to the second.



Can I use both sample_weight and further re-weight it with class_weight in the model.fit() function?



Or do I first make a new array of new_weights and pass it to the fit function as sample_weight?



Edit:



TO further clarify, I already have individual weights for each sample in my dataset, and to further add to the complexity, the total sum of sample weights of the first class is far more than the total sample weights of the second class.



For example I currently have:




y = [0,0,0,0,1,1]



sample_weights = [0.01,0.03,0.05,0.02, 0.01,0.02]




so the sum of weights for class '0' is 0.11 and for class '1' is 0.03. So I should have:




class_weight = {0 : 1. , 1: 0.11/0.03}




I need to use both sample_weight AND class_weight features. If one overrides the other then I will have to create new sample_weights and then use fit() or train_on_batch().



So my question is, can I use both, or does one override the other?










share|improve this question





























    3















    My dataset already has weighted examples. And in this binary classification I also have far more of the first class compared to the second.



    Can I use both sample_weight and further re-weight it with class_weight in the model.fit() function?



    Or do I first make a new array of new_weights and pass it to the fit function as sample_weight?



    Edit:



    TO further clarify, I already have individual weights for each sample in my dataset, and to further add to the complexity, the total sum of sample weights of the first class is far more than the total sample weights of the second class.



    For example I currently have:




    y = [0,0,0,0,1,1]



    sample_weights = [0.01,0.03,0.05,0.02, 0.01,0.02]




    so the sum of weights for class '0' is 0.11 and for class '1' is 0.03. So I should have:




    class_weight = {0 : 1. , 1: 0.11/0.03}




    I need to use both sample_weight AND class_weight features. If one overrides the other then I will have to create new sample_weights and then use fit() or train_on_batch().



    So my question is, can I use both, or does one override the other?










    share|improve this question



























      3












      3








      3


      2






      My dataset already has weighted examples. And in this binary classification I also have far more of the first class compared to the second.



      Can I use both sample_weight and further re-weight it with class_weight in the model.fit() function?



      Or do I first make a new array of new_weights and pass it to the fit function as sample_weight?



      Edit:



      TO further clarify, I already have individual weights for each sample in my dataset, and to further add to the complexity, the total sum of sample weights of the first class is far more than the total sample weights of the second class.



      For example I currently have:




      y = [0,0,0,0,1,1]



      sample_weights = [0.01,0.03,0.05,0.02, 0.01,0.02]




      so the sum of weights for class '0' is 0.11 and for class '1' is 0.03. So I should have:




      class_weight = {0 : 1. , 1: 0.11/0.03}




      I need to use both sample_weight AND class_weight features. If one overrides the other then I will have to create new sample_weights and then use fit() or train_on_batch().



      So my question is, can I use both, or does one override the other?










      share|improve this question
















      My dataset already has weighted examples. And in this binary classification I also have far more of the first class compared to the second.



      Can I use both sample_weight and further re-weight it with class_weight in the model.fit() function?



      Or do I first make a new array of new_weights and pass it to the fit function as sample_weight?



      Edit:



      TO further clarify, I already have individual weights for each sample in my dataset, and to further add to the complexity, the total sum of sample weights of the first class is far more than the total sample weights of the second class.



      For example I currently have:




      y = [0,0,0,0,1,1]



      sample_weights = [0.01,0.03,0.05,0.02, 0.01,0.02]




      so the sum of weights for class '0' is 0.11 and for class '1' is 0.03. So I should have:




      class_weight = {0 : 1. , 1: 0.11/0.03}




      I need to use both sample_weight AND class_weight features. If one overrides the other then I will have to create new sample_weights and then use fit() or train_on_batch().



      So my question is, can I use both, or does one override the other?







      python tensorflow keras






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Jan 10 '18 at 9:59







      user7867665

















      asked Jan 9 '18 at 16:59









      user7867665user7867665

      19012




      19012
























          2 Answers
          2






          active

          oldest

          votes


















          5














          You can surely do both if you want, the thing is if that is what you need. According to the keras docs:





          • class_weight: Optional dictionary mapping class indices (integers) to a weight (float) value, used for weighting the loss function (during training only). This can be useful to tell the model to "pay more attention" to samples from an under-represented class.


          • sample_weight: Optional Numpy array of weights for the training samples, used for weighting the loss function (during training only). You can either pass a flat (1D) Numpy array with the same length as the input samples (1:1 mapping between weights and samples), or in the case of temporal data [...].





          So given that you mention that you "have far more of the first class compared to the second" I think that you should go for the class_weight parameter. There you can indicate that ratio your dataset presents so you can compensate for imbalanced data classes. The sample_weight is more when you want to define a weight or importance for each data element.



          For example if you pass:



          class_weight = {0 : 1. , 1: 50.}


          you will be saying that every sample from class 1 would count as 50 samples from class 0, therefore giving more "importance" to your elements from class 1 (as you have less of those samples surely). You can custom this to fit your own needs. More info con imbalanced datasets on this great question.



          Note: To further compare both parameters, have in mind that passing class_weight as {0:1., 1:50.} would be equivalent to pass sample_weight as [1.,1.,1.,...,50.,50.,...], given you had samples whose classes where [0,0,0,...,1,1,...].



          As we can see it is more practical to use class_weight on this case, and sample_weight could be of use on more specific cases where you actually want to give an "importance" to each sample individually. Using both can also be done if the case requires it, but one has to have in mind its cumulative effect.



          Edit: As per your new question, digging on the Keras source code it seems that indeed sample_weights overrides class_weights, here is the piece of code that does it on the _standarize_weigths method (line 499):





          if sample_weight is not None:
          #...Does some error handling...
          return sample_weight #simply returns the weights you passed

          elif isinstance(class_weight, dict):
          #...Some error handling and computations...
          #Then creates an array repeating class weight to match your target classes
          weights = np.asarray([class_weight[cls] for cls in y_classes
          if cls in class_weight])

          #...more error handling...
          return weights


          This means that you can only use one or the other, but not both. Therefore you will indeed need to multiply your sample_weights by the ratio you need to compensate for the imbalance.






          share|improve this answer





















          • 1





            Actually, sample_weight will override class_weight in Keras, and the latter will have no effect at all (somewhat unexpected IMO). So the OP may need to merge the weights into one before passing it to fit.

            – Yu-Yang
            Jan 10 '18 at 3:28








          • 1





            @Yu-Yang interesting, thanks for sharing (will try that and update when on desktop). Still how OP describes it seems that using class_weight will suffice for balancing imbalanced datasets. If further customization is needed then manual merging and calculating sample_weighs could help more.

            – DarkCygnus
            Jan 10 '18 at 3:33






          • 1





            @user7867665 digging Keras source code I found an answer to your new question, updating shortly.

            – DarkCygnus
            Jan 10 '18 at 15:38






          • 1





            @DarkCygnus Thanks! That clears it up. I'm do the re-weighting before training with sample_weights. Perhaps we make a feature request

            – user7867665
            Jan 11 '18 at 19:54






          • 1





            @user7867665 surely that is a feature that may be useful. Go ahead :)

            – DarkCygnus
            Jan 11 '18 at 20:14



















          0














          To add a little to DarkCygnus answer, for those who actually need to use class weight & sample weights simultaneously:

          Here is a code, that I use for generating sample weights for classifying multiclass temporal data in sequences:

          (targets is an array of dimension [#temporal, #categories] with values being in set(#classes), class_weights is an array of [#categories, #classes]).

          The generated sequence has the same length as the targets array and the common usecase in batching is to pad the targets with zeros and the sample weights also up to the same size, thus making the network ignore the padded data.



          def multiclass_temoral_class_weights(targets, class_weights):
          s_weights = np.ones((targets.shape[0],))
          # if we are counting the classes, the weights do not exist yet!
          if class_weights is not None:
          for i in range(len(s_weights)):
          weight = 0.0
          for itarget, target in enumerate(targets[i]):
          weight += class_weights[itarget][int(round(target))]
          s_weights[i] = weight
          return s_weights





          share|improve this answer























            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%2f48173168%2fuse-both-sample-weight-and-class-weight-simultaneously%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            5














            You can surely do both if you want, the thing is if that is what you need. According to the keras docs:





            • class_weight: Optional dictionary mapping class indices (integers) to a weight (float) value, used for weighting the loss function (during training only). This can be useful to tell the model to "pay more attention" to samples from an under-represented class.


            • sample_weight: Optional Numpy array of weights for the training samples, used for weighting the loss function (during training only). You can either pass a flat (1D) Numpy array with the same length as the input samples (1:1 mapping between weights and samples), or in the case of temporal data [...].





            So given that you mention that you "have far more of the first class compared to the second" I think that you should go for the class_weight parameter. There you can indicate that ratio your dataset presents so you can compensate for imbalanced data classes. The sample_weight is more when you want to define a weight or importance for each data element.



            For example if you pass:



            class_weight = {0 : 1. , 1: 50.}


            you will be saying that every sample from class 1 would count as 50 samples from class 0, therefore giving more "importance" to your elements from class 1 (as you have less of those samples surely). You can custom this to fit your own needs. More info con imbalanced datasets on this great question.



            Note: To further compare both parameters, have in mind that passing class_weight as {0:1., 1:50.} would be equivalent to pass sample_weight as [1.,1.,1.,...,50.,50.,...], given you had samples whose classes where [0,0,0,...,1,1,...].



            As we can see it is more practical to use class_weight on this case, and sample_weight could be of use on more specific cases where you actually want to give an "importance" to each sample individually. Using both can also be done if the case requires it, but one has to have in mind its cumulative effect.



            Edit: As per your new question, digging on the Keras source code it seems that indeed sample_weights overrides class_weights, here is the piece of code that does it on the _standarize_weigths method (line 499):





            if sample_weight is not None:
            #...Does some error handling...
            return sample_weight #simply returns the weights you passed

            elif isinstance(class_weight, dict):
            #...Some error handling and computations...
            #Then creates an array repeating class weight to match your target classes
            weights = np.asarray([class_weight[cls] for cls in y_classes
            if cls in class_weight])

            #...more error handling...
            return weights


            This means that you can only use one or the other, but not both. Therefore you will indeed need to multiply your sample_weights by the ratio you need to compensate for the imbalance.






            share|improve this answer





















            • 1





              Actually, sample_weight will override class_weight in Keras, and the latter will have no effect at all (somewhat unexpected IMO). So the OP may need to merge the weights into one before passing it to fit.

              – Yu-Yang
              Jan 10 '18 at 3:28








            • 1





              @Yu-Yang interesting, thanks for sharing (will try that and update when on desktop). Still how OP describes it seems that using class_weight will suffice for balancing imbalanced datasets. If further customization is needed then manual merging and calculating sample_weighs could help more.

              – DarkCygnus
              Jan 10 '18 at 3:33






            • 1





              @user7867665 digging Keras source code I found an answer to your new question, updating shortly.

              – DarkCygnus
              Jan 10 '18 at 15:38






            • 1





              @DarkCygnus Thanks! That clears it up. I'm do the re-weighting before training with sample_weights. Perhaps we make a feature request

              – user7867665
              Jan 11 '18 at 19:54






            • 1





              @user7867665 surely that is a feature that may be useful. Go ahead :)

              – DarkCygnus
              Jan 11 '18 at 20:14
















            5














            You can surely do both if you want, the thing is if that is what you need. According to the keras docs:





            • class_weight: Optional dictionary mapping class indices (integers) to a weight (float) value, used for weighting the loss function (during training only). This can be useful to tell the model to "pay more attention" to samples from an under-represented class.


            • sample_weight: Optional Numpy array of weights for the training samples, used for weighting the loss function (during training only). You can either pass a flat (1D) Numpy array with the same length as the input samples (1:1 mapping between weights and samples), or in the case of temporal data [...].





            So given that you mention that you "have far more of the first class compared to the second" I think that you should go for the class_weight parameter. There you can indicate that ratio your dataset presents so you can compensate for imbalanced data classes. The sample_weight is more when you want to define a weight or importance for each data element.



            For example if you pass:



            class_weight = {0 : 1. , 1: 50.}


            you will be saying that every sample from class 1 would count as 50 samples from class 0, therefore giving more "importance" to your elements from class 1 (as you have less of those samples surely). You can custom this to fit your own needs. More info con imbalanced datasets on this great question.



            Note: To further compare both parameters, have in mind that passing class_weight as {0:1., 1:50.} would be equivalent to pass sample_weight as [1.,1.,1.,...,50.,50.,...], given you had samples whose classes where [0,0,0,...,1,1,...].



            As we can see it is more practical to use class_weight on this case, and sample_weight could be of use on more specific cases where you actually want to give an "importance" to each sample individually. Using both can also be done if the case requires it, but one has to have in mind its cumulative effect.



            Edit: As per your new question, digging on the Keras source code it seems that indeed sample_weights overrides class_weights, here is the piece of code that does it on the _standarize_weigths method (line 499):





            if sample_weight is not None:
            #...Does some error handling...
            return sample_weight #simply returns the weights you passed

            elif isinstance(class_weight, dict):
            #...Some error handling and computations...
            #Then creates an array repeating class weight to match your target classes
            weights = np.asarray([class_weight[cls] for cls in y_classes
            if cls in class_weight])

            #...more error handling...
            return weights


            This means that you can only use one or the other, but not both. Therefore you will indeed need to multiply your sample_weights by the ratio you need to compensate for the imbalance.






            share|improve this answer





















            • 1





              Actually, sample_weight will override class_weight in Keras, and the latter will have no effect at all (somewhat unexpected IMO). So the OP may need to merge the weights into one before passing it to fit.

              – Yu-Yang
              Jan 10 '18 at 3:28








            • 1





              @Yu-Yang interesting, thanks for sharing (will try that and update when on desktop). Still how OP describes it seems that using class_weight will suffice for balancing imbalanced datasets. If further customization is needed then manual merging and calculating sample_weighs could help more.

              – DarkCygnus
              Jan 10 '18 at 3:33






            • 1





              @user7867665 digging Keras source code I found an answer to your new question, updating shortly.

              – DarkCygnus
              Jan 10 '18 at 15:38






            • 1





              @DarkCygnus Thanks! That clears it up. I'm do the re-weighting before training with sample_weights. Perhaps we make a feature request

              – user7867665
              Jan 11 '18 at 19:54






            • 1





              @user7867665 surely that is a feature that may be useful. Go ahead :)

              – DarkCygnus
              Jan 11 '18 at 20:14














            5












            5








            5







            You can surely do both if you want, the thing is if that is what you need. According to the keras docs:





            • class_weight: Optional dictionary mapping class indices (integers) to a weight (float) value, used for weighting the loss function (during training only). This can be useful to tell the model to "pay more attention" to samples from an under-represented class.


            • sample_weight: Optional Numpy array of weights for the training samples, used for weighting the loss function (during training only). You can either pass a flat (1D) Numpy array with the same length as the input samples (1:1 mapping between weights and samples), or in the case of temporal data [...].





            So given that you mention that you "have far more of the first class compared to the second" I think that you should go for the class_weight parameter. There you can indicate that ratio your dataset presents so you can compensate for imbalanced data classes. The sample_weight is more when you want to define a weight or importance for each data element.



            For example if you pass:



            class_weight = {0 : 1. , 1: 50.}


            you will be saying that every sample from class 1 would count as 50 samples from class 0, therefore giving more "importance" to your elements from class 1 (as you have less of those samples surely). You can custom this to fit your own needs. More info con imbalanced datasets on this great question.



            Note: To further compare both parameters, have in mind that passing class_weight as {0:1., 1:50.} would be equivalent to pass sample_weight as [1.,1.,1.,...,50.,50.,...], given you had samples whose classes where [0,0,0,...,1,1,...].



            As we can see it is more practical to use class_weight on this case, and sample_weight could be of use on more specific cases where you actually want to give an "importance" to each sample individually. Using both can also be done if the case requires it, but one has to have in mind its cumulative effect.



            Edit: As per your new question, digging on the Keras source code it seems that indeed sample_weights overrides class_weights, here is the piece of code that does it on the _standarize_weigths method (line 499):





            if sample_weight is not None:
            #...Does some error handling...
            return sample_weight #simply returns the weights you passed

            elif isinstance(class_weight, dict):
            #...Some error handling and computations...
            #Then creates an array repeating class weight to match your target classes
            weights = np.asarray([class_weight[cls] for cls in y_classes
            if cls in class_weight])

            #...more error handling...
            return weights


            This means that you can only use one or the other, but not both. Therefore you will indeed need to multiply your sample_weights by the ratio you need to compensate for the imbalance.






            share|improve this answer















            You can surely do both if you want, the thing is if that is what you need. According to the keras docs:





            • class_weight: Optional dictionary mapping class indices (integers) to a weight (float) value, used for weighting the loss function (during training only). This can be useful to tell the model to "pay more attention" to samples from an under-represented class.


            • sample_weight: Optional Numpy array of weights for the training samples, used for weighting the loss function (during training only). You can either pass a flat (1D) Numpy array with the same length as the input samples (1:1 mapping between weights and samples), or in the case of temporal data [...].





            So given that you mention that you "have far more of the first class compared to the second" I think that you should go for the class_weight parameter. There you can indicate that ratio your dataset presents so you can compensate for imbalanced data classes. The sample_weight is more when you want to define a weight or importance for each data element.



            For example if you pass:



            class_weight = {0 : 1. , 1: 50.}


            you will be saying that every sample from class 1 would count as 50 samples from class 0, therefore giving more "importance" to your elements from class 1 (as you have less of those samples surely). You can custom this to fit your own needs. More info con imbalanced datasets on this great question.



            Note: To further compare both parameters, have in mind that passing class_weight as {0:1., 1:50.} would be equivalent to pass sample_weight as [1.,1.,1.,...,50.,50.,...], given you had samples whose classes where [0,0,0,...,1,1,...].



            As we can see it is more practical to use class_weight on this case, and sample_weight could be of use on more specific cases where you actually want to give an "importance" to each sample individually. Using both can also be done if the case requires it, but one has to have in mind its cumulative effect.



            Edit: As per your new question, digging on the Keras source code it seems that indeed sample_weights overrides class_weights, here is the piece of code that does it on the _standarize_weigths method (line 499):





            if sample_weight is not None:
            #...Does some error handling...
            return sample_weight #simply returns the weights you passed

            elif isinstance(class_weight, dict):
            #...Some error handling and computations...
            #Then creates an array repeating class weight to match your target classes
            weights = np.asarray([class_weight[cls] for cls in y_classes
            if cls in class_weight])

            #...more error handling...
            return weights


            This means that you can only use one or the other, but not both. Therefore you will indeed need to multiply your sample_weights by the ratio you need to compensate for the imbalance.







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Jan 10 '18 at 15:49

























            answered Jan 9 '18 at 18:04









            DarkCygnusDarkCygnus

            3,30832036




            3,30832036








            • 1





              Actually, sample_weight will override class_weight in Keras, and the latter will have no effect at all (somewhat unexpected IMO). So the OP may need to merge the weights into one before passing it to fit.

              – Yu-Yang
              Jan 10 '18 at 3:28








            • 1





              @Yu-Yang interesting, thanks for sharing (will try that and update when on desktop). Still how OP describes it seems that using class_weight will suffice for balancing imbalanced datasets. If further customization is needed then manual merging and calculating sample_weighs could help more.

              – DarkCygnus
              Jan 10 '18 at 3:33






            • 1





              @user7867665 digging Keras source code I found an answer to your new question, updating shortly.

              – DarkCygnus
              Jan 10 '18 at 15:38






            • 1





              @DarkCygnus Thanks! That clears it up. I'm do the re-weighting before training with sample_weights. Perhaps we make a feature request

              – user7867665
              Jan 11 '18 at 19:54






            • 1





              @user7867665 surely that is a feature that may be useful. Go ahead :)

              – DarkCygnus
              Jan 11 '18 at 20:14














            • 1





              Actually, sample_weight will override class_weight in Keras, and the latter will have no effect at all (somewhat unexpected IMO). So the OP may need to merge the weights into one before passing it to fit.

              – Yu-Yang
              Jan 10 '18 at 3:28








            • 1





              @Yu-Yang interesting, thanks for sharing (will try that and update when on desktop). Still how OP describes it seems that using class_weight will suffice for balancing imbalanced datasets. If further customization is needed then manual merging and calculating sample_weighs could help more.

              – DarkCygnus
              Jan 10 '18 at 3:33






            • 1





              @user7867665 digging Keras source code I found an answer to your new question, updating shortly.

              – DarkCygnus
              Jan 10 '18 at 15:38






            • 1





              @DarkCygnus Thanks! That clears it up. I'm do the re-weighting before training with sample_weights. Perhaps we make a feature request

              – user7867665
              Jan 11 '18 at 19:54






            • 1





              @user7867665 surely that is a feature that may be useful. Go ahead :)

              – DarkCygnus
              Jan 11 '18 at 20:14








            1




            1





            Actually, sample_weight will override class_weight in Keras, and the latter will have no effect at all (somewhat unexpected IMO). So the OP may need to merge the weights into one before passing it to fit.

            – Yu-Yang
            Jan 10 '18 at 3:28







            Actually, sample_weight will override class_weight in Keras, and the latter will have no effect at all (somewhat unexpected IMO). So the OP may need to merge the weights into one before passing it to fit.

            – Yu-Yang
            Jan 10 '18 at 3:28






            1




            1





            @Yu-Yang interesting, thanks for sharing (will try that and update when on desktop). Still how OP describes it seems that using class_weight will suffice for balancing imbalanced datasets. If further customization is needed then manual merging and calculating sample_weighs could help more.

            – DarkCygnus
            Jan 10 '18 at 3:33





            @Yu-Yang interesting, thanks for sharing (will try that and update when on desktop). Still how OP describes it seems that using class_weight will suffice for balancing imbalanced datasets. If further customization is needed then manual merging and calculating sample_weighs could help more.

            – DarkCygnus
            Jan 10 '18 at 3:33




            1




            1





            @user7867665 digging Keras source code I found an answer to your new question, updating shortly.

            – DarkCygnus
            Jan 10 '18 at 15:38





            @user7867665 digging Keras source code I found an answer to your new question, updating shortly.

            – DarkCygnus
            Jan 10 '18 at 15:38




            1




            1





            @DarkCygnus Thanks! That clears it up. I'm do the re-weighting before training with sample_weights. Perhaps we make a feature request

            – user7867665
            Jan 11 '18 at 19:54





            @DarkCygnus Thanks! That clears it up. I'm do the re-weighting before training with sample_weights. Perhaps we make a feature request

            – user7867665
            Jan 11 '18 at 19:54




            1




            1





            @user7867665 surely that is a feature that may be useful. Go ahead :)

            – DarkCygnus
            Jan 11 '18 at 20:14





            @user7867665 surely that is a feature that may be useful. Go ahead :)

            – DarkCygnus
            Jan 11 '18 at 20:14













            0














            To add a little to DarkCygnus answer, for those who actually need to use class weight & sample weights simultaneously:

            Here is a code, that I use for generating sample weights for classifying multiclass temporal data in sequences:

            (targets is an array of dimension [#temporal, #categories] with values being in set(#classes), class_weights is an array of [#categories, #classes]).

            The generated sequence has the same length as the targets array and the common usecase in batching is to pad the targets with zeros and the sample weights also up to the same size, thus making the network ignore the padded data.



            def multiclass_temoral_class_weights(targets, class_weights):
            s_weights = np.ones((targets.shape[0],))
            # if we are counting the classes, the weights do not exist yet!
            if class_weights is not None:
            for i in range(len(s_weights)):
            weight = 0.0
            for itarget, target in enumerate(targets[i]):
            weight += class_weights[itarget][int(round(target))]
            s_weights[i] = weight
            return s_weights





            share|improve this answer




























              0














              To add a little to DarkCygnus answer, for those who actually need to use class weight & sample weights simultaneously:

              Here is a code, that I use for generating sample weights for classifying multiclass temporal data in sequences:

              (targets is an array of dimension [#temporal, #categories] with values being in set(#classes), class_weights is an array of [#categories, #classes]).

              The generated sequence has the same length as the targets array and the common usecase in batching is to pad the targets with zeros and the sample weights also up to the same size, thus making the network ignore the padded data.



              def multiclass_temoral_class_weights(targets, class_weights):
              s_weights = np.ones((targets.shape[0],))
              # if we are counting the classes, the weights do not exist yet!
              if class_weights is not None:
              for i in range(len(s_weights)):
              weight = 0.0
              for itarget, target in enumerate(targets[i]):
              weight += class_weights[itarget][int(round(target))]
              s_weights[i] = weight
              return s_weights





              share|improve this answer


























                0












                0








                0







                To add a little to DarkCygnus answer, for those who actually need to use class weight & sample weights simultaneously:

                Here is a code, that I use for generating sample weights for classifying multiclass temporal data in sequences:

                (targets is an array of dimension [#temporal, #categories] with values being in set(#classes), class_weights is an array of [#categories, #classes]).

                The generated sequence has the same length as the targets array and the common usecase in batching is to pad the targets with zeros and the sample weights also up to the same size, thus making the network ignore the padded data.



                def multiclass_temoral_class_weights(targets, class_weights):
                s_weights = np.ones((targets.shape[0],))
                # if we are counting the classes, the weights do not exist yet!
                if class_weights is not None:
                for i in range(len(s_weights)):
                weight = 0.0
                for itarget, target in enumerate(targets[i]):
                weight += class_weights[itarget][int(round(target))]
                s_weights[i] = weight
                return s_weights





                share|improve this answer













                To add a little to DarkCygnus answer, for those who actually need to use class weight & sample weights simultaneously:

                Here is a code, that I use for generating sample weights for classifying multiclass temporal data in sequences:

                (targets is an array of dimension [#temporal, #categories] with values being in set(#classes), class_weights is an array of [#categories, #classes]).

                The generated sequence has the same length as the targets array and the common usecase in batching is to pad the targets with zeros and the sample weights also up to the same size, thus making the network ignore the padded data.



                def multiclass_temoral_class_weights(targets, class_weights):
                s_weights = np.ones((targets.shape[0],))
                # if we are counting the classes, the weights do not exist yet!
                if class_weights is not None:
                for i in range(len(s_weights)):
                weight = 0.0
                for itarget, target in enumerate(targets[i]):
                weight += class_weights[itarget][int(round(target))]
                s_weights[i] = weight
                return s_weights






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Jan 19 at 9:17









                KhajKhaj

                244213




                244213






























                    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%2f48173168%2fuse-both-sample-weight-and-class-weight-simultaneously%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

                    Homophylophilia

                    Updating UILabel text programmatically using a function

                    Cloud Functions - OpenCV Videocapture Read method fails for larger files from cloud storage