pytorch 1.9.0 backward函数解释以及报错(RuntimeError: grad can be implicitly created only for scalar outputs)
生活随笔
收集整理的這篇文章主要介紹了
pytorch 1.9.0 backward函数解释以及报错(RuntimeError: grad can be implicitly created only for scalar outputs)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
文章目錄
- 官方文檔
- 簡單示例
- 示例1
- 示例2(報錯(RuntimeError: grad can be implicitly created only for scalar outputs)解決方法)
官方文檔
torch._tensor.Tensor def backward(self,gradient: Optional[Tensor] = None,retain_graph: Any = None,create_graph: Any = False,inputs: Any = None) -> Any Computes the gradient of current tensor w.r.t. graph leaves. The graph is differentiated using the chain rule. If the tensor is non-scalar (i.e. its data has more than one element) and requires gradient, the function additionally requires specifying gradient. It should be a tensor of matching type and location, that contains the gradient of the differentiated function w.r.t. self. This function accumulates gradients in the leaves - you might need to zero .grad attributes or set them to None before calling it. See Default gradient layouts for details on the memory layout of accumulated gradients. Note If you run any forward ops, create gradient, and/or call backward in a user-specified CUDA stream context, see Stream semantics of backward passes . 計算當前張量w.r.t.圖葉的梯度。 使用鏈規則區分圖形。如果張量是非標量(即其數據有多個元素)且需要梯度,則函數還需要指定梯度。它應該是一個匹配類型和位置的張量,包含微分函數w.r.t.self的梯度。 此函數用于在葉子中累積漸變-在調用它之前,可能需要將.grad屬性設置為零或將其設置為無。有關累積漸變的內存布局的詳細信息,請參見默認漸變布局。 注 如果在用戶指定的CUDA流上下文中運行任何向前操作、創建梯度和/或向后調用,請參閱向后過程的流語義。參數: gradient – Gradient w.r.t. the tensor. If it is a tensor, it will be automatically converted to a Tensor that does not require grad unless ``create_graph`` is True. None values can be specified for scalar Tensors or ones that don't require grad. If a None value would be acceptable then this argument is optional.梯度–梯度w.r.t.張量。如果它是張量,它將自動轉換為不需要梯度的張量,除非“create\u graph”為True。不能為標量張量或不需要梯度的張量指定任何值。 如果可以接受None值,則此參數是可選的。retain_graph – If ``False``, the graph used to compute the grads will be freed. Note that in nearly all cases setting this option to True is not needed and often can be worked around in a much more efficient way. Defaults to the value of ``create_graph``.retain_graph–如果“False”,用于計算梯度的圖形將被釋放。 請注意,在幾乎所有情況下,都不需要將此選項設置為True,而且通常可以以更有效的方式進行處理。 默認值為“創建圖形”。create_graph – If ``True``, graph of the derivative will be constructed, allowing to compute higher order derivative products. Defaults to ``False``.create_graph–若``True`,將構造導數圖,允許計算高階導數積。 默認值為“False”。inputs – Inputs w.r.t. which the gradient will be accumulated into ``.grad``. All other Tensors will be ignored. If not provided, the gradient is accumulated into all the leaf Tensors that were used to compute the attr::tensors. All the provided inputs must be leaf Tensors. 輸入–輸入w.r.t.,梯度將累積到“.grad”。 所有其他張量將被忽略。 如果未提供,梯度將累積到用于計算attr::張量的所有葉張量中。 所有提供的輸入必須是葉張量。< Python 3.8 >簡單示例
示例1
# -*- coding: utf-8 -*- import torch x = torch.tensor(2, dtype=torch.float, requires_grad=True) y = x * x print(y) y.backward() print(x.grad)y = x * x print(y) y.backward() print(x.grad)y = x * x print(y) y.backward() print(x.grad)z = 2*x print(z) z.backward() print(x.grad)z = 2*x print(x.grad)運行結果:
tensor(4., grad_fn=<MulBackward0>) tensor(4.) tensor(4., grad_fn=<MulBackward0>) tensor(8.) tensor(4., grad_fn=<MulBackward0>) tensor(12.) tensor(4., grad_fn=<MulBackward0>) tensor(14.) tensor(14.)進程已結束,退出代碼0可以看出:
1、當x設置requires_grad=True后,后面每一次用對象obj對x操作后,用obj調用backward函數,都會將這次操作的梯度疊加到x的grad屬性里
2、對x操作的對象obj可以有很多個obj1、obj2等等,只有某個obj操作后調用backward函數,才能把那次操作的梯度疊加到x的grad屬性里,否則不會疊加
示例2(報錯(RuntimeError: grad can be implicitly created only for scalar outputs)解決方法)
# -*- coding: utf-8 -*- import torch x = torch.tensor([2, 3, 4], dtype=torch.float, requires_grad=True) y = x * x print(y) # y.backward(torch.ones_like(x)) y.backward() print(x.grad)運行結果:
D:\Dontla_miniconda3.8\python.exe C:/Users/Administrator/Desktop/d2l/torch_code/code/test3.py tensor([ 4., 9., 16.], grad_fn=<MulBackward0>) Traceback (most recent call last):File "C:/Users/Administrator/Desktop/d2l/torch_code/code/test3.py", line 7, in <module>y.backward()File "D:\Dontla_miniconda3.8\lib\site-packages\torch\_tensor.py", line 255, in backwardtorch.autograd.backward(self, gradient, retain_graph, create_graph, inputs=inputs)File "D:\Dontla_miniconda3.8\lib\site-packages\torch\autograd\__init__.py", line 143, in backwardgrad_tensors_ = _make_grads(tensors, grad_tensors_)File "D:\Dontla_miniconda3.8\lib\site-packages\torch\autograd\__init__.py", line 50, in _make_gradsraise RuntimeError("grad can be implicitly created only for scalar outputs") RuntimeError: grad can be implicitly created only for scalar outputs進程已結束,退出代碼1提示報錯:RuntimeError: grad can be implicitly created only for scalar outputs
對于標量輸出,梯度只能被隱式創建
就是說使用.backward()的前提是x必須是標量,而不是向量,
如果x是向量,那么,backward函數里就要加參數
像這樣:
# -*- coding: utf-8 -*- import torch x = torch.tensor([2, 3, 4], dtype=torch.float, requires_grad=True) y = x * x print(y) # y.backward(torch.ones_like(x)) y.backward(torch.ones_like(y)) print(x.grad)運行結果:
D:\Dontla_miniconda3.8\python.exe C:/Users/Administrator/Desktop/d2l/torch_code/code/test3.py tensor([ 4., 9., 16.], grad_fn=<MulBackward0>) tensor([4., 6., 8.])進程已結束,退出代碼0參考文章:pytorch中backward()函數詳解
總結
以上是生活随笔為你收集整理的pytorch 1.9.0 backward函数解释以及报错(RuntimeError: grad can be implicitly created only for scalar outputs)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: (d2l-ai/d2l-zh)《动手学深
- 下一篇: pytorch torch.norm(i