以下是在Ubuntu上使用Anaconda搭配PyTorch的方法:
bash Anaconda3-xxx-Linux-x86_64.sh
安装,安装过程中可按需选择安装路径等选项,安装完成后需将Anaconda添加到系统环境变量,可通过编辑~/.bashrc
文件添加export PATH="/anaconda3/bin:$PATH"
,然后执行source ~/.bashrc
使配置生效。conda create -n pytorch_env python=3.x
命令创建,其中pytorch_env
是环境名称,3.x
是Python版本号,可根据需要选择。conda activate pytorch_env
命令激活创建好的虚拟环境。conda install pytorch torchvision torchaudio cudatoolkit=11.8 -c pytorch -c nvidia
,若不需要CUDA则去掉cudatoolkit
相关参数。python
进入Python交互环境,输入import torch
,再输入print(torch.__version__)
和print(torch.cuda.is_available())
,若显示版本号且cuda
可用则为安装成功。