Install and set up PyTorch (torch) - Python deep learning framework with GPU acceleration for tensor computation and neural networks
Install and configure PyTorch (torch), a Python package for tensor computation with strong GPU acceleration and deep neural networks built on a tape-based autograd system.
This skill guides you through installing PyTorch from PyPI, verifying the installation, checking GPU availability, and running a basic tensor operation to confirm everything works correctly.
Follow these steps to install and verify PyTorch:
Verify you have Python 3.10 or later installed:
```bash
python --version
```
If Python is not installed or the version is too old, install or upgrade Python first.
Install the latest stable version of PyTorch from PyPI:
```bash
pip install torch
```
**For specific configurations:**
```bash
pip install torch --index-url https://download.pytorch.org/whl/cpu
```
```bash
pip install torch --index-url https://download.pytorch.org/whl/cu121
```
```bash
pip install torch --index-url https://download.pytorch.org/whl/cu118
```
Visit [pytorch.org/get-started/locally](https://pytorch.org/get-started/locally/) for the most up-to-date installation commands for your platform.
Create a Python script or open a Python REPL and run:
```python
import torch
print(f"PyTorch version: {torch.__version__}")
print(f"CUDA available: {torch.cuda.is_available()}")
if torch.cuda.is_available():
print(f"CUDA version: {torch.version.cuda}")
print(f"GPU device: {torch.cuda.get_device_name(0)}")
```
This confirms PyTorch is installed and shows whether GPU acceleration is available.
Test basic tensor operations:
```python
import torch
x = torch.rand(3, 3)
print("Random tensor:")
print(x)
if torch.cuda.is_available():
x = x.cuda()
print("\nTensor moved to GPU")
print(x)
y = x + 2
print("\nTensor + 2:")
print(y)
```
If this runs without errors, PyTorch is correctly installed and functional.
For additional functionality, install optional packages:
```bash
pip install torchvision
pip install torchaudio
pip install torchtext
```
Leave a review
No reviews yet. Be the first to review this skill!
# Download SKILL.md from killerskills.ai/api/skills/pypi-package-torch/raw