Hi everyone!
I’m facing a problem using class_weight
parameter in Keras.
I have a InterleaveDataset composed of images and their labels, which are integers (but the problem is still about classification).
Also, I have a dictionary with the weights for each class, i.e. {16: 6.0, 18: 8.0, 21: 5.0}.
If I tried to use that dictionary, I get the error: Expected class_weight
to be a dict with keys from 0 to one less than the number of classes, found {16: 6.0, 18: 8.0, 21: 5.0}
I understand that error, but my problem is that I do not know the order of each class. I mean, I know that class 16 has a weight of 6, but if I try to write a dictionary in the way {0:weight, 1:weight…}, I do not know if the class 16 is the 0, or the 1…
Also, if I made a dictionary in that style, i.e. {0: 6.0, 1: 8.0, 2: 5.0} (that is bad because I do not know the order of each class), I get errors like: indices[0] = 18 is not in [0, 3)
How can I know the order of my dictionary, and not changing the numbers of my labels?
Thank you!.
Hi @alexgczs, As far as i know, You need to send class weights for all the classes using the class weights dictionary. You can calculate for the class weights using sklearn
from sklearn.utils import class_weight
class_weights = class_weight.compute_class_weight('balanced', np.unique(y_train), y_train)
or you can also calculate the class weights manually. Let say we have 2 classes one is positive and other is negative you can calculate the weights
weight_for_0 = (1 / neg) * (total / 2.0)
weight_for_1 = (1 / pos) * (total / 2.0)
class_weight = {0: weight_for_0, 1: weight_for_1}
Thank You.
Hi @Kiran_Sai_Ramineni , and thank you for your response. I already have the weights calculated, but my problem is another. Imagine I have 3 classes: class 16, class 18 and class 21. But I do not know the order that Keras will use classes, because I have a generator (from sample_from_datasets
function).
So my problems are:
- I do not know the order in which keras will order the class indices, so I do not know if the “class 0” in the dictionary is my class 16, 18 or 21.
- Another problem that I had is that I needed to keep my labels as integers, but it seems that it causes some problems to have the labels as integers if I want to use
class_weights
Thanks for your help!