All Guides

Установка PyTorch

Установите библиотеку глубокого обучения Meta PyTorch.

Advanced30 мин.

Setup Steps

1. Install CPU version:

pip install torch torchvision torchaudio

2. For NVIDIA GPU (CUDA 12.1):

pip install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu121

3. Verify installation:

python3 -c "import torch; print(torch.__version__); print(torch.cuda.is_available())"

4. Simple model example:

python
import torch
import torch.nn as nn

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

model = SimpleNet()
criterion = nn.CrossEntropyLoss()
optimizer = torch.optim.Adam(model.parameters(), lr=0.001)

5. Move to GPU:

device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
model = model.to(device)

6. Save model:

torch.save(model.state_dict(), 'model.pth')

7. Load model:

model.load_state_dict(torch.load('model.pth'))