PyTorch:安装和配置

安装

在官网选择即可[stable],老版本[previous-versions]

pytorch查看版本号

torch.__version__

pip自动选择安装

一般装了cuda就安装gpu版本的torch了

pip3 install torch torchvision -i https://pypi.tuna.tsinghua.edu.cn/simple

安装cpu版本torch

pip3 install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cpu

pip install torch==2.0.1+cpu

Note: ERROR: Could not find a version that satisfies the requirement torch==2.0.1+cpu
如果找不到可以通过:
pip install torch==2.0.1+cpu -f https://download.pytorch.org/whl/cpu/torch_stable.html
还安装不了可以从[https://download.pytorch.org/whl/cpu/torch_stable.html]里面下载[torch-2.0.1%2Bcpu-cp310-cp310-linux_x86_64.whl]

或者从[https://download.pytorch.org/whl/torch/]下载whl文件后再安装
pip install torch-2.0.1+cpu-cp310-cp310-linux_x86_64.whl
jupter上可能需要重启terminal,环境才会重新加载

不过貌似装了cuda的gpu环境,装cpu版本的就会出错,也不是重装依赖能解决的:
ImportError: cannot import name '_get_cpp_backtrace' from 'torch._C'

安装gpu版本torch

先安装cuda

wget https://developer.download.nvidia.com/compute/cuda/repos/ubuntu2204/x86_64/cuda-keyring_1.1-1_all.deb
sudo dpkg -i cuda-keyring_1.1-1_all.deb
sudo apt-get update
sudo apt-get -y install cuda

[CUDA Toolkit 12.2 Update 2 Downloads | NVIDIA Developer]

Note: macOS High Sierra 10.13.6之后NVIDIA不在对显卡驱动进行支持:[MacOS 10.13.6安装cuda+cudann+pytorch的gpu版本]

再安装torch

pip3 install torch torchvision torchaudio

pip install torch==2.0.1+cu117

linux上安装torch出错

ERROR: torch-2.0.1+cpu-cp310-cp310-linux_x86_64.whl is not a supported wheel on this platform.

首页uname -p查看是否是x86_64平台,再看是不是py版本没对上。

macos还需要安装

brew install libomp

否则出错:ImportError: dlopen(/...torch/_C.cpython-36m-darwin.so, 9): Library not loaded: /usr/local/opt/libomp/lib/libomp.dylib
  Referenced from: .../torch/lib/libshm.dylib  Reason: image not found

安装固定cuda版本对应的pytorch

pip install torch==1.7.1+cu92 torchvision==0.8.2+cu92 torchaudio==0.7.2 -f https://download.pytorch.org/whl/torch_stable.html

其它cuda版本对应的pytorch版本查询[https://download.pytorch.org/whl/torch_stable.html]
如cuda=9.0版本时,支持的最高pytorch版本可能就是1.1.0了:cu90/torch-1.1.0-cp36-cp36m-linux_x86_64.whl

[INSTALL PYTORCH][INSTALLING PREVIOUS VERSIONS OF PYTORCH]

显卡驱动版本查询

显卡驱动版本-cuda版本-pytorch版本需要一一对应,否则torch.cuda.is_available()=False。

$nvidia-smi    #Driver Version: 384.81
Mon Jan 11 14:51:48 2021       
+-----------------------------------------------------------------------------+
| NVIDIA-SMI 384.81                 Driver Version: 384.81                    |
|-------------------------------+----------------------+----------------------+
| GPU  Name        Persistence-M| Bus-Id        Disp.A | Volatile Uncorr. ECC |
| Fan  Temp  Perf  Pwr:Usage/Cap|         Memory-Usage | GPU-Util  Compute M. |
|===============================+======================+======================|
|   0  Tesla P100-PCIE...  On   | 00000000:04:00.0 Off |                    0 |
| N/A   33C    P0    25W / 250W |      0MiB / 16276MiB |      0%      Default |
+-------------------------------+----------------------+----------------------+
|   1  Tesla P100-PCIE...  On   | 00000000:81:00.0 Off |                    0 |
| N/A   28C    P0    25W / 250W |      0MiB / 16276MiB |      0%      Default |
+-------------------------------+----------------------+----------------------+
                                                                               
+-----------------------------------------------------------------------------+
| Processes:                                                       GPU Memory |
|  GPU       PID   Type   Process name                             Usage      |
|=============================================================================|
|  No running processes found                                                 |
+-----------------------------------------------------------------------------+

cuda(toolkit)版本查询

nvcc --version或nvcc -V,如果 nvcc 没有安装,那么用

$cat /usr/local/cuda/version.txt

CUDA Version 9.0.176

查看pytorch对应cuda的版本

import torch

print(torch.version.cuda)

查看 cuDNN 版本
cat /usr/local/cuda/include/cudnn.h | grep CUDNN_MAJOR -A 2
如果没有,那么可能没有安装 cuDNN。

Note: NVIDIA cuDNN是用于深度神经网络的GPU加速库。

驱动版本和cuda(toolkit)版本对应关系

Table 2. CUDA Toolkit and Compatible Driver Versions
CUDA ToolkitLinux x86_64 Driver VersionWindows x86_64 Driver Version
CUDA 11.2.0 GA>=460.27.04>=460.89
CUDA 11.1.1 Update 1>=455.32>=456.81
CUDA 11.1 GA>=455.23>=456.38
CUDA 11.0.3 Update 1>= 450.51.06>= 451.82
CUDA 11.0.2 GA>= 450.51.05>= 451.48
CUDA 11.0.1 RC>= 450.36.06>= 451.22
CUDA 10.2.89>= 440.33>= 441.22
CUDA 10.1 (10.1.105 general release, and updates)>= 418.39>= 418.96
CUDA 10.0.130>= 410.48>= 411.31
CUDA 9.2 (9.2.148 Update 1)>= 396.37>= 398.26
CUDA 9.2 (9.2.88)>= 396.26>= 397.44
CUDA 9.1 (9.1.85)>= 390.46>= 391.29
CUDA 9.0 (9.0.76)>= 384.81>= 385.54
CUDA 8.0 (8.0.61 GA2)>= 375.26>= 376.51
CUDA 8.0 (8.0.44)>= 367.48>= 369.30
CUDA 7.5 (7.5.16)>= 352.31>= 353.66
CUDA 7.0 (7.0.28)>= 346.46>= 347.62

[官方对照表][nvidia显卡,驱动以及cuda版本对应查询]

[pytorch版本,cuda版本,系统cuda版本查询和对应关系]

from: -柚子皮-

ref: 
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值