When I ran the following code, I got an error message:
import torch
from torch.autograd import Variable
tensor = torch.FloatTensor([[1,2],[3,4]])
variable_false = Variable(tensor) # can't compute gradients
variable_true = Variable(tensor, requires_grad=True)
# tensor operations
t_out = torch.mean(tensor*tensor)
# variable operations
v_out_false = torch.mean(variable_false*variable_false)
v_out_true = torch.mean(variable_true*variable_true)
# backpropagation
v_out_false.backward()
RuntimeError: there are no graph nodes that require computing gradients
I know I should added requires_grad=True
, but here my questions are:
- the
graph
is computational graph for calculating gradients, right? - how can such
graph
and its nodes be accessed?
Thanks!