Search code examples
pythonpython-3.xneural-networkkeraskeras-layer

Keras class_weight for generators


This is a continuation of a question: https://datascience.stackexchange.com/questions/22814/class-weighting-during-validation-in-keras

class_weight can be used in Keras fit function to tell the optimizer to weight the under represented class. According to the answer in stackexchange it is also considered during validation. For example, if my class ratio is 10 negatives for every 1 positive then an accuracy score of 0.8 is not so good (a fixed classifier on negatives will do better). I have two questions:

  • How exactly class_weight is being considered during validation?
  • How can I use class_weight in fit_generator? is it the same parameter as in fit?

Solution

  • For your first question, it is considered the same way as during training.

    Basically if you look at the function weighted_masked_objective, individual samples are multiplied with the weights, and the mean is returned back.(Note: Keras does not automatically set the class weights, you need to pass the weights at model.fit() or model.fit_generator()

    Class weights can be computed by the inverse proportion to frequency. Using sklearn

    fit_generator is identical to fit, except for the fact it takes a generator as input.