2
$\begingroup$

I'm using validation data for hyperparameter optimization and am trying to use class weights. For model.fit(), there is an option to pass class weights (the argument is class_weight) to the function for test data, but I'd like to do the same to validation data.

Another user had the same question a while back but no satisfactory answers were given:

Class weighting during validation in Keras

When passing validation data as an argument to model.fit(), the validation data can take the form of a tuple: (x_val, y_val, val_sample_weights) but in the Keras documentation I'm not seeing any way to use class weights instead of sample weights.

Does anyone have any idea on how to use class weights with validation data in Keras?

Here is some info that might be helpful:

From the Keras documentation, description of the 'class_weight' argument: "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."

Here is a notebook from Francois Chollet which shows an example of using class_weight: https://colab.research.google.com/drive/1xL2jSdY-MGlN60gGuSH_L30P7kxxwUfM#scrollTo=REP1KlrSEx-m

Thanks for any help!

$\endgroup$
3
$\begingroup$

Class weights make sense only in the context of a loss function. When you validate your model you are making predictions and comparing to ground truth using a metric - but in that phase you aren't propagating back any changes, so weights are useless.

$\endgroup$
1
  • $\begingroup$ I am training my model in such a way that it saves the model instance where I got the lowest validation loss. I think class-weighting in validation step can help to lower the validation error. I am confused. $\endgroup$ – samra irshad Dec 11 '20 at 0:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.