Part 3: Building Neural Networks with torch.nn
My First Neural Network
The nn.Module Foundation
Basic nn.Module
import torch
import torch.nn as nn
class SimpleModel(nn.Module):
"""Basic neural network structure."""
def __init__(self, input_size, hidden_size, output_size):
super().__init__()
# Define layers
self.layer1 = nn.Linear(input_size, hidden_size)
self.activation = nn.ReLU()
self.layer2 = nn.Linear(hidden_size, output_size)
def forward(self, x):
"""Forward pass - define computation."""
x = self.layer1(x)
x = self.activation(x)
x = self.layer2(x)
return x
# Create model
model = SimpleModel(input_size=10, hidden_size=20, output_size=2)
# Use model
input_data = torch.randn(5, 10) # Batch of 5 samples
output = model(input_data)
print(f"Input shape: {input_data.shape}")
print(f"Output shape: {output.shape}")
print(f"Number of parameters: {sum(p.numel() for p in model.parameters())}")Common Layers
Linear (Fully Connected)
Convolutional Layers
Pooling Layers
Activation Functions
Dropout and Regularization
Building Real Architectures
Image Classifier (CNN)
Text Classifier (RNN/LSTM)
Sequential vs Custom Forward
Using nn.Sequential
Custom Forward (More Flexible)
Loss Functions
Classification Losses
Regression Losses
Custom Loss
Real Production Model
Model Inspection
Print architecture:
Count parameters:
Layer-wise parameters:
Summary (requires torchsummary):
Best Practices
What's Next?
Last updated