Specify network to not backpropagate the gradients in a given function
In my neural network, in order to compute the loss, I need to do some intermediate computations during training to first obtain some transformations rv.

rv = factor.ransac(source, target, prob, device)
predicted = factor.predict(source, rv, outputs, device)
loss = criterion(predicted, target)


I want to backpropagate the gradients only through predicted but not through factor.ransac. How do I do this?

You can use detach().

rv = factor.ransac(source, target, prob, device).detach()


That way, gradients on rv will be discarded.
