I am having the same issue, however, my problem is how to implement this across an entire batch. Do you have any suggestions for how to implement this an entire batch, while avoiding having to loop over each sample?