Weight factor for the BCEwithlogitloss

I have unbalanced data and i want to use BCEwithLogitLoss with weight factor for minority classes. Total samples in each classes are,
samples = torch.FloatTensor([74891,11865,194148,98997,29350,104203,130637,30649,141300,164775,176567,16267,148950,1536,12022,22100,1566,67277,74877])
and I have devised a formula for the calculation of weight factor as max(1,log2(majority_class/class_samples)) which gives somehow the values which seems fine to me as well. but when I am trying to implement this with pytorch it gives me error as,

step1 = torch.log2(torch.max(samples)/samples)
step2 = torch.max(1,step1)

error at step2 as Dimension out of range (expected to be in range of [-1, 0], but got 1)

final weights should be
weights= torch.FloatTensor([1.37429258667969,4.03237283987156,1,1,2.7257244370681,1, 1,2.66324493724027,1,1,1,3.57713673021635,1,6.98183481923172, 4.01340801523125,3.13503857078672, 6.95392882266183,1.52897156569056,1.37456230692208])

Lastly this formula is fine? or can anyone suggest me any better formula or improvement to this? Thanks alot in advance,

Hy @Mughees , I hope I understand this correctly,
When I run this.

samples = torch.FloatTensor([74891,11865,194148,98997,29350,104203,130637,30649,141300,164775,176567,16267,148950,1536,12022,22100,1566,67277,74877])
a = torch.tensor(1).float() 
step1 = torch.log2(torch.max(samples)/samples)
step2 = torch.max(a,step1)
print(step2)
tensor([1.3743, 4.0324, 1.0000, 1.0000, 2.7257, 1.0000, 1.0000, 2.6632, 1.0000,
        1.0000, 1.0000, 3.5771, 1.0000, 6.9818, 4.0134, 3.1350, 6.9539, 1.5290,
        1.3746])

It gives the desired output.

2 Likes

I guess I was missing the part to convert 1 into tensors. Thanks alot.