PyTocrch »

My Coding > Programming language > Python > PyTocrch

PyTocrch

PyTorch is a Tensors and Dynamic neural networks in Python with strong GPU acceleration.

PyTorch installation

PyTorch is a very fast library, using all possibilities of computer CPU and CUDA on GPU. Therefore, it is very important to install proper configuration of this library. It is possible to install PyTorch with Conda or PIP.

PyTorch installation for Linux

Conda installation


# CUDA version 10.2
conda install pytorch torchvision torchaudio cudatoolkit=10.2 -c pytorch
# CUDA version 11.3
conda install pytorch torchvision torchaudio cudatoolkit=11.3 -c pytorch
# CPU only version
conda install pytorch torchvision torchaudio cpuonly -c pytorch

Or if you prefer to use PIP, you can do the same with PIP


# CUDA version 10.2
pip3 install torch torchvision torchaudio
# CUDA version 11.3
pip3 install torch torchvision torchaudio 
--extra-index-url https://download.pytorch.org/whl/cu113
# CPU only version
pip3 install torch torchvision torchaudio 
--extra-index-url https://download.pytorch.org/whl/cpu

PyTorch installation for Windows

For Windows it is a bit more complicated. CUDA-10.2 PyTorch builds are no longer available for Windows, please use CUDA-11.3


# CUDA version 11.3
conda install pytorch torchvision torchaudio cudatoolkit=11.3 -c pytorch
# or with pip
pip3 install torch torchvision torchaudio 
--extra-index-url https://download.pytorch.org/whl/cu113

# CPU only 
conda install pytorch torchvision torchaudio cpuonly -c pytorch
# or with pip
pip3 install torch torchvision torchaudio

Access to GPU

GPU is much faster than CPU for tensor calculations, but it is not always available. Therefore, it is better to write program, which can work with GPU and CPU. This can be done by assigning calculations on different devices.


import torch
torch.cuda.is_available() # True is CUDA available, or False
# selection device for calculations
device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu')
# transfer torch object (x) to a new device
x = x.to(device)


Published: 2022-05-04 19:41:45
Updated: 2022-05-04 19:42:39

Last 10 artitles


9 popular artitles

© 2020 MyCoding.uk -My blog about coding and further learning. This blog was writen with pure Perl and front-end output was performed with TemplateToolkit.