2020年3月26日 星期四

PyTorch 中 weight 和 weight.data 的差異


當我們做完 backpropagation 得到 weight 和 bias 的 grad 之後,要進行 weight 和 bias 的更新時,有兩種不同的 coding style。

(1) 使用 torch.no_grad()
with torch.no_grad():
    weight -= learning_rate * weight.grad
    bias -= learning_rate * bias.grad

(2) 使用 weight.data 和 weight.grad.data
weight.data -= learning_rate * weight.grad.data
bias.data -= learning_rate * bias.grad.data

這兩種方法有何區別呢?根據 PyTorch 官方網站給的解釋:
Recall that tensor.data gives a tensor that shares the storage with tensor, but doesn't track history.
 tensor 和 tensor.data 共享同一個儲存空間,但是當我們使用 tensor.data 時,PyTorch 不會追蹤運算的歷史,也就是當我們進行 backward() 時,在 tensor.data 中不會進行 gradient 的計算。


Reference
https://pytorch.org/tutorials/beginner/examples_autograd/two_layer_net_autograd.html

沒有留言:

張貼留言