My understanding is that the class_weight regards the weights of all
classes for the entire dataset whereas the sample_weights regards the
weights of all classes for each individual chunk created by the
generator. Is that correct? If not, can someone elaborate on the
matter?
class_weight
affects the relative weight of each class in the calculation of the objective function. sample_weights
, as the name suggests, allows further control of the relative weight of samples that belong to the same class.
Is it necessary to give both the class_weight to the fit_generator and
then the sample_weights as an output for each chunk? If yes, then why?
If not then which one is better to give?
It depends on your application. Class weights are useful when training on highly skewed data sets; for example, a classifier to detect fraudulent transactions. Sample weights are useful when you don't have equal confidence in the samples in your batch. A common example is performing regression on measurements with variable uncertainty.
If I should give the sample_weights for each chunk, how do I map the
weights if some of the classes are missing from a specific chunk? Let
me give an example. In my overall dataset, I have 7 possible classes
(labels). Because these classes are highly imbalanced, when I create
smaller chunks of data as an output from the fit_generator, some of
the classes are missing from the specific chunk. How should I create
the sample_weights for these chunks?
This is not an issue. sample_weights
is defined on a per-sample basis and is independent from the class. For this reason, the documentation states that (inputs, targets, sample_weights)
should be the same length.
The function _weighted_masked_objective
in engine/training.py
has an example of sample_weights are being applied.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…