In pytorch, the corresponding functions are:

f(x; w) = ...
dfdx = autograd.grad(f, x, df, create_graph=True)
loss = sum_of_error(dfdx, dfdx_true)

optimizer.zero_grad() then loss.backward()

optimizer.ste()