Thanks for your response.
I have used bcewithlogitloss() and pos weight calculated as below
y_train=torch.Tensor(y_train)
num_positives = torch.sum(y_train, dim=0)
num_negatives = len(train_dataset) - num_positives
pos_weight = num_negatives / num_positives
pos_weight
For me no of 0s are very less than number of 1s .When I used this method or even with weighted random sampler for which I computed class weights as below
y_train_indices = train_dataset.indices
y_train = [target[i] for i in y_train_indices]
class_sample_count = np.array(
[len(np.where(y_train == t)[0]) for t in np.unique(y_train)])
print(class_sample_count)
weight = 1. / class_sample_count
samples_weight = np.array([weight[t] for t in y_train])
samples_weight = torch.from_numpy(samples_weight)
print(samples_weight)
sampler = torch.utils.data.sampler.WeightedRandomSampler(samples_weight.type(‘torch.DoubleTensor’), len(samples_weight))
There is no improvement in the number of 0 s of F1 score and is resulted as below ,
precision recall f1-score support
0 0.23 0.75 0.35 4
1 0.93 0.58 0.72 24
accuracy 0.61 28
macro avg 0.58 0.67 0.54 28
weighted avg 0.83 0.61 0.67 28
I ran for 3 epochs and both the methods were tried by substituting them in the loop of train data loader.
The validation loss is not decreasing as you see above for both the methods .
Hence I wanted to try now using cross entropy loss for which I am using the below ,
np.unique(train_dataset.indices)
y_train = [target[i] for i in y_train_indices]
np.unique(y_train)
class_wts = compute_class_weight(“balanced”,classes= np.unique(y_train),y=y_train)
class_wts
and substituting them in the cross entropy loss.
I have read many posts of this forum regarding the first two methods but in my case its overfitting .I am not sure as I have binary class with single label,the pos weight I have taken as a single value which is tensor 6.5 resulted from the above .
Kindly correct me if there is any step that needs to be corrected for all the 3 methods.
Thanks.