I'd like to use class_weight argument in keras model.fit to handle the imbalanced training data. By looking at some documents, I understood we can pass a dictionary like this:
class_weight = {0 : 1,
1: 1,
2: 5}
(In this example, class-2 will get higher penalty in the loss function.)
The problem is that my network's output has one-hot encoding i.e. class-0 = (1, 0, 0), class-1 = (0, 1, 0), and class-3 = (0, 0, 1).
How can we use the class_weight for one-hot encoded output?
By looking at some codes in Keras, it looks like _feed_output_names
contain a list of output classes, but in my case, model.output_names
/model._feed_output_names
returns ['dense_1']
Related: How to set class weights for imbalanced classes in Keras?
I guess we can use sample_weights
instead. Inside Keras, actually, class_weights
are converted to sample_weights
.
sample_weight: optional array of the same length as x, containing weights to apply to the model's loss for each sample. In the case of temporal data, you can pass a 2D array with shape (samples, sequence_length), to apply a different weight to every timestep of every sample. In this case you should make sure to specify sample_weight_mode="temporal" in compile().
sample_weights
is used to provide a weight for each training sample. That means that you should pass a 1D array with the same number of elements as your training samples (indicating the weight for each of those samples)