Machine Learning
40K subscribers
3.6K photos
28 videos
47 files
623 links
Real Machine Learning — simple, practical, and built on experience.
Learn step by step with clear explanations and working code.

Admin: @HusseinSheikho || @Hussein_Sheikho
Download Telegram
Machine Learning
Photo
### 2. Pruning
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
![GNN Architecture](https://distill.pub/2021/gnn-intro/images/gnn-overview.png)

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
![Neural ODE](https://miro.medium.com/max/1400/1*5q0q0jQ6Z5Z5Z5Z5Z5Z5Z5A.png)

- 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
Machine Learning
Photo
### 4. TensorRT Optimization
# Convert ONNX to TensorRT
trt_logger = trt.Logger(trt.Logger.WARNING)
with trt.Builder(trt_logger) as builder:
with builder.create_network(1) as network:
with trt.OnnxParser(network, trt_logger) as parser:
with open("model.onnx", "rb") as model:
parser.parse(model.read())
engine = builder.build_cuda_engine(network)


---

## 🔹 PyTorch Ecosystem
### 1. TorchVision
from torchvision.models import efficientnet_b0
from torchvision.ops import nms, roi_align

# Pretrained models
model = efficientnet_b0(pretrained=True)

# Computer vision ops
boxes = torch.tensor([[10, 20, 50, 60], [15, 25, 40, 70]])
scores = torch.tensor([0.9, 0.8])
keep = nms(boxes, scores, iou_threshold=0.5)


### 2. TorchText
from torchtext.data import Field, BucketIterator
from torchtext.datasets import IMDB

# Define fields
TEXT = Field(tokenize='spacy', lower=True, include_lengths=True)
LABEL = Field(sequential=False, dtype=torch.float)

# Load dataset
train_data, test_data = IMDB.splits(TEXT, LABEL)

# Build vocabulary
TEXT.build_vocab(train_data, max_size=25000)
LABEL.build_vocab(train_data)


### 3. TorchAudio
import torchaudio
import torchaudio.transforms as T

# Load audio
waveform, sample_rate = torchaudio.load('audio.wav')

# Spectrogram
spectrogram = T.Spectrogram()(waveform)

# MFCC
mfcc = T.MFCC(sample_rate=sample_rate)(waveform)

# Audio augmentation
augmented = T.TimeStretch()(waveform, n_freq=0.5)


---

## 🔹 Best Practices Summary
1. For GNNs: Normalize node features and use appropriate pooling
2. For Neural ODEs: Monitor ODE solver statistics during training
3. For Interpretability: Combine multiple explanation methods
4. For Deployment: Profile models before deployment (latency/throughput)
5. For Production: Implement monitoring for model drift

---

### 📌 Final Thoughts
Congratulations on completing this comprehensive PyTorch journey! You've learned:

✔️ Core PyTorch fundamentals
✔️ Deep neural networks & CNNs
✔️ Sequence modeling with RNNs/Transformers
✔️ Generative models & reinforcement learning
✔️ Advanced architectures & deployment

#PyTorch #DeepLearning #MachineLearning 🎓🚀

Final Practice Exercises:
1. Implement a GNN for molecular property prediction
2. Train a Neural ODE on irregularly-sampled time series
3. Deploy a model with TorchServe and create a monitoring dashboard
4. Compare SHAP and Integrated Gradients for your CNN model
5. Optimize a transformer model with TensorRT

# Molecular GNN starter
class MolecularGNN(nn.Module):
def __init__(self, node_features, edge_features, hidden_dim):
super().__init__()
self.node_encoder = nn.Linear(node_features, hidden_dim)
self.edge_encoder = nn.Linear(edge_features, hidden_dim)
self.conv = tg.nn.MessagePassing(aggr='mean')

def forward(self, data):
x, edge_index, edge_attr = data.x, data.edge_index, data.edge_attr
x = self.node_encoder(x)
edge_attr = self.edge_encoder(edge_attr)
return self.conv(x, edge_index, edge_attr)
5
Please open Telegram to view this post
VIEW IN TELEGRAM
1
🔥 Trending Repository: LMCache

📝 Description: Supercharge Your LLM with the Fastest KV Cache Layer

🔗 Repository URL: https://github.com/LMCache/LMCache

🌐 Website: https://lmcache.ai/

📖 Readme: https://github.com/LMCache/LMCache#readme

📊 Statistics:
🌟 Stars: 4.3K stars
👀 Watchers: 24
🍴 Forks: 485 forks

💻 Programming Languages: Python - Cuda - Shell

🏷️ Related Topics:
#fast #amd #cuda #inference #pytorch #speed #rocm #kv_cache #llm #vllm


==================================
🧠 By: https://xn--r1a.website/DataScienceM
🔥 Trending Repository: supervision

📝 Description: We write your reusable computer vision tools. 💜

🔗 Repository URL: https://github.com/roboflow/supervision

🌐 Website: https://supervision.roboflow.com

📖 Readme: https://github.com/roboflow/supervision#readme

📊 Statistics:
🌟 Stars: 34K stars
👀 Watchers: 211
🍴 Forks: 2.7K forks

💻 Programming Languages: Python

🏷️ Related Topics:
#python #tracking #machine_learning #computer_vision #deep_learning #metrics #tensorflow #image_processing #pytorch #video_processing #yolo #classification #coco #object_detection #hacktoberfest #pascal_voc #low_code #instance_segmentation #oriented_bounding_box


==================================
🧠 By: https://xn--r1a.website/DataScienceM
🔥 Trending Repository: vllm

📝 Description: A high-throughput and memory-efficient inference and serving engine for LLMs

🔗 Repository URL: https://github.com/vllm-project/vllm

🌐 Website: https://docs.vllm.ai

📖 Readme: https://github.com/vllm-project/vllm#readme

📊 Statistics:
🌟 Stars: 55.5K stars
👀 Watchers: 428
🍴 Forks: 9.4K forks

💻 Programming Languages: Python - Cuda - C++ - Shell - C - CMake

🏷️ Related Topics:
#amd #cuda #inference #pytorch #transformer #llama #gpt #rocm #model_serving #tpu #hpu #mlops #xpu #llm #inferentia #llmops #llm_serving #qwen #deepseek #trainium


==================================
🧠 By: https://xn--r1a.website/DataScienceM
3
🔥 Trending Repository: LLMs-from-scratch

📝 Description: Implement a ChatGPT-like LLM in PyTorch from scratch, step by step

🔗 Repository URL: https://github.com/rasbt/LLMs-from-scratch

🌐 Website: https://amzn.to/4fqvn0D

📖 Readme: https://github.com/rasbt/LLMs-from-scratch#readme

📊 Statistics:
🌟 Stars: 64.4K stars
👀 Watchers: 589
🍴 Forks: 9K forks

💻 Programming Languages: Jupyter Notebook - Python

🏷️ Related Topics:
#python #machine_learning #ai #deep_learning #pytorch #artificial_intelligence #transformer #gpt #language_model #from_scratch #large_language_models #llm #chatgpt


==================================
🧠 By: https://xn--r1a.website/DataScienceM
🔥 Trending Repository: LLMs-from-scratch

📝 Description: Implement a ChatGPT-like LLM in PyTorch from scratch, step by step

🔗 Repository URL: https://github.com/rasbt/LLMs-from-scratch

🌐 Website: https://amzn.to/4fqvn0D

📖 Readme: https://github.com/rasbt/LLMs-from-scratch#readme

📊 Statistics:
🌟 Stars: 68.3K stars
👀 Watchers: 613
🍴 Forks: 9.6K forks

💻 Programming Languages: Jupyter Notebook - Python

🏷️ Related Topics:
#python #machine_learning #ai #deep_learning #pytorch #artificial_intelligence #transformer #gpt #language_model #from_scratch #large_language_models #llm #chatgpt


==================================
🧠 By: https://xn--r1a.website/DataScienceM