A better way to handle class imbalance would be to pre-process the input data and post-process the results.
eg: if there are stopwords, which you don’t necessarily want in the results, get rid of them.
I’ve used this for temperature scaling, softmax = e^(z/T) / sum_i e^(z_i/T), to bring about variations in the output. Not sure if you can use it with CosineEmbeddingLoss.