The inverse transfer law is the most important part of deep learning, and the backward in torch can calculate and accumulate gradients in the calculation diagram.
Here is a program to demonstrate basic backward operations and areas to note
>>> Import Torch >>> from Torch.autograd import Variable >>> x = Variable (Torch.ones (2,2), req uires_grad=true) >>> y = x + 2 >>> y.grad_fn out[6]: <torch.autograd.function.addconstantbackward at 0x229e7068138> >>> Y.grad >>> z = y*y*3 >>> z.grad_fn out[9]: <torch.autograd.function. Mulconstantbackward at 0x229e86cc5e8> >>> z out[10]: Variable containing:27 [Torch. Floattensor of size 2x2] >>> out = Z.mean () >>> out.grad_fn out[12]: <torch.autograd.function.meanba Ckward at 0x229e86cc408> >>> Out.backward () # This is because out is scalar scalars, so parameters do not need to be filled in >>> X.grad out[19]: Var Iable containing:4.5000 4.5000 4.5000 4.5000 [Torch. Floattensor of size 2x2] >>> out # for scalar out[20]: Variable containing:27 [Torch. Floattensor of size 1] >>> x = Variable (torch. Tensor ([2,2,2]), requires_grad=true) >>> y = x*2 >>> y OUT[52]: Variable containing:4 4 4 [Torch. Floattensor of size 3] >>> Y.backward () # Because the Y output is non-scalar, the gradient of the elements between the vectors needs to be labeled with the elements of the same length, labeled Traceback (most recent CA LL last): File ' C:\Users\dell\Anaconda3\envs\my-pytorch\lib\site-packages\IPython\core\interactiveshell.py ', line 2862, in Run_code exec (code_obj, Self.user_global_ns, Self.user_ns) File "<ipython-input-53-95acac9c3254>", Li NE 1, in <module> Y.backward () File "C:\Users\dell\Anaconda3\envs\my-pytorch\lib\site-packages\torch\autograd\ variable.py ", line 156, in backward Torch.autograd.backward (self, gradient, retain_graph, Create_graph, retain_variabl ES) File "C:\Users\dell\Anaconda3\envs\my-pytorch\lib\site-packages\torch\autograd\__init__.py", line +, in Backward Grad_variables, create_graph = _make_grads (variables, grad_variables, create_graph) File "C:\Users\dell\Ana conda3\envs\my-pytorch\lib\site-packages\torch\autograd\__init__.py ", line, _make_grads raise RuntimeError ("Grad can implicitly created only for scalar outputs ") Runtimeerror:grad can is implicitly created only for scalar OUTP UTS >>> Y.backward (torch. Floattensor ([0.1, 1, ten])) >>> X.grad #注意这里的0.1,1.10 for gradient evaluation out[55]: Variable containing:0.200 0 2.0000 20.0000 [torch. Floattensor of size 3] >>> y.backward (torch. Floattensor ([0.1, 1, ten])) >>> X.grad # Gradient Cumulative out[57]: Variable containing:0.4000 4.0000 40. 0000 [Torch. Floattensor of size 3] >>> x.grad.data.zero_ () # gradient accumulation to clear 0 Out[60]: 0 0 0 [torch. Floattensor of size 3] >>> X.grad # Cumulative empty out[61]: Variable containing:0 0 0 [Torch. Floattensor of size 3] >>> y.backward (torch. Floattensor ([0.1, 1, ten])) >>> X.grad out[63]: Variable containing:0.2000 2.0000 20.0000 [Torch.
Floattensor of size 3]