I am implementation an algorithm of the following type:

at each iteration, the algorithm computes gradients of two objectives f and g with respect to the parameters, and combine them in some way, and then use the combined result as the effective gradient for SGD or Adam… (The combination of the gradients are not linear. So that I can’t just combine the objective f and g first and take a single gradient)

Is it true that the best way for me to do it is to compute the gradient of f by

opt.zero_grad()

f.backward()

and then clone all the grad attribute of the parameters

and then do the same thing for g

and then combine them and assign the .grad attributes with the new effective grad, and call the opt.step() at the end?

Is there any better way to do this?