Autograd: Automatic differential

Source: Internet
Author: User

Autograd
1、深度学习的算法本质上是通过反向传播求导数,Pytorch的Autograd模块实现了此功能;在Tensor上的所有操作,Autograd都能为他们自动提供微分,避免手动计算导数的复杂过程。2、autograd.Variable是Autograd中的核心类,它简单的封装了Tensor,并支持几乎所有Tensor操作;Tensor被封装为Variable之后,可以调用它的.backward()实现反向传播,自动计算所有的梯度。3、Variable主要包含三个属性:        data:保存Variable所包含的Tensor;        grad:保存data对应的梯度,grad也是个Variable,而不是Tensor,它和data的形状一样;        grad_fn:指向一个Function对象,这个Function用来反向传播计算输入的梯度。
Specific code parsing
  1. #_Author_: Monkey
  2. #!/usr/bin/env python
  3. #-*-Coding:utf-8-*-
  4. Import Torch as T
  5. From Torch.autograd import Variable
  6. x = Variable (T.ones (2,2), Requires_grad = True)
  7. Print (x)
  8. "'"tensor ([[1., 1.],
  9. [1., 1.]], requires_grad=true) "
  10. y = X.sum ()
  11. Print (y)
  12. "'tensor (4., grad_fn=<sumbackward0>) '
  13. Print (Y.GRAD_FN) #指向一个Function对象 This function is used to reverse-propagate the gradient of the computed input
  14. "'<sumbackward0 object at 0x000002d4240ab860> '
  15. Y.backward ()
  16. Print (X.grad)
  17. "'"tensor ([[1., 1.],
  18. [1., 1.]]
  19. Y.backward ()
  20. Print (X.grad)
  21. "'"tensor ([[2., 2.],
  22. [2., 2.]]
  23. Y.backward ()
  24. Print (X.grad)
  25. "'"tensor ([[3., 3.],
  26. [3., 3.]]
  27. "'"grad in the process of reverse propagation (accumulated), which means running
  28. Reverse propagation, the gradient will accumulate before the gradient, so the reverse propagation requires a gradient clear 0 "
  29. Print (X.grad.data.zero_ ())
  30. "'"tensor ([[0., 0.],
  31. [0., 0.]]
  32. Y.backward ()
  33. Print (X.grad)
  34. "'"tensor ([[1., 1.],
  35. [1., 1.]]
  36. m = Variable (T.ones (4,5))
  37. n = T.cos (m)
  38. Print (m)
  39. Print (n)
  40. "'"tensor ([[1., 1., 1., 1., 1.],
  41. [1., 1., 1., 1., 1.],
  42. [1., 1., 1., 1., 1.],
  43. [1., 1., 1., 1., 1.]]
  44. Tensor ([[0.5403, 0.5403, 0.5403, 0.5403, 0.5403],
  45. [0.5403, 0.5403, 0.5403, 0.5403, 0.5403],
  46. [0.5403, 0.5403, 0.5403, 0.5403, 0.5403],
  47. [0.5403, 0.5403, 0.5403, 0.5403, 0.5403]] "
  48. M_tensor_cos = T.cos (m.data)
  49. Print (M_tensor_cos)
  50. "'"Ensor ([0.5403, 0.5403, 0.5403, 0.5403, 0.5403],
  51. [0.5403, 0.5403, 0.5403, 0.5403, 0.5403],
  52. [0.5403, 0.5403, 0.5403, 0.5403, 0.5403],
  53. [0.5403, 0.5403, 0.5403, 0.5403, 0.5403]] "

Autograd: Automatic differential

Contact Us

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.