How to use class weights in loss function for imbalanced dataset

Hi! How do I define the weights values? I have a dataset of ~112 000 images from chest xrays. There are only ~2000 images with nodules which are the ones I want to identify the most!
Thanks in advance.

I’ve found this:

self.weights = np.array([0 if i == 0 else 1 / i for i in counts])
self.weights = torch.DoubleTensor((self.weights)[self.labels])

or alternatively

self.weights = np.divide(1, counts, out=np.zeros_like(counts, dtype=np.float64), where=(counts!=0))
self.weights = torch.DoubleTensor((self.weights)[self.labels])

from: