安装PyTorch以及GPU上运行

本文介绍了如何在PyTorch中正确设置GPU环境,包括安装 torch、torchvision 和 cudatoolkit,如何将自定义参数、模型及运算结果适配CUDA,并通过device对象指定设备。重点讲解了如何将模型迁移到cuda并进行CPU与GPU间的转换操作。
摘要由CSDN通过智能技术生成

Pytorch:torch,torchvision,cudatoolkit 对应的版本

https://pytorch.org/get-started/previous-versions/

下载torch以及torchvision,toolkit网址

http://download.pytorch.org/whl/torch_stable.html

下载下来的whl文件
在这个目录下
pip install xxxxxx.whl

在GPU上执行程序
a.自定义的参数需要转化为cuda支持的tensor
b.model需要转化为cuda支持的model
c.执行的结果需要和cpu的tensor计算时
tensor.cpu()把cuda的tensor转化为cpu的tensor

#定义一个device对象
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')

例:将model定义在cuda上

my_linear = MyLinear().to(device)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值