Deep Learning Theory and Mathematical Foundations
The foundation of neural networks begins with the perceptron, a simple linear classifier that forms the basis of modern deep learning.
Non-linear activation functions enable neural networks to learn complex patterns:
The core algorithm for training neural networks through gradient descent:
Specialized for processing grid-like data such as images:
class ConvBlock(nn.Module):
def __init__(self, in_channels, out_channels):
super().__init__()
self.conv = nn.Conv2d(in_channels, out_channels, 3, padding=1)
self.bn = nn.BatchNorm2d(out_channels)
self.relu = nn.ReLU(inplace=True)
def forward(self, x):
return self.relu(self.bn(self.conv(x)))
Applying transformer architecture to computer vision:
Combining CNNs and Transformers for optimal performance:
Squeeze-and-Excitation blocks recalibrate channel-wise feature responses:
Focus on where to attend in the spatial dimensions:
class SpatialAttention(nn.Module):
def __init__(self):
super().__init__()
self.conv = nn.Conv2d(2, 1, 7, padding=3)
self.sigmoid = nn.Sigmoid()
def forward(self, x):
avg_out = torch.mean(x, dim=1, keepdim=True)
max_out, _ = torch.max(x, dim=1, keepdim=True)
x = torch.cat([avg_out, max_out], dim=1)
x = self.conv(x)
return self.sigmoid(x)
Combines both channel and spatial attention mechanisms:
Objective functions for different tasks:
Preventing overfitting and improving generalization:
Adaptive learning rate strategies:
# SGD with Momentum
v_t = β × v_{t-1} + η × ∇L
W_t = W_{t-1} - v_t
# Adam Optimizer
m_t = β₁ × m_{t-1} + (1 - β₁) × ∇L
v_t = β₂ × v_{t-1} + (1 - β₂) × (∇L)²
m̂_t = m_t / (1 - β₁^t)
v̂_t = v_t / (1 - β₂^t)
W_t = W_{t-1} - η × m̂_t / (√v̂_t + ε)
Accelerate training with automatic mixed precision:
from torch.cuda.amp import autocast, GradScaler
scaler = GradScaler()
with autocast():
output = model(input)
loss = criterion(output, target)
scaler.scale(loss).backward()
scaler.step(optimizer)
scaler.update()