-
默認只有葉子節點才保持梯度,如:
1
A = Variable(torch.ones(2), requires_grad = True)
-
在中間節點可以設置保持梯度,如:
1 2 3 4 5 6 7 8 9 10 11 12
A = Variable(torch.ones(2), requires_grad = True) B = A*2 B.retain_grad() C = B.norm() C.backward() print B.grad #outputs Variable containing: 0.7071 0.7071 [torch.FloatTensor of size 2]
也可以設置hook輸出梯度:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
A = Variable(torch.ones(2), requires_grad = True) B = A*2 def show_grad(grad): print(grad) B.register_hook(show_grad) C = B.norm() C.backward() #... #grad show be auto displayed after grad of B is generated #outputs Variable containing: 0.7071 0.7071 [torch.FloatTensor of size 2]
-
每一個非葉子節點都會保存其創建,如:
1 2 3
# not B.creator now, ref: https://github.com/pytorch/tutorials/pull/91/files print B.grad_fn <MulBackward0 object at 0x7f3b0536f710>
-
早期PyTorch的執行模型參見:function.py、engine.py
-
backward只能由結果是標量的operator執行,比如:nn.CrossEntropyLoss。原因暫不明。
-
Variable包含一些Inplcae操作(其requires_grad不能爲True),均以下劃線“_”結尾,如:
1 2 3 4 5 6 7 8
A = Variable(torch.ones(2), requires_grad = False) A.fill_(3.0) #outputs Variable containing: 3 3 [torch.FloatTensor of size 2]
-
Storage與Tensor的概念:Storage是實際內存(1維),Tensor是對Storage的信息描述。
-
自動求導原理: autograd
-
設置CUDA同步執行,設置環境變量:CUDA_LAUNCH_BLOCKING=1,或者使用copy_強制同步
-
PyTorch多進程作用和使用
-
PyTorch的模型存取,2種方式:
1.讀取參數1 2 3 4 5 6
#save model torch.save(the_model.state_dict(), PATH) #load model the_model = TheModelClass(*args, **kwargs) the_model.load_state_dict(torch.load(PATH))
2.讀取模型
1 2 3 4 5
#save model torch.save(the_model, PATH) #load model the_model = torch.load(PATH)
-
CrossEntropyLoss與NLLLoss不同。前者是Softmax與CrossEntropy的組合,在Loss之前不要再加Softmax;後者只是CrossEntropy,需要在Loss之前加Softmax。
-
默認爲training狀態,在test時需要設置爲module.eval()或者module.train(False)
- 在訓練每個batch之前記得加model.train(),訓練完若干個iteration之後在驗證前記得加model.eval()。否則會影響dropout和BN。
- 用F.dropout()時一定要手動設參數self.training,正確用法:F.dropout(x, 0.2, self.training)。
- reference: https://www.zhihu.com/question/67209417/answer/303214329
-
Tensor.unsqueeze與Tensor.view作用類似,在某個地方插入一個維度(1)
-
Tensor.contiguous將Tensor內可能是不同的內存塊整理爲一塊連續的內存,如果本來就是連續的則不作操作。
PyTorch學習筆記 (一)
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.