Install PyTorch on Windows

foreword

This document is suitable for installing PyTorch on Windows. The prerequisite for installing PyTorch by referring to this document is that the conda environment has been installed by referring to the following video.

Link: https://pan.baidu.com/s/1pWVPMc2vysfEPxLRCV_UNw?pwd=3pda Extraction code: 3pda

See the link for the MacOS installation process .

CPU version installation

installation steps

  1. Create a new conda environment
conda create -n torch_39 python=3.9
conda activate torch_39

Please add a picture description
Please add a picture description

  1. Install torch with pip command
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple torch torchvision torchaudio

Please add a picture description

  1. Configuration of Jupyter Lab virtual environment

This step is to add this conda environment "torch_39" to the Kernel of Jupyter Lab

//安装ipykernel
conda install ipykernel 

//在ipykernel中安装当前环境
python -m ipykernel install --name torch_39

conda deactivate

Please add a picture description
At this time, open Jupyter Lab to switch Kernel, and the "torch_39" conda environment just installed has appeared.

Please add a picture description

Test whether the CPU version of PyTorch is successfully installed

Please add a picture description

GPU version installation

Create a new conda environment

conda create -n torch_GPU python=3.9
conda activate torch_GPU

Note:

The cuda version of the old machine used in the laboratory test here is 11.1, and the graphics card is 730

Please add a picture description

install torch

Method 1: Online installation (recommended usage 2)

Install version 11.0:

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

Please add a picture description

Method 2: Download torch and torchvision and install them locally (this method is recommended)

Pay special attention to the correspondence between torch, torchvision, and torchaudio versions

Take a look at "five" in reference article 1 here

Reference article 2: Correspondence between torch, torchvision, and torchaudio versions in PyTorch

If the download is slow, here is the VPN download:

Please add a picture description
Please add a picture description
Switch to the folder and install with the command:


pip install torch-1.8.1+cu111-cp39-cp39-win_amd64.whl

pip install ...

Configuration of Jupyter Lab virtual environment

This step is to add this conda environment "torch_GPU" to the Kernel of Jupyter Lab

//安装ipykernel
conda install ipykernel 

//在ipykernel中安装当前环境
python -m ipykernel install --name torch_GPU

conda deactivate

At this time, open Jupyter Lab to switch the Kernel, and the "torch_GPU" conda environment just installed has appeared.

Please add a picture description

Test whether the installation is successful

import torch

# 验证PyTorch是否安装成功
print("PyTorch version:", torch.__version__)

# 验证CUDA是否可用
print("CUDA available:", torch.cuda.is_available())

# 如果CUDA可用,打印CUDA版本和设备信息
if torch.cuda.is_available():
    print("CUDA version:", torch.version.cuda)
    print("CUDA device name:", torch.cuda.get_device_name())

Run the screenshot:

Please add a picture description

Guess you like

Origin blog.csdn.net/Waldocsdn/article/details/131979883