手动线性回归的第一个时期后,梯度消失了

时间:2019-01-06 19:08:51

标签: pytorch

我是Pytorch的新手,我一直在研究教程并研究玩具示例。我只想制作一个超级简单的模型来更好地处理autograd,但是我遇到了问题。

我正在尝试训练线性回归模型,但我一直遇到以下错误,

---------------------------------------------------------------------- 

RuntimeError                              Traceback (most recent call last)
<ipython-input-80-ba5ca34a3a54> in <module>()
      9   loss = torch.dot(delta, delta)
     10 
---> 11   loss.backward()
     12   with torch.no_grad():
     13     w, b = w - learning_rate*w.grad.data, b - learning_rate*b.grad.data

/usr/local/lib/python3.6/dist-packages/torch/tensor.py in backward(self, gradient, retain_graph, create_graph)
     91                 products. Defaults to ``False``.
     92         """
---> 93         torch.autograd.backward(self, gradient, retain_graph, create_graph)
     94 
     95     def register_hook(self, hook):

/usr/local/lib/python3.6/dist-packages/torch/autograd/__init__.py in backward(tensors, grad_tensors, retain_graph, create_graph, grad_variables)
     87     Variable._execution_engine.run_backward(
     88         tensors, grad_tensors, retain_graph, create_graph,
---> 89         allow_unreachable=True)  # allow_unreachable flag
     90 
     91 

RuntimeError: element 0 of tensors does not require grad and does not have a grad_fn

供参考,代码在这里,

# dataset for training
X = torch.randn(100, 3)
y = -3*X[:,0] + 2.2*X[:,1] + 0.002*X[:,2] + 1

w = torch.randn(3, requires_grad=True, dtype=torch.float)  # model weights
b = torch.randn(1, requires_grad=True, dtype=torch.float)  # model bias


num_epochs = 10
learning_rate = 1e-4

for i in range(num_epochs):

  y_hat = torch.mv(X, w) + b
  delta = y_hat - y
  loss = torch.dot(delta, delta)

  loss.backward()
  with torch.no_grad():
    w, b = w - learning_rate*w.grad, b - learning_rate*b.grad

问题似乎是在第一个时期之后,渐变属性设置为“无”,但是我有点困惑为什么会是这种情况。

如果我在更新权重后尝试将梯度归零,则会收到类似的错误消息。

1 个答案:

答案 0 :(得分:1)

答案在locally disabling gradient computation中。如您在第一个示例中看到的那样,使用torch.no_grad()上下文管理器执行的计算得出了requires_grad == False的张量。由于您创建的是“新鲜的” wb,而不是就地更新它们,因此这些张量在第一次迭代后会失去requires_grad属性,并且在第二次迭代时会出现错误。一个简单的解决方法是重新启用渐变

with torch.no_grad(): 
    w, b = w - learning_rate*w.grad, b - learning_rate*b.grad 
    w.requires_grad_(True) 
    b.requires_grad_(True) 

如果您在pytorch optim模块中查找优化器的来源,例如SGD,则会看到它们使用就地运算符,例如add_。您可以通过这种方式重写循环

with torch.no_grad(): 
    w.sub_(learning_rate*w.grad)
    b.sub_(learning_rate*b.grad)

它不会碰到requires_grad标志,因为张量保持其“身份”-只需更改值即可。在这种情况下,您将需要记住在每次迭代中都调用w.zero_grad()b.zero_grad(),否则梯度值将不断增加。