# Thoughts on the Torch.autograd.backward in Pytroch

Source: Internet
Author: User
Tags scalar pytorch

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]```
Related Keywords:

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

## A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

• #### Sales Support

1 on 1 presale consultation

• #### After-Sales Support

24/7 Technical Support 6 Free Tickets per Quarter Faster Response

• Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.