PyTorch installation and environmentconfiguration
1. installation before 准备
in installationPyTorch之 before , 我们需要确保system已经installation了Pythonenvironment. PyTorchsupportPython 3.6及以 on version, 推荐usingPython 3.8 or 更 high version.
1.1 checkPythonversion
首先, 我们需要checksystemin is 否已installationPython以及Python version:
python --version # or 者 python3 --version
1.2 installationpip
pip is Python packagemanagementtool, 我们需要using它来installationPyTorch. such as果systemin还没 has installationpip, 可以按照以 under 步骤installation:
# Windowssystem python -m ensurepip --upgrade # macOS and Linuxsystem sudo apt install python3-pip # Ubuntu/Debian brew install python3-pip # macOS (usingHomebrew)
2. installationPyTorch
PyTorchproviding了 many 种installation方式, includingusingpip, condaetc.packagemanagementtool. 我们可以根据自己 operationsystem and 硬件environment选择合适 installation方式.
2.1 usingpipinstallation
usingpipinstallation is 最common 方式, 我们可以根据 is 否需要CUDAsupport选择不同 installationcommands:
2.1.1 installationCPUversion (不supportGPU加速)
pip install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cpu
2.1.2 installationCUDAversion (supportGPU加速)
such as果你 system has NVIDIA GPU, 并且installation了CUDA, 可以选择installationCUDAversion以获得GPU加速:
# CUDA 11.8 pip install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu118 # CUDA 12.1 pip install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu121
2.2 usingcondainstallation
such as果你usingAnaconda or Miniconda, 可以usingcondacommandsinstallationPyTorch:
2.2.1 installationCPUversion
conda install pytorch torchvision torchaudio cpuonly -c pytorch
2.2.2 installationCUDAversion
# CUDA 11.8 conda install pytorch torchvision torchaudio cudatoolkit=11.8 -c pytorch -c nvidia # CUDA 12.1 conda install pytorch torchvision torchaudio cudatoolkit=12.1 -c pytorch -c nvidia
3. verificationinstallation
installationcompletion after , 我们可以throughrun一个 simple Python脚本来verificationPyTorch is 否installation成功:
import torch
import torchvision
import torchaudio
print("PyTorch version:", torch.__version__)
print("TorchVision version:", torchvision.__version__)
print("TorchAudio version:", torchaudio.__version__)
print("CUDA available:", torch.cuda.is_available())
# such as果CUDA可用, 打印CUDAversion
if torch.cuda.is_available():
print("CUDA version:", torch.version.cuda)
print("Number of GPUs:", torch.cuda.device_count())
print("Current GPU:", torch.cuda.current_device())
print("GPU name:", torch.cuda.get_device_name(0))
4. creation虚拟environment
for 了避免依赖conflict, 推荐 in 虚拟environmentininstallationPyTorch. 我们可以usingvenv or condacreation虚拟environment.
4.1 usingvenvcreation虚拟environment
# creation虚拟environment python -m venv pytorch-env # 激活虚拟environment # Windows pytorch-env\Scripts\activate # macOS and Linux source pytorch-env/bin/activate # in 虚拟environmentininstallationPyTorch pip install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cpu
4.2 usingcondacreation虚拟environment
# creation虚拟environment conda create -n pytorch-env python=3.8 # 激活虚拟environment conda activate pytorch-env # in 虚拟environmentininstallationPyTorch conda install pytorch torchvision torchaudio cpuonly -c pytorch
5. commoninstallationissues及solution
5.1 CUDAversion不匹配
issues:
installationCUDAversion PyTorch after , runcode时出现CUDAversion不匹配 error.
solution:
确保installation PyTorchversion and systemininstallation CUDAversion匹配. 可以 in PyTorch官网查看support CUDAversion.
5.2 memory不足
issues:
installationPyTorch时出现memory不足 error.
solution:
尝试using--no-cache-dir选项installation, or 者增加system 虚拟memory.
pip install --no-cache-dir torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cpu
5.3 networkissues
issues:
installation过程in出现network连接超时 error.
solution:
可以尝试using国 in 镜像sources, or 者usingproxyserver.
# using国 in 镜像sources pip install torch torchvision torchaudio -i https://pypi.tuna.tsinghua.edu.cn/simple
6. 推荐 Developmentenvironment
除了installationPyTorch本身, 我们还推荐installation以 under tool来提升Developmentefficiency:
6.1 Jupyter Notebook/Lab
Jupyter is a 交互式计算environment, 非常适合for深度Learning实验:
pip install jupyter jupyterlab
6.2 常用 Pythonlibrary
in 深度Learningprojectin, 我们经常会用 to 以 under library:
pip install numpy pandas matplotlib scikit-learn tqdm
提示
for 了方便management依赖, 建议 in projectinusingrequirements.txtfile来记录所 has 依赖package及其version:
# 生成requirements.txt pip freeze > requirements.txt # from requirements.txtinstallation依赖 pip install -r requirements.txt
实践练习
练习1: installationPyTorch
根据你 operationsystem and 硬件environment, 选择合适 方式installationPyTorch. installationcompletion after , runverification脚本确认installation成功.
练习2: creation虚拟environment
usingvenv or condacreation一个 new 虚拟environment, 并 in 其ininstallationPyTorch and 必要 依赖package.
练习3: 解决installationissues
尝试故意using不匹配 CUDAversioninstallationPyTorch, 观察errorinformation, 然 after 解决这个issues.
7. summarized
本tutorial介绍了PyTorch installationmethod, including:
- usingpip and condainstallationPyTorch
- installationCPUversion and CUDAversion PyTorch
- creation and management虚拟environment
- 解决common installationissues
- 推荐 Developmentenvironment and tool
installationcompletion after , 你就可以开始usingPyTorchfor深度Learningproject Development了.