2

I have imbalanced dataset with 2 classes. I am using categorical_crossentropy. I wonder about my code. Is it correct to use class_weight with categorical_crossentropy ?? If yes , does the class_weight applied only to training set or to the whole data??

I have searched many times but I didn't find any helpful sites. Any help would be appreciated.

my code:

model.compile(loss='categorical_crossentropy', optimizer=opt_adam, metrics=['accuracy'])
history=model.fit_generator(generate_arrays_for_training(indexPat, train_data, start=0,end=100)
validation_data=generate_arrays_for_training(indexPat, test_data, start=0,end=100)
steps_per_epoch=int((len(train_data)/2)), 
                                validation_steps=int((len(test_data)/2)),
                                verbose=2,class_weight = {0:1, 1:1.181},
                                epochs=65, max_queue_size=2, shuffle=True)
Edayildiz
  • 545
  • 7
  • 16

2 Answers2

1

Yes, you can use the class weights with categorical cross entropy. The weights are applied when the loss function is calculated. Wrong classifications are penalized according to the weights. So the weights are applied neither to validation set nor to test set. The idea is then in training time model gives more attention to a class and updates the weights correspondingly.

That is why in the test or validation time, the learned weights will implicitly be biased with respect to class weights.

The only problem in your code might be the class weights. It can be that the weights have to add up to 1 but you should check the library details for this.

Berkay Berabi
  • 1,933
  • 1
  • 10
  • 26
  • you mean the wrong is the 1.181 of class 1 ? unfortunately I didn't find the library details page. can you send me the library page ? and I also searched about reference paper about class weight to reference it on my article. if you can help me on this I would be appreciate that – Edayildiz Jan 21 '21 at 20:58
  • https://keras.io/api/models/model_training_apis/#fit-method here is the link for fit method. It has to be equivalent to fit_generator. It seems that the class weights do not have to add up to 1. I do not know about a paper. I think using class weights does not necessarily require a citation. It is a pretty common thing that has been always used. – Berkay Berabi Jan 21 '21 at 21:44
  • thank you for your help i appreciate that. OK if I have 3808 samples for class 0 and 1291 samples for class 1 what should I write in `class_weight` ? , I think it should be `class_weight={0:1, 1:1.95}` , it is correct ?? – Edayildiz Jan 23 '21 at 15:40
  • Can you tell me how you arrive at 1.95? – Berkay Berabi Jan 24 '21 at 11:20
  • ‘1.95*1291=2517’ that is the difference between samples then after this weight the samples of class 1 should be 1291+2517=3808 . Is that correct ?? – Edayildiz Jan 24 '21 at 11:44
  • No this is wrong. Important thing is the ratio between number of samples not the absolute difference. Consider another case: 0: 2000 1: 1000. With your approach the weight for class 1 would be 1, that does not make a difference. There are twice many samples in class 0 as in 1. So the weight for class 1 should be double of the class 0's weight. In your case, 3808/1291 = 2.94 and it should be your class weight. It is then as if you have 3808 samples for both of classes. – Berkay Berabi Jan 24 '21 at 23:19
1

I do not have enough reputation to comment, but since you have asked for a reference paper, here is a newly published paper on dynamically adjusted class weights and class imbalance. https://ieeexplore.ieee.org/document/9324926

Dushi Fdz
  • 161
  • 3
  • 22