Hey all,
I am trying to utilise BCELoss with weights, but I am struggling to understand. I currently am using  LSTM model to detect an event in time-series data. My output from the model and true_output are as follows[batch_size, seq_length].
Currently, I think I have managed to do hard code it but it’s not the best way to achieve this.
         loss_get = self.criterion(predictions.float(), target.float())
            # Weighted Binary Corss_entropy Function
            loss_flat = loss_get.flatten()
            target_flat = target.flatten()
            loss_flat[target_flat == 1] *= self.pos_weight_factor
            loss = loss_flat.mean()
            loss.backward()

My datasets are imbalance, meaning that I do not have a constant length of the dataset as well as there are more 0’s than 1’s, approximately 100:1, hence I need to penalise the 0’s by multiplying it with an arbitrary number. I understand that there are a few topics on this, but I cannot quite get my head around it.How to apply a weighted BCE loss to an imbalanced dataset? What will the weight tensor contain? 2
Therefore If I wanted to apply weights with this using the built-in function or solution has suggested by this post. https://discuss.pytorch.org/t/solved-class-weight-for-bceloss/3114?u=ykukkim 2
Can anyone guide me through this?
Thanks!