Not sure but I do the same. 1/frequency is good. You can also do something like 1/softmax(frequency). This one will not break if there is a class that does not exist in dataset (ie 1/frequency -> inf) or extremely infrequent. Depends on your requirements. BTW I never tested it, just made it up.