Machine Learning
Photo
### 2. Pruning
### 3. ONNX Export
### 4. TorchScript
---
## 🔹 PyTorch Lightning Best Practices
### 1. LightningModule Structure
### 2. Advanced Lightning Features
---
## 🔹 Best Practices Summary
1. For GANs: Use spectral norm, progressive growing, and TTUR
2. For VAEs: Monitor both reconstruction and KL divergence terms
3. For RL: Properly normalize rewards and use experience replay
4. For Deployment: Quantize, prune, and export to optimized formats
5. For Maintenance: Use PyTorch Lightning for reproducible experiments
---
### 📌 What's Next?
In Part 6 (Final), we'll cover:
➡️ Advanced Architectures (Graph NNs, Neural ODEs)
➡️ Model Interpretation Techniques
➡️ Production Deployment (TorchServe, Flask API)
➡️ PyTorch Ecosystem (TorchVision, TorchText, TorchAudio)
#PyTorch #DeepLearning #GANs #ReinforcementLearning 🚀
Practice Exercises:
1. Implement WGAN-GP with gradient penalty
2. Train a VAE on MNIST and visualize latent space
3. Build a DQN agent for CartPole environment
4. Quantize a pretrained ResNet and compare accuracy/speed
5. Convert a model to TorchScript and serve with Flask
parameters_to_prune = (
(model.conv1, 'weight'),
(model.fc1, 'weight'),
)
prune.global_unstructured(
parameters_to_prune,
pruning_method=prune.L1Unstructured,
amount=0.2
)
# Remove pruning reparameterization
for module, param in parameters_to_prune:
prune.remove(module, param)
### 3. ONNX Export
dummy_input = torch.randn(1, 3, 224, 224)
torch.onnx.export(
model,
dummy_input,
"model.onnx",
input_names=["input"],
output_names=["output"],
dynamic_axes={
"input": {0: "batch_size"},
"output": {0: "batch_size"}
}
)
### 4. TorchScript
# Tracing
example_input = torch.rand(1, 3, 224, 224)
traced_script = torch.jit.trace(model, example_input)
traced_script.save("traced_model.pt")
# Scripting
scripted_model = torch.jit.script(model)
scripted_model.save("scripted_model.pt")
---
## 🔹 PyTorch Lightning Best Practices
### 1. LightningModule Structure
import pytorch_lightning as pl
class LitModel(pl.LightningModule):
def __init__(self, learning_rate=1e-3):
super().__init__()
self.save_hyperparameters()
self.model = nn.Sequential(
nn.Linear(28*28, 128),
nn.ReLU(),
nn.Linear(128, 10)
)
def forward(self, x):
return self.model(x)
def training_step(self, batch, batch_idx):
x, y = batch
y_hat = self(x)
loss = nn.functional.cross_entropy(y_hat, y)
self.log('train_loss', loss)
return loss
def validation_step(self, batch, batch_idx):
x, y = batch
y_hat = self(x)
loss = nn.functional.cross_entropy(y_hat, y)
self.log('val_loss', loss)
def configure_optimizers(self):
return optim.Adam(self.parameters(), lr=self.hparams.learning_rate)
# Training
trainer = pl.Trainer(gpus=1, max_epochs=10)
model = LitModel()
trainer.fit(model, train_loader, val_loader)
### 2. Advanced Lightning Features
# Mixed Precision
trainer = pl.Trainer(precision=16)
# Distributed Training
trainer = pl.Trainer(gpus=2, accelerator='ddp')
# Callbacks
early_stop = pl.callbacks.EarlyStopping(monitor='val_loss')
checkpoint = pl.callbacks.ModelCheckpoint(monitor='val_loss')
trainer = pl.Trainer(callbacks=[early_stop, checkpoint])
# Logging
trainer = pl.Trainer(logger=pl.loggers.TensorBoardLogger('logs/'))
---
## 🔹 Best Practices Summary
1. For GANs: Use spectral norm, progressive growing, and TTUR
2. For VAEs: Monitor both reconstruction and KL divergence terms
3. For RL: Properly normalize rewards and use experience replay
4. For Deployment: Quantize, prune, and export to optimized formats
5. For Maintenance: Use PyTorch Lightning for reproducible experiments
---
### 📌 What's Next?
In Part 6 (Final), we'll cover:
➡️ Advanced Architectures (Graph NNs, Neural ODEs)
➡️ Model Interpretation Techniques
➡️ Production Deployment (TorchServe, Flask API)
➡️ PyTorch Ecosystem (TorchVision, TorchText, TorchAudio)
#PyTorch #DeepLearning #GANs #ReinforcementLearning 🚀
Practice Exercises:
1. Implement WGAN-GP with gradient penalty
2. Train a VAE on MNIST and visualize latent space
3. Build a DQN agent for CartPole environment
4. Quantize a pretrained ResNet and compare accuracy/speed
5. Convert a model to TorchScript and serve with Flask
# WGAN-GP Gradient Penalty
def compute_gradient_penalty(D, real_samples, fake_samples):
alpha = torch.rand(real_samples.size(0), 1, 1, 1).to(device)
interpolates = (alpha * real_samples + (1 - alpha) * fake_samples).requires_grad_(True)
d_interpolates = D(interpolates)
gradients = torch.autograd.grad(
outputs=d_interpolates,
inputs=interpolates,
grad_outputs=torch.ones_like(d_interpolates),
create_graph=True,
retain_graph=True,
only_inputs=True
)[0]
gradients = gradients.view(gradients.size(0), -1)
gradient_penalty = ((gradients.norm(2, dim=1) - 1) ** 2).mean()
return gradient_penalty
Machine Learning
Photo
# 📚 PyTorch Tutorial for Beginners - Part 6/6: Advanced Architectures & Production Deployment
#PyTorch #DeepLearning #GraphNNs #NeuralODEs #ModelServing #ExplainableAI
Welcome to the final part of our PyTorch series! This comprehensive lesson covers cutting-edge architectures, model interpretation techniques, production deployment strategies, and the broader PyTorch ecosystem.
---
## 🔹 Graph Neural Networks (GNNs)
### 1. Core Concepts

Key Components:
- Node Features: Characteristics of each graph node
- Edge Features: Properties of connections between nodes
- Message Passing: Nodes aggregate information from neighbors
- Graph Pooling: Reduces graph to fixed-size representation
### 2. Implementing GNN with PyTorch Geometric
### 3. Advanced GNN Architectures
---
## 🔹 Neural Ordinary Differential Equations (Neural ODEs)
### 1. Core Concepts

- Continuous-depth networks: Replace discrete layers with ODE solver
- Memory efficiency: Constant memory cost regardless of "depth"
- Adaptive computation: ODE solver adjusts evaluation points
#PyTorch #DeepLearning #GraphNNs #NeuralODEs #ModelServing #ExplainableAI
Welcome to the final part of our PyTorch series! This comprehensive lesson covers cutting-edge architectures, model interpretation techniques, production deployment strategies, and the broader PyTorch ecosystem.
---
## 🔹 Graph Neural Networks (GNNs)
### 1. Core Concepts

Key Components:
- Node Features: Characteristics of each graph node
- Edge Features: Properties of connections between nodes
- Message Passing: Nodes aggregate information from neighbors
- Graph Pooling: Reduces graph to fixed-size representation
### 2. Implementing GNN with PyTorch Geometric
import torch_geometric as tg
from torch_geometric.nn import GCNConv, global_mean_pool
class GNN(torch.nn.Module):
def __init__(self, node_features, hidden_dim, num_classes):
super().__init__()
self.conv1 = GCNConv(node_features, hidden_dim)
self.conv2 = GCNConv(hidden_dim, hidden_dim)
self.classifier = nn.Linear(hidden_dim, num_classes)
def forward(self, data):
x, edge_index, batch = data.x, data.edge_index, data.batch
# Message passing
x = self.conv1(x, edge_index).relu()
x = self.conv2(x, edge_index)
# Graph-level pooling
x = global_mean_pool(x, batch)
# Classification
return self.classifier(x)
# Example usage
dataset = tg.datasets.Planetoid(root='/tmp/Cora', name='Cora')
model = GNN(node_features=dataset.num_node_features,
hidden_dim=64,
num_classes=dataset.num_classes).to(device)
# Specialized DataLoader
loader = tg.data.DataLoader(dataset, batch_size=32, shuffle=True)
### 3. Advanced GNN Architectures
# Graph Attention Network (GAT)
class GAT(torch.nn.Module):
def __init__(self, in_channels, out_channels):
super().__init__()
self.conv1 = tg.nn.GATConv(in_channels, 8, heads=8, dropout=0.6)
self.conv2 = tg.nn.GATConv(8*8, out_channels, heads=1, concat=False, dropout=0.6)
def forward(self, data):
x, edge_index = data.x, data.edge_index
x = F.dropout(x, p=0.6, training=self.training)
x = F.elu(self.conv1(x, edge_index))
x = F.dropout(x, p=0.6, training=self.training)
x = self.conv2(x, edge_index)
return F.log_softmax(x, dim=1)
# Graph Isomorphism Network (GIN)
class GIN(torch.nn.Module):
def __init__(self, in_channels, hidden_channels, out_channels):
super().__init__()
self.conv1 = tg.nn.GINConv(
nn.Sequential(
nn.Linear(in_channels, hidden_channels),
nn.ReLU(),
nn.Linear(hidden_channels, hidden_channels)
), train_eps=True)
self.conv2 = tg.nn.GINConv(
nn.Sequential(
nn.Linear(hidden_channels, hidden_channels),
nn.ReLU(),
nn.Linear(hidden_channels, out_channels)
), train_eps=True)
def forward(self, data):
x, edge_index = data.x, data.edge_index
x = self.conv1(x, edge_index)
x = F.relu(x)
x = self.conv2(x, edge_index)
return x
---
## 🔹 Neural Ordinary Differential Equations (Neural ODEs)
### 1. Core Concepts

- Continuous-depth networks: Replace discrete layers with ODE solver
- Memory efficiency: Constant memory cost regardless of "depth"
- Adaptive computation: ODE solver adjusts evaluation points
❤3