Autograd:自動微分

來源:互聯網
上載者:User

標籤:obj   data   highlight   rom   import   back   ORC   運行   簡單   

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用來反向傳播計算輸入的梯度。
具體代碼解析 
  1. #_Author_:Monkey  
  2. #!/usr/bin/env python  
  3. #-*- coding:utf-8 -*-  
  4. import torch as t  
  5. from  torch.autograd import Variable  
  6.   
  7. x = Variable(t.ones(2,2),requires_grad = True)  
  8. print(x)  
  9. ‘‘‘‘‘tensor([[1., 1.], 
  10.         [1., 1.]], requires_grad=True)‘‘‘  
  11. y = x.sum()  
  12. print(y)  
  13. ‘‘‘‘‘tensor(4., grad_fn=<SumBackward0>)‘‘‘  
  14. print(y.grad_fn)    #指向一個Function對象,這個Function用來反向傳播計算輸入的梯度  
  15. ‘‘‘‘‘<SumBackward0 object at 0x000002D4240AB860>‘‘‘  
  16. y.backward()  
  17. print(x.grad)  
  18. ‘‘‘‘‘tensor([[1., 1.], 
  19.         [1., 1.]])‘‘‘  
  20. y.backward()  
  21. print(x.grad)  
  22. ‘‘‘‘‘tensor([[2., 2.], 
  23.         [2., 2.]])‘‘‘  
  24. y.backward()  
  25. print( x.grad )  
  26. ‘‘‘‘‘tensor([[3., 3.], 
  27.         [3., 3.]])‘‘‘  
  28. ‘‘‘‘‘grad在反向傳播過程中時累加的(accumulated),這意味著運行 
  29. 反向傳播,梯度都會累加之前的梯度,所以反向傳播之前需要梯度清零‘‘‘  
  30. print( x.grad.data.zero_() )  
  31. ‘‘‘‘‘tensor([[0., 0.], 
  32.         [0., 0.]])‘‘‘  
  33.   
  34. y.backward()  
  35. print( x.grad )  
  36. ‘‘‘‘‘tensor([[1., 1.], 
  37.         [1., 1.]])‘‘‘  
  38.   
  39. m = Variable(t.ones(4,5))  
  40. n = t.cos(m)  
  41. print(m)  
  42. print(n)  
  43. ‘‘‘‘‘tensor([[1., 1., 1., 1., 1.], 
  44.         [1., 1., 1., 1., 1.], 
  45.         [1., 1., 1., 1., 1.], 
  46.         [1., 1., 1., 1., 1.]]) 
  47. tensor([[0.5403, 0.5403, 0.5403, 0.5403, 0.5403], 
  48.         [0.5403, 0.5403, 0.5403, 0.5403, 0.5403], 
  49.         [0.5403, 0.5403, 0.5403, 0.5403, 0.5403], 
  50.         [0.5403, 0.5403, 0.5403, 0.5403, 0.5403]])‘‘‘  
  51. m_tensor_cos = t.cos(m.data)  
  52. print(m_tensor_cos)  
  53. ‘‘‘‘‘ensor([[0.5403, 0.5403, 0.5403, 0.5403, 0.5403], 
  54.         [0.5403, 0.5403, 0.5403, 0.5403, 0.5403], 
  55.         [0.5403, 0.5403, 0.5403, 0.5403, 0.5403], 
  56.         [0.5403, 0.5403, 0.5403, 0.5403, 0.5403]])‘‘‘  

Autograd:自動微分

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.