indexes +=1 is in-place, final_entropy += entropy is inplace.
I dont think that is your issue, as both of these look like python numbers. However, a correction is:
final_entropy += entropy.item() # makes entropy a python number, instead of a pytorch scalar