在Debian上配置PyTorch环境,可参考以下步骤:
系统准备
更新系统包:sudo apt update,sudo apt upgrade。安装Python和pip:sudo apt install python3 python3-pip。
创建虚拟环境(可选)
使用venv模块创建,如python3 -m venv pytorch_env,然后激活环境:source pytorch_env/bin/activate。
安装PyTorch
- 使用pip安装:若用CPU版本,命令为
pip install torch torchvision torchaudio;若用GPU版本,需根据CUDA版本选择,如CUDA 11.7版本可执行pip install torch torchvision torchaudio --extra-index-url https://download.pytorch.org/whl/cu117。 - 使用conda安装:先安装Miniconda或Anaconda,然后创建并激活环境,如
conda create -n pytorch_env python=3.8,conda activate pytorch_env。安装CPU版本可执行conda install pytorch torchvision torchaudio cpuonly -c pytorch;安装GPU版本可执行conda install pytorch torchvision torchaudio cudatoolkit=11.7 -c pytorch(需提前安装对应CUDA和cuDNN)。
验证安装
在Python环境中执行import torch,print(torch.__version__),print(torch.cuda.is_available())。若显示版本号且cuda.is_available()返回True(GPU版本),则安装成功。