Using the C++ frontend, during training I compute a loss tensor with a construction similar to
auto x=module->forward(inputs);
auto loss=loss_function->compute(x,targets);
if(loss.requires_grad()) { // tests to true
loss.backward();
}
This is being done in a closure passed to an optimizer; this closure then returns the loss.
I wanted to preserve the gradient at this point for diagnostic purposes. I attempt to extract the gradient of the loss at this point using something similar to
grads=loss.grad().clone(); // with or without the clone()
In all cases, this seems to resolve to a gdb description of grads of the form
{impl_ = {target_ = some_address c10::UndefinedTensorImpl::_singleton}}
Any time I try to use this, I get a throw of the form
Expected a Tensor of type Variable but found an undefined Tensor…
Note that if I don’t try to grab the gradient at this or any point, the optimization proceeds just fine, so the necessary gradients are indeed being made available to the optimizer.
I am probably missing something about the semantics and/or proper use of the .grad() method. My understanding is that, after .backward(), this should return a “mutable reference” to the gradient of the tensor with respect to all the weights. (I’m hoping this curious phrasing doesn’t imply that it is “write only” and can only be used as the target of assignments.)
There is a const Tensor& grad() method prototype as well, which I have also tried and used in a successful compilation; the description of the action of this in the source is a little bit ambiguous. The behavior is essentially the same.
This has probably come up before, but what is the “standard” approach to grabbing loss gradients for future inspection?
Thanks,
Eric