I want different losses to have their gradients computed with respect to different variables, and those variables to then all step together.
Here’s a simple example demonstrating what I want:
import torch as T
x = T.randn(3, requires_grad = True)
y = T.randn(4, requires_grad = True)
z = T.randn(5, requires_grad = True)
x_opt = T.optim.Adadelta([x])
y_opt = T.optim.Adadelta([y])
z_opt = T.optim.Adadelta([z])
for i in range(n_iter):
x_opt.zero_grad()
y_opt.zero_grad()
z_opt.zero_grad()
shared_computation = foobar(x, y, z)
x_loss = f(x, y, z, shared_computation)
y_loss = g(x, y, z, shared_computation)
z_loss = h(x, y, z, shared_computation)
x_loss.backward_with_respect_to(x)
y_loss.backward_with_respect_to(y)
z_loss.backward_with_respect_to(z)
x_opt.step()
y_opt.step()
z_opt.step()
My question is how do we do that backward_with_respect_to
part in PyTorch? I only want x
's gradient w.r.t. x_loss
, etc… And then I want all the optimizers to step together (based on the current values of x
, y
, and z
).