Getting Started with PyTorch: Installation, Core Operations, and Practical Deep Learning Projects
This article introduces PyTorch, covering installation on CPU/GPU, basic tensor operations, automatic differentiation, building and training neural networks, data loading with DataLoader, image classification on MNIST, model deployment, and useful tips for accelerating deep‑learning workflows.
Today we introduce PyTorch, the open‑source deep‑learning framework developed by Meta, known for its dynamic computation graph and ease of use, suitable for both research and engineering.
PyTorch Installation
Select the appropriate version for your OS and hardware from the official PyTorch installation page. Common commands are:
<code># CPU installation
pip install torch torchvision torchaudio
# GPU installation (CUDA 11.8)
pip install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu118</code>Verify the installation:
<code>import torch
print("PyTorch version:", torch.__version__)
print("CUDA available:", torch.cuda.is_available())</code>Basic Tensor Operations
Tensors are the core data structure in PyTorch, similar to NumPy arrays but with GPU support.
<code>import torch
# Create a tensor
x = torch.tensor([[1, 2], [3, 4]])
print("Tensor:\n", x)
# Tensor addition
y = torch.ones_like(x)
z = x + y
print("Addition result:\n", z)
# Move to GPU if available
if torch.cuda.is_available():
device = torch.device("cuda")
x_gpu = x.to(device)
print("Tensor on GPU:", x_gpu)</code>Automatic Differentiation
PyTorch provides powerful autograd for gradient computation.
<code># Tensor that requires gradient
x = torch.tensor([2.0], requires_grad=True)
# Define function y = x^2
y = x ** 2
# Backward pass
y.backward()
print("Gradient of x:", x.grad)</code>Output: x 的梯度: tensor([4.])
Building a Neural Network
Use torch.nn to define models. Below is a simple linear regression model:
<code>import torch.nn as nn
class LinearModel(nn.Module):
def __init__(self):
super(LinearModel, self).__init__()
self.linear = nn.Linear(1, 1)
def forward(self, x):
return self.linear(x)
model = LinearModel()
print(model)</code>Define loss and optimizer:
<code>import torch.optim as optim
criterion = nn.MSELoss()
optimizer = optim.SGD(model.parameters(), lr=0.01)</code>Training loop example:
<code># Training data
x_train = torch.tensor([[1.0], [2.0], [3.0]])
y_train = torch.tensor([[2.0], [4.0], [6.0]])
for epoch in range(100):
y_pred = model(x_train)
loss = criterion(y_pred, y_train)
optimizer.zero_grad()
loss.backward()
optimizer.step()
if (epoch + 1) % 10 == 0:
print(f"Epoch [{epoch+1}/100], Loss: {loss.item():.4f}")</code>Data Loading with DataLoader
Efficiently load data using DataLoader :
<code>from torch.utils.data import DataLoader, TensorDataset
dataset = TensorDataset(x_train, y_train)
dataloader = DataLoader(dataset, batch_size=2, shuffle=True)
for batch_idx, (x_batch, y_batch) in enumerate(dataloader):
print(f"Batch {batch_idx+1}:")
print("x:", x_batch)
print("y:", y_batch)</code>Image Classification on MNIST
Load the MNIST dataset and build a simple convolutional network:
<code>from torchvision import datasets, transforms
from torch.utils.data import DataLoader
train_dataset = datasets.MNIST(root="./data", train=True, transform=transforms.ToTensor(), download=True)
test_dataset = datasets.MNIST(root="./data", train=False, transform=transforms.ToTensor())
train_loader = DataLoader(train_dataset, batch_size=64, shuffle=True)
test_loader = DataLoader(test_dataset, batch_size=64, shuffle=False)
class ConvNet(nn.Module):
def __init__(self):
super(ConvNet, self).__init__()
self.conv1 = nn.Conv2d(1, 32, kernel_size=3, stride=1, padding=1)
self.relu = nn.ReLU()
self.pool = nn.MaxPool2d(kernel_size=2, stride=2)
self.fc = nn.Linear(32 * 14 * 14, 10)
def forward(self, x):
x = self.pool(self.relu(self.conv1(x)))
x = x.view(x.size(0), -1)
return self.fc(x)
model = ConvNet()
criterion = nn.CrossEntropyLoss()
optimizer = optim.Adam(model.parameters(), lr=0.001)
for epoch in range(5):
for images, labels in train_loader:
outputs = model(images)
loss = criterion(outputs, labels)
optimizer.zero_grad()
loss.backward()
optimizer.step()
print(f"Epoch [{epoch+1}/5], Loss: {loss.item():.4f}")
# Evaluation
model.eval()
correct, total = 0, 0
with torch.no_grad():
for images, labels in test_loader:
outputs = model(images)
_, predicted = torch.max(outputs, 1)
total += labels.size(0)
correct += (predicted == labels).sum().item()
print(f"Test Accuracy: {correct / total * 100:.2f}%")</code>Deployment and Acceleration
Run the model on GPU when available:
<code>device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
model = model.to(device)
# Move data to GPU inside training loop
images, labels = images.to(device), labels.to(device)</code>Save and load model checkpoints:
<code>torch.save(model.state_dict(), "model.pth")
model.load_state_dict(torch.load("model.pth"))</code>PyTorch is a flexible and powerful deep‑learning framework, especially suited for research and rapid experimentation.
We hope this PyTorch guide helps you quickly get started with deep learning and build your AI projects.
Python Programming Learning Circle
A global community of Chinese Python developers offering technical articles, columns, original video tutorials, and problem sets. Topics include web full‑stack development, web scraping, data analysis, natural language processing, image processing, machine learning, automated testing, DevOps automation, and big data.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.