第一步:
打开pytorch官网:https://pytorch.org/,选择 "Get Started";
第二步:
根据自身的需求,选择版本、操作系统、安装方式、语言、CUDA等;“Run this Command”部分会给出安装命令。
这是我的安装选择,直接在终端执行:pip3 install torch torchvision,即可。
第三步:
验证是否安装成功,执行命令如下:
from __future__ import print_function
import torch
x = torch.rand(5, 3)
print(x)
输出结果类似如下情况即成功。
tensor([[0.3380, 0.3845, 0.3217],
[0.8337, 0.9050, 0.2650],
[0.2979, 0.7141, 0.9069],
[0.1449, 0.1132, 0.1375],
[0.4675, 0.3947, 0.1426]])
第四步:
验证能在 GPU显卡和CUDA 上正确运行,输入:
import torch
torch.cuda.is_available()
返回 True 即成功。