執行 tensor 運算時,符號計算與以往不同?
在撰寫 Day3 作業時,我有一段代碼如下:
```python
W1 = W1 - W1.grad*learning_rate
W2 = W2 - W2.grad*learning_rate
W1.grad.zero_()
W2.grad.zero_()
```
這是在執行時會跳錯誤
```python
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
回答列表
-
2021/06/15 下午 08:17王健安贊同數:1不贊同數:0留言數:1
您好: 在pytorch中, 若想改變某個 tensor, 會遵從 in-place operation, 代表它不會計算完新的值後覆蓋到舊的值(W1 = W1 - learning_rate * W1.grad), 而是直接在舊的值做計算(W1 -= learning_rate * W1.grad), 另外, 若使用下圖的coding方法, 則兩種方法皆可使用。 參考資料: https://discuss.pytorch.org/t/nonetype-object-has-no-attribute-zero/61013 https://www.programmersought.com/article/68314894134/ https://blog.csdn.net/york1996/article/details/81835873 ![image](http://kwassistfile.cupoy.com/0000017A0F8D05E70000000D6375706F795F72656C65617365414E53/1623377370022/large)