Loss function that penalizes uncommon values (frequency-based)

I would like to make a loss function that assigns a loss based on the frequency each item appears.
For example if it gets a tensor [1,6,1,2,4,9,4,4] where 1 appears twice and 4 three times, the loss could be (1/2 + 1/1 + 1/2 + 1/1 + 1/3 + 1/1 + 1/3 + 1/3) / 8 = 0,625 , whereas [1,2,3,4,5,6,7,8] would have a loss of 1.
I have been struggling with this idea and how it could be implemented in a way that works with backpropagation. Does it seem feasible and what should I look into?