0

I try to train a neural network with 12 features in input and 3 possible outputs.

These 3 classes are not balanced, I tried to train my neural network with balanced weight based on the occurrence of each class. So I compute the number of occurrence of each class in my training set

w = torch.Tensor(df_train["type"].values)
Counter(w.numpy())

This outputs me the following:

Counter({1.0: 20164, 0.0: 25828, 2.0: 13250})

And so, the class weights will be computed as:

class_weights=compute_class_weight(class_weight='balanced',classes= np.unique(w),y=w.numpy())
class_weights=torch.tensor(class_weights,dtype=torch.float)

which gives the following weights:

tensor([0.7646, 0.9793, 1.4904])

to be able to input it into the criterion:

criterion = torch.nn.CrossEntropyLoss(weight=class_weights)

The thing is that there is one class (class 1) that I really want to classify correctly. So I prefer the 2 other classes 0 and 2 to be mislabeled as class 1 than the opposite. Of course I don't want to classify everything as class 1 and have a precision of 0 for the 2 other classes.

Therefore what I tried is to give myself the different weights as:

class_weights=torch.tensor([1., 5., 1.])

But I wonder if there is a better way to do such a thing ? Thank you for your help.

Mapoulet
  • 1
  • 1

0 Answers0