Reputation: 555
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)
Upvotes: 2
Views: 278
Reputation: 151
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
Upvotes: 1
Reputation: 2338
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.
Upvotes: 1