> ## Documentation Index
> Fetch the complete documentation index at: https://base.bangwu.me/llms.txt
> Use this file to discover all available pages before exploring further.

# PyTorch

> PyTorch deep learning framework guide covering CPU and GPU installation, tensor creation and operations, automatic differentiation, and neural network basics.

# PyTorch

PyTorch is an open-source deep learning framework developed by Facebook AI Research and widely used in both academic research and industry.

## Installation

### Automatic installation

Choose the right command from the official website: [https://pytorch.org/get-started/locally/](https://pytorch.org/get-started/locally/)

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
# CPU 版本
pip install torch torchvision torchaudio

# GPU 版本 (CUDA 12.4)
pip install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu124
```

### Manual GPU installation

Download page: [https://download.pytorch.org/whl/torch/](https://download.pytorch.org/whl/torch/)

You need to download three packages, making sure the Python and CUDA versions match:

* `torch-2.4.1+cu124-cp312-cp312-win_amd64.whl`
* `torchvision-0.19.1+cu124-cp312-cp312-win_amd64.whl`
* `torchaudio-2.4.1+cu124-cp312-cp312-win_amd64.whl`

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
pip install torch-2.4.1+cu124-cp312-cp312-win_amd64.whl
pip install torchvision-0.19.1+cu124-cp312-cp312-win_amd64.whl
pip install torchaudio-2.4.1+cu124-cp312-cp312-win_amd64.whl
```

### Verify the installation

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
import torch

print(torch.__version__)
print(torch.cuda.is_available())  # True 表示 GPU 可用
print(torch.cuda.get_device_name(0))  # 显示 GPU Name
```

## Basic concepts

### Tensors

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
import torch

# 创建张量
x = torch.tensor([1, 2, 3])
y = torch.zeros(2, 3)
z = torch.randn(2, 3)

# 张量运算
a = torch.tensor([1.0, 2.0])
b = torch.tensor([3.0, 4.0])
c = a + b
d = torch.matmul(a, b)  # 矩阵乘法

# GPU 加速
if torch.cuda.is_available():
    x = x.cuda()  # 转移到 GPU
    # 或者
    x = x.to('cuda')
```

### Automatic differentiation

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
# 需要梯度's 张量
x = torch.tensor([1.0, 2.0], requires_grad=True)
y = x ** 2
z = y.sum()

# 反向传播
z.backward()
print(x.grad)  # 梯度: [2.0, 4.0]
```

## Building neural networks

### Simple model

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
import torch.nn as nn

class SimpleNet(nn.Module):
    def __init__(self):
        super(SimpleNet, self).__init__()
        self.fc1 = nn.Linear(784, 128)
        self.fc2 = nn.Linear(128, 10)
        self.relu = nn.ReLU()
    
    def forward(self, x):
        x = self.fc1(x)
        x = self.relu(x)
        x = self.fc2(x)
        return x

model = SimpleNet()
```

### Convolutional neural network

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
class CNN(nn.Module):
    def __init__(self):
        super(CNN, self).__init__()
        self.conv1 = nn.Conv2d(1, 32, 3, 1)
        self.conv2 = nn.Conv2d(32, 64, 3, 1)
        self.fc1 = nn.Linear(9216, 128)
        self.fc2 = nn.Linear(128, 10)
        self.relu = nn.ReLU()
        self.maxpool = nn.MaxPool2d(2)
    
    def forward(self, x):
        x = self.conv1(x)
        x = self.relu(x)
        x = self.maxpool(x)
        x = self.conv2(x)
        x = self.relu(x)
        x = self.maxpool(x)
        x = x.view(x.size(0), -1)
        x = self.fc1(x)
        x = self.relu(x)
        x = self.fc2(x)
        return x
```

## Training a model

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
import torch.optim as optim

# 准备数据
train_loader = torch.utils.data.DataLoader(dataset, batch_size=64)

# 定义模型、损失函数、优化器
model = SimpleNet()
criterion = nn.CrossEntropyLoss()
optimizer = optim.Adam(model.parameters(), lr=0.001)

# 训练循环
for epoch in range(10):
    for batch_idx, (data, target) in enumerate(train_loader):
        # 前向传播
        output = model(data)
        loss = criterion(output, target)
        
        # 反向传播
        optimizer.zero_grad()
        loss.backward()
        optimizer.step()
        
        if batch_idx % 100 == 0:
            print(f'Epoch: {epoch}, Loss: {loss.item()}')
```

## Data loading

### `Dataset` and `DataLoader`

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
from torch.utils.data import Dataset, DataLoader

class CustomDataset(Dataset):
    def __init__(self, data, labels):
        self.data = data
        self.labels = labels
    
    def __len__(self):
        return len(self.data)
    
    def __getitem__(self, idx):
        return self.data[idx], self.labels[idx]

dataset = CustomDataset(data, labels)
dataloader = DataLoader(dataset, batch_size=32, shuffle=True)
```

### Image augmentation

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
from torchvision import transforms

transform = transforms.Compose([
    transforms.Resize(256),
    transforms.CenterCrop(224),
    transforms.ToTensor(),
    transforms.Normalize(mean=[0.485, 0.456, 0.406],
                        std=[0.229, 0.224, 0.225])
])
```

## Saving and loading models

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
# 保存模型
torch.save(model.state_dict(), 'model.pth')

# 加载模型
model = SimpleNet()
model.load_state_dict(torch.load('model.pth'))
model.eval()

# 保存完整模型
torch.save(model, 'model_complete.pth')
model = torch.load('model_complete.pth')
```

## Jupyter Notebook setup

Create a dedicated PyTorch kernel:

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
# 创建虚拟环境
python -m venv pytorch_env

# 激活环境
source pytorch_env/bin/activate  # Linux/Mac
pytorch_env\Scripts\activate     # Windows

# 安装 PyTorch 和 Jupyter
pip install torch torchvision jupyter ipykernel

# 创建内核
python -m ipykernel install --user --name=pytorch --display-name "Python (PyTorch)"
```

Reference: [https://blog.csdn.net/ccaoshangfei/article/details/126521809](https://blog.csdn.net/ccaoshangfei/article/details/126521809)

## Useful tips

### Set the random seed

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
torch.manual_seed(42)
if torch.cuda.is_available():
    torch.cuda.manual_seed(42)
```

### Show model structure

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
from torchsummary import summary

summary(model, input_size=(1, 28, 28))
```

### Freeze part of the model

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
# 冻结前两层
for param in model.parameters():
    param.requires_grad = False

for param in model.fc2.parameters():
    param.requires_grad = True
```

## Learning resources

* Official docs: [https://pytorch.org/docs/](https://pytorch.org/docs/)
* Official tutorials: [https://pytorch.org/tutorials/](https://pytorch.org/tutorials/)
* Chinese docs: [https://pytorch-cn.readthedocs.io/](https://pytorch-cn.readthedocs.io/)
* GitHub: [https://github.com/pytorch/pytorch](https://github.com/pytorch/pytorch)
