class weights formula for imbalanced dataset
I really don't suggest Under/Oversampling as it would change the distribution of dataset. we should consider distribution as a useful feature of dataset. so I think the weighted loss would have better performance in most cases. if you're using TF/Keras, this link would be useful. you can use a variety of loss functions, like the below one, to apply the weight.
tf.nn.weighted_cross_entropy_with_logits(
    labels, logits, pos_weight, name=None
)


A value pos_weight &gt; 1 decreases the false negative count, hence increasing the recall. Conversely setting pos_weight &lt; 1 decreases the false positive count and increases the precision.