logo
Loading...

執行 tensor 運算時,符號計算與以往不同? - Cupoy

在撰寫 Day3 作業時,我有一段代碼如下: W1 = W1 - W1.grad*learning...

執行 tensor 運算時,符號計算與以往不同?

2021/06/14 下午 05:33
使用 Pytorch 進行微分與倒傳遞
Chi-Kang Su
觀看數:37
回答數:1
收藏數:0

在撰寫 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) in 35 print(W1) 36 # 將紀錄的gradient清空(因為已經更新參數) ---> 37 W1.grad.zero_() 38 W2.grad.zero_() AttributeError: 'NoneType' object has no attribute 'zero_' ``` 但如果換成下述代碼就可以正常運行,這與我們平時的計算邏輯不大相同,請問為什麼會這樣呢? ```python W1 -= W1.grad*learning_rate W2 -= W2.grad*learning_rate # 將紀錄的gradient清空(因為已經更新參數) W1.grad.zero_() W2.grad.zero_() ```

回答列表

  • 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)