生活随笔
收集整理的這篇文章主要介紹了
pytorch教程龙曲良01-05
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
01深度學習框架與介紹
pytorch優(yōu)勢
1使用gpu加速
import torch
import time
print
(torch.__version__
)
print
(torch.cuda.is_available
())a
=torch.randn
(10000,1000
)
b
=torch.randn
(1000,2000
)
t0
=time.time
()
c
=torch.matmul
(a,b
)
t1
=time.time
()
print
(a.device,t1-t0,c.norm
(2
))
device
=torch.device
('cuda')
a
=a.to
(device
)
b
=b.to
(device
)t0
=time.time
()
c
=torch.matmul
(a,b
)
t2
=time.time
()
print
(a.device,t2-t0,c.norm
(2
))
t0
=time.time
()
c
=torch.matmul
(a,b
)
t2
=time.time
()
print
(a.device,t2-t0,c.norm
(2
))
1.7.1+cu110
True
cpu 0.4168832302093506 tensor
(141016.6719
)
cuda:0 4.1152663230896 tensor
(141416.5000, device
='cuda:0')
cuda:0 0.003988981246948242 tensor
(141416.5000, device
='cuda:0')
2自動求導 這樣可以實現(xiàn)對loss和w每次更新的求導
import torch
from torch
import autograd
x
=torch.tensor
(1.
)
a
=torch.tensor
(1.,requires_grad
=True
)
b
=torch.tensor
(2.,requires_grad
=True
)
c
=torch.tensor
(3.,requires_grad
=True
)
y
=a**2*x+b*x+cprint
('before:',a.grad,b.grad,c.grad
)
grads
=autograd.grad
(y,
[a,b,c
])
print
('after:',grads
[0
],grads
[1
],grads
[2
])
before: None None None
after: tensor
(2.
) tensor
(1.
) tensor
(1.
)
02開發(fā)環(huán)境的準備
本課程環(huán)境
1anaconda安裝
安裝后
cmd1conda list 2conda --version
看是否安裝成功
2安裝cuda
cuda官網
安裝后cmd
nvcc -V
檢查cuda是否安裝成功
如果顯示nvcc不存在此命令 配置環(huán)境
3pytorch的安裝
官網
管理員身份cmd 復制安裝命令
03線性回歸-1
x’=x-grad(x)
每次x’都是x減去對x的梯度(就是y’在x的結果) 每次將x’迭代求得取到最小值得X*,但是每次x’=x-lrgrad(x) ,lr是學習率,縮放倍數(shù),使得迭代的次數(shù)增加效果更好,這樣會使得梯度慢慢降低,
當x=5發(fā)現(xiàn)grad(x)=0,則x’=x-lrgrad(x)=5 所以x’還是5沒變化,但是因為有grad(x)有計算誤差所以x會在5附近徘徊
(之后求w和b同理也是利用梯度下降不斷找到符合最小值的w和b)
如果lr比較大,x’=x-lr*grad(x),就會使得步長比較大 x在最低點附近有大幅度的波動
一般lr設置為0.001,一般比較簡單的手寫數(shù)字數(shù)據(jù)集是設置的0.01,比較復雜新奇的算法探索不同的learning rate
如圖是不同的求解器,對公式做不同的優(yōu)化,考慮上一次的前進方向要不要和本次的保持一致,加了各種各樣約束就會得到各種不同的梯度下降求解器,導致求解速度更快求解精度更快
最經常用的是adam、sgd、msprop
可以求出精確解的算式是closed form solution
但是求出精確解的很少,實際問題一般是求近似解,證明近似解在經驗上可行就已經達到了目的了
噪聲:均值為0.01,方差為1的高斯分布
loss:求y和y_pre=wx+b之差平方的最小值,我們希望的是真實值y和預測值y_pred接近,這樣可以找到精確的w和b
04線性回歸2
如圖是100含有高斯噪聲的樣本點,我們希望找到一個模型(直線)使得點盡可能在線上,即loss=(wx+b+ε-y)^2 最小
可視化可以發(fā)現(xiàn)w=1,b=0的時候loss比較小,
但實際問題很難可視化,
初始化w=0,b=0,每一處對w和b進行求導,更新100次左右可以發(fā)現(xiàn)所求的wheb比較渡河理論值
linear regression:預測的值y是個連續(xù)的值取值范圍(負無窮,正無窮)或者是某個連續(xù)的實數(shù)空間也可以
已知xi和yi ,求xn的yn,yn是連續(xù)的就成為linear regression
logistics regression在linear regression增加壓縮函數(shù)(sigmod function),使得y的取值從實數(shù)范圍(負無窮,正無窮)變成[0,1],可以變成概率,適合二分類或分類問題。
05線性回歸實戰(zhàn)
loss對w,b的偏導如圖,之后不斷更新w和b可以套兩個公式
loss對w的偏導=2(wx+b-y)*x
loss對b的偏導=2(wx+b-y)*1
import numpy as np
def loss
(b,w,points
):totalloss
=0
for i
in range
(0,len
(points
)):x
=points
[i,0
]y
=points
[i,1
]totalloss+
=(y-
(w*x+b
))**2
return totalloss/float
(len
(points
))
def step_gradient
(b_current,w_current,points,lr
):b_gradient
=0w_gradient
=0N
=float
(len
(points
))for i
in range
(0,len
(points
)):x
=points
[i,0
]y
=points
[i,1
]b_gradient+
=-
(2/N
)*
(y-
(w_current*x+b_current
))w_gradient +
= -
(2 / N
) * x*
(y -
(w_current * x + b_current
))new_b
=b_current-lr*b_gradientnew_w
=w_current-lr*w_gradient
return [new_b,new_w
]
def gradient_descent_runner
(points,staring_b,staring_w,lr,num_iteration
):b
=staring_bw
=staring_w
for i
in range
(num_iteration
):b,w
=step_gradient
(b,w,np.array
(points
),lr
)return [b,w
]
def run
():points
=np.genfromtxt
('data.csv',delimiter
=',')lr
=0.0001initial_b
=0initial_w
=0num_iterations
=1000print
('Staring gradient descent at b={0},w={1},error={2}'.format
(initial_b,initial_w,loss
(initial_b,initial_w,points
)))print
('Runing...')[b,w
]=gradient_descent_runner
(points,initial_b,initial_w,lr,num_iterations
)print
('After {0} iteration b={1},w={2},error={3}'.format
(num_iterations,b,w,loss
(b,w,points
)))if __name__
=='__main__':run
()
Staring gradient descent at b
=0,w
=0,error
=5565.107834483211
Runing
...
After 1000 iteration b
=0.08893651993741346,w
=1.4777440851894448,error
=112.61481011613473
總結
以上是生活随笔為你收集整理的pytorch教程龙曲良01-05的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。