Pytorch1.5.1版本安裝的方法步驟
查看自己cuda版本,
我的cuda是11版本了,所以可以安裝11版本以下的任何版本。
進(jìn)入pytorch官網(wǎng)
官網(wǎng)網(wǎng)址:https://pytorch.org/
2020年11月19號(hào),更新
最簡單的是直接按官網(wǎng)給的Run this Command命令,直接安裝,如下:
conda install pytorch torchvision torchaudio cudatoolkit=10.2 -c pytorch
解釋:-c pytorch,意思是從pytorch網(wǎng)站下載,速度感人,有辦法的那就方便多了。
按照上面圖這樣選擇,安裝pytorch有GPU加速的版本,安裝命令可以改下,后面加個(gè)豆瓣源,這樣下載速度快些。
pip install torch===1.5.1 torchvision===0.6.1 -f https://download.pytorch.org/whl/torch_stable.html -i https://pypi.douban.com/simple
或者直接用conda安裝,去掉后面的 -c pytorch
conda install pytorch torchvision cudatoolkit=10.2
如果上面方法都下載慢,那就按下面方法來。(適用于win版本,Linux的可以返回上一層尋找對應(yīng)的版本)
先進(jìn)清華源https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/pytorch/win-64/?C=M&O=D
下載對應(yīng)pytorch版本,我的是Python3.7,需要PyTorch-gpu版本,cuda需要10.2,找到對應(yīng)的bz2文件下載,如圖
還要下載對應(yīng)的torchvision===0.6.1,如圖
下載好就在命令行進(jìn)入你下載的路徑目錄里面安裝,并輸入下面代碼進(jìn)行離線安裝。
conda install --offline 對應(yīng)的安裝包文件名字
安裝完后還要安裝cudatoolkit=10.2
conda install cudatoolkit=10.2
然后運(yùn)行測試代碼:
# TEST import torch from torch.backends import cudnn x = torch.Tensor([1.0]) xx = x.cuda() print(torch.__version__) print(torch.version.cuda) print(torch.cuda.is_available()) print(xx) print(cudnn.is_acceptable(xx))
結(jié)果:
1.5.1
10.2
True
tensor([1.], device='cuda:0')
True
安裝成功!
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)) 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))
結(jié)果:
1.5.1
True
cpu 0.13901472091674805 tensor(140929.9688)
cpu 0.16696977615356445 tensor(140929.9688)
cuda:0 0.22500324249267578 tensor(141330.6875, device='cuda:0')
cuda:0 0.003974437713623047 tensor(141330.6875, device='cuda:0')
運(yùn)行兩次是cuda有個(gè)預(yù)熱的過程,第二次的時(shí)間明顯減少了。和CPU相比,更快。
自動(dòng)求導(dǎo)
代碼:
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 + c print('before:', a.grad, b.grad, c.grad) grads = autograd.grad(y, [a, b, c]) print('after :', grads[0], grads[1], grads[2])
結(jié)果:
before: None None None
after : tensor(2.) tensor(1.) tensor(1.)
可以看出pytorch比TensorFlow1.X好理解,適合人類思維,功能也都全。
到此這篇關(guān)于Pytorch1.5.1版本安裝的方法步驟的文章就介紹到這了,更多相關(guān)Pytorch1.5.1版本安裝內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python實(shí)現(xiàn)APP自動(dòng)化發(fā)微信群消息的示例代碼
本文主要介紹了Python實(shí)現(xiàn)APP自動(dòng)化發(fā)微信群消息的示例代,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下碼2022-01-01只需7行Python代碼玩轉(zhuǎn)微信自動(dòng)聊天
今天小編就為大家分享一篇關(guān)于只需7行Python代碼玩轉(zhuǎn)微信自動(dòng)聊天,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧2019-01-01如何用六步教會(huì)你使用python爬蟲爬取數(shù)據(jù)
網(wǎng)絡(luò)爬蟲就是按照一定規(guī)則自動(dòng)訪問互聯(lián)網(wǎng)上的信息并把內(nèi)容下載下來的程序或腳本,下面這篇文章主要給大家介紹了關(guān)于如何用六步教會(huì)你使用python爬蟲爬取數(shù)據(jù)的相關(guān)資料,需要的朋友可以參考下2022-04-04python 判斷參數(shù)為Nonetype類型或空的實(shí)例
今天小編就為大家分享一篇python 判斷參數(shù)為Nonetype類型或空的實(shí)例,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-10-10