jupyter notebook 使用pytorch-GPU版本
1-安装anaconda
2-安装pytorch
教程
如图所示安装成功
3-安装cuda 和cudnn
4-jupyter连接GPU使用pytorch
(1)安装nb_conda
#base环境下使用命令
conda install nb_conda_kernels
并重新启动notebook,在kernel -> change kernel中即可切换到指定的虚拟环境
(2)添加内核(上图中后面三个)
#切换到虚拟环境(使用anaconda新建)
#安装内核
pip install ipykernel
#本命令默认新内核名与虚拟环境名相同
(3) 使用新内核新建notebook
import torch
torch.cuda.is_available()
安装成功
5-测试代码
'''
Date: 2021-11-18 10:22:15
LastEditTime: 2021-11-18 10:24:06
'''
# Created on JemeryHua
import torch
import time
from torch import autograd
#GPU加速
print(torch.__version__)
print(torch.cuda.is_available())
a=torch.randn(10000,1000)
b=torch.randn(1000,10000)
print(a)
print(b)
t0=time.time()
c=torch.matmul(a,b)
t1=time.time()
print(a.device,t1-t0,c.norm(2))
device=torch.device('cuda')
print(device)
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))
t1=time.time()
print(a.device,t1-t0,c.norm(2))
device=torch.device('cuda')
print(device)
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))