更新权重的表达式形式要注意:
w -= 0.01 * w.grad is an in-place operation, so it performs calculation on existing w and updates the value.
However, w = w - 0.01 * w.grad is not in-place operation, so it creates a new variable w, which does not have requires_grad set and so the error.
You can quickly check this by calling - print(w.requires_grad). In the first case you would get True wheres in the second case it would be False.
网友评论