Hi,
If you are getting nan, it means that you’re most likely dividing by 0 somewhere in your function. Can you check the different values to confirm that?
You can use .detach() but that will have a different effect: No gradient will flow back from the scale_factor. It depends on what you want to do here.