Data Science Machine Learning Data Analysis
37.1K subscribers
1.13K photos
27 videos
39 files
1.24K links
This channel is for Programmers, Coders, Software Engineers.

1- Data Science
2- Machine Learning
3- Data Visualization
4- Artificial Intelligence
5- Data Analysis
6- Statistics
7- Deep Learning

Cross promotion and ads: @hussein_sheikho
加入频道
Data Science Machine Learning Data Analysis
Photo
## 🔹 Complete Training Pipeline
### 1. Training Loop
def train(model, train_loader, criterion, optimizer, epochs=10):
model.train()
losses = []

for epoch in range(epochs):
running_loss = 0.0

for inputs, labels in train_loader:
inputs, labels = inputs.to(device), labels.to(device)

# Forward pass
outputs = model(inputs)
loss = criterion(outputs, labels)

# Backward pass
optimizer.zero_grad()
loss.backward()
optimizer.step()

running_loss += loss.item()

epoch_loss = running_loss / len(train_loader)
losses.append(epoch_loss)
print(f'Epoch {epoch+1}/{epochs}, Loss: {epoch_loss:.4f}')

return losses


### 2. Evaluation Function
def evaluate(model, test_loader):
model.eval()
correct = 0
total = 0

with torch.no_grad():
for inputs, labels in test_loader:
inputs, labels = inputs.to(device), labels.to(device)
outputs = model(inputs)
_, predicted = torch.max(outputs.data, 1)
total += labels.size(0)
correct += (predicted == labels).sum().item()

accuracy = 100 * correct / total
print(f'Test Accuracy: {accuracy:.2f}%')
return accuracy


### 3. Full Execution
# Hyperparameters
input_size = 784 # MNIST images (28x28)
hidden_sizes = [128, 64]
output_size = 10 # Digits 0-9
lr = 0.001
epochs = 10

# Initialize
model = DNN(input_size, hidden_sizes, output_size).to(device)
criterion = nn.CrossEntropyLoss()
optimizer = optim.Adam(model.parameters(), lr=lr)

# Flatten MNIST images
train_loader.dataset.transform = transforms.Compose([
transforms.ToTensor(),
transforms.Normalize((0.5,), (0.5,)),
transforms.Lambda(lambda x: x.view(-1)) # Flatten
])

# Train and evaluate
losses = train(model, train_loader, criterion, optimizer, epochs)
evaluate(model, test_loader)

# Plot training curve
plt.plot(losses)
plt.xlabel('Epoch')
plt.ylabel('Loss')
plt.title('Training Loss Curve')
plt.show()


---

## 🔹 Debugging & Visualization
### 1. Gradient Checking
# After loss.backward()
for name, param in model.named_parameters():
if param.grad is not None:
print(f"{name} gradient mean: {param.grad.mean().item():.6f}")


### 2. Weight Histograms
def plot_weights(model):
for name, param in model.named_parameters():
if 'weight' in name:
plt.figure()
plt.hist(param.detach().cpu().numpy().flatten(), bins=50)
plt.title(name)
plt.show()


---

## 🔹 Advanced Techniques
### 1. Weight Initialization
def init_weights(m):
if isinstance(m, nn.Linear):
nn.init.xavier_uniform_(m.weight)
nn.init.zeros_(m.bias)

model.apply(init_weights)


### 2. Early Stopping
best_loss = float('inf')
patience = 3
trigger_times = 0

for epoch in range(100):
# Training...
val_loss = validate(model, val_loader, criterion)

if val_loss < best_loss:
best_loss = val_loss
trigger_times = 0
torch.save(model.state_dict(), 'best_model.pth')
else:
trigger_times += 1
if trigger_times >= patience:
print("Early stopping!")
break


---

## 🔹 Best Practices
1. Always normalize input data (e.g., scale to [0,1] or standardize)
2. Use batch normalization for deeper networks
3. Start with Adam optimizer (lr=0.001) as default
4. Monitor training with validation set to detect overfitting
5. Visualize weight distributions periodically
6. Use GPU for training (model.to(device))

---

### 📌 What's Next?
In Part 3, we'll cover:
➡️ Convolutional Neural Networks (CNNs)
➡️ Transfer Learning
➡️ Image Augmentation Techniques
➡️ Visualizing CNNs

#PyTorch #DeepLearning #MachineLearning 🚀
2🔥2
Data Science Machine Learning Data Analysis
Photo
# 📚 PyTorch Tutorial for Beginners - Part 3/6: Convolutional Neural Networks (CNNs) & Computer Vision
#PyTorch #DeepLearning #ComputerVision #CNNs #TransferLearning

Welcome to Part 3 of our PyTorch series! This comprehensive lesson dives deep into Convolutional Neural Networks (CNNs), the powerhouse behind modern computer vision applications. We'll cover architecture design, implementation tricks, transfer learning, and visualization techniques.

---

## 🔹 Introduction to CNNs
### Why CNNs for Images?
Traditional fully-connected networks (DNNs) fail for images because:
- Parameter explosion: A 256x256 RGB image → 196,608 input features
- No spatial awareness: DNNs treat pixels as independent features
- Translation variance: Objects in different positions require re-learning

### CNN Key Innovations
| Concept | Purpose | Visual Example |
|--------------------|-------------------------------------------------------------------------|-----------------------------|
| Local Receptive Fields | Processes small regions at a time (e.g., 3x3 windows) | ![Kernel](https://i.imgur.com/YKd5oYk.gif) |
| Weight Sharing | Same filters applied across entire image (reduces parameters) | |
| Hierarchical Features | Early layers detect edges → textures → object parts → whole objects | ![Feature hierarchy](https://miro.medium.com/max/1400/1*uAeAnQw1OdQ0dBL4Z1QlBQ.png) |

---

## 🔹 Core CNN Components
### 1. Convolutional Layers
import torch.nn as nn

# 2D convolution (for images)
conv = nn.Conv2d(
in_channels=3, # Input channels (RGB=3, grayscale=1)
out_channels=16, # Number of filters
kernel_size=3, # 3x3 filter
stride=1, # Filter movement step
padding=1 # Preserves spatial dimensions (with stride=1)
)

# Shape transformation: (batch, channels, height, width)
x = torch.randn(32, 3, 64, 64) # 32 RGB images of 64x64
print(conv(x).shape) # → torch.Size([32, 16, 64, 64])


### 2. Pooling Layers
# Max pooling (common for downsampling)
pool = nn.MaxPool2d(kernel_size=2, stride=2)
print(pool(conv(x)).shape) # → torch.Size([32, 16, 32, 32])

# Adaptive pooling (useful for varying input sizes)
adaptive_pool = nn.AdaptiveAvgPool2d((7, 7))
print(adaptive_pool(x).shape) # → torch.Size([32, 3, 7, 7])


### 3. Normalization Layers
# Batch Normalization
bn = nn.BatchNorm2d(16) # num_features = out_channels
x = conv(x)
x = bn(x)

# Layer Normalization (for NLP/sequences)
ln = nn.LayerNorm([16, 64, 64])


### 4. Dropout
# Spatial dropout (drops entire channels)
dropout = nn.Dropout2d(p=0.25)


---

## 🔹 Building a CNN from Scratch
### Complete Architecture
class CNN(nn.Module):
def __init__(self, num_classes=10):
super().__init__()
self.features = nn.Sequential(
# Block 1
nn.Conv2d(3, 32, kernel_size=3, padding=1),
nn.BatchNorm2d(32),
nn.ReLU(),
nn.MaxPool2d(2),

# Block 2
nn.Conv2d(32, 64, kernel_size=3, padding=1),
nn.BatchNorm2d(64),
nn.ReLU(),
nn.MaxPool2d(2),

# Block 3
nn.Conv2d(64, 128, kernel_size=3, padding=1),
nn.BatchNorm2d(128),
nn.ReLU(),
nn.MaxPool2d(2),
)

self.classifier = nn.Sequential(
nn.Linear(128 * 4 * 4, 512), # Adjusted based on input size
nn.ReLU(),
nn.Dropout(0.5),
nn.Linear(512, num_classes)
)

def forward(self, x):
x = self.features(x)
x = torch.flatten(x, 1) # Flatten all dimensions except batch
x = self.classifier(x)
return x

# Usage
model = CNN().to(device)
print(model)


### Shape Calculation Formula
For a layer with:
- Input size: (Hᵢₙ, Wᵢₙ)
- Kernel: K
- Padding: P
- Stride: S

Output dimensions:
Hₒᵤₜ = ⌊(Hᵢₙ + 2P - K)/S⌋ + 1
Wₒᵤₜ = ⌊(Wᵢₙ + 2P - K)/S⌋ + 1


---
Data Science Machine Learning Data Analysis
Photo
## 🔹 Best Practices for CNN Development
1. Start with pretrained models when possible
2. Use progressive resizing (start with small images, then increase)
3. Monitor class activation maps to debug model focus areas
4. Apply test-time augmentation (TTA) for better inference
5. Use label smoothing for classification tasks
6. Implement learning rate warmup for large batch training

# Label smoothing example
criterion = nn.CrossEntropyLoss(label_smoothing=0.1)

# Learning rate warmup
def warmup_lr(epoch, warmup_epochs=5, base_lr=0.001):
return base_lr * (epoch + 1) / warmup_epochs if epoch < warmup_epochs else base_lr


---

### 📌 What's Next?
In Part 4, we'll cover:
➡️ Recurrent Neural Networks (RNNs/LSTMs)
➡️ Sequence Modeling
➡️ Attention Mechanisms
➡️ Transformer Architectures

#PyTorch #DeepLearning #ComputerVision 🚀

Practice Exercises:
1. Modify the CNN to use depthwise separable convolutions
2. Implement a ResNet-18 from scratch
3. Apply Grad-CAM to visualize model decisions
4. Train on CIFAR-100 with CutMix augmentation
5. Compare Adam vs. SGD with momentum performance

# Depthwise separable convolution example
class DepthwiseSeparableConv(nn.Module):
def __init__(self, in_channels, out_channels, stride=1):
super().__init__()
self.depthwise = nn.Conv2d(in_channels, in_channels, kernel_size=3,
stride=stride, padding=1, groups=in_channels)
self.pointwise = nn.Conv2d(in_channels, out_channels, kernel_size=1)

def forward(self, x):
return self.pointwise(self.depthwise(x))


https://yangx.top/DataScienceM 🌟
Please open Telegram to view this post
VIEW IN TELEGRAM
1
Data Science Machine Learning Data Analysis
Photo
# 📚 PyTorch Tutorial for Beginners - Part 4/6: Sequence Modeling with RNNs, LSTMs & Attention
#PyTorch #DeepLearning #NLP #RNN #LSTM #Transformer

Welcome to Part 4 of our PyTorch series! This comprehensive lesson dives deep into sequence modeling, covering recurrent networks, attention mechanisms, and transformer architectures with practical implementations.

---

## 🔹 Introduction to Sequence Modeling
### Key Challenges with Sequences
1. Variable Length: Sequences can be arbitrarily long (sentences, time series)
2. Temporal Dependencies: Current output depends on previous inputs
3. Context Preservation: Need to maintain long-range relationships

### Comparison of Approaches
| Model Type | Pros | Cons | Typical Use Cases |
|------------------|---------------------------------------|---------------------------------------|---------------------------------|
| RNN | Simple, handles sequences | Struggles with long-term dependencies | Short time series, char-level NLP |
| LSTM | Better long-term memory | Computationally heavier | Machine translation, speech recognition |
| GRU | LSTM-like with fewer parameters | Still limited context | Medium-length sequences |
| Transformer | Parallel processing, global context | Memory intensive for long sequences | Modern NLP, any sequence task |

---

## 🔹 Recurrent Neural Networks (RNNs)
### 1. Basic RNN Architecture
class VanillaRNN(nn.Module):
def __init__(self, input_size, hidden_size, output_size):
super().__init__()
self.hidden_size = hidden_size
self.rnn = nn.RNN(input_size, hidden_size, batch_first=True)
self.fc = nn.Linear(hidden_size, output_size)

def forward(self, x, hidden=None):
# x shape: (batch, seq_len, input_size)
out, hidden = self.rnn(x, hidden)
# Only use last output for classification
out = self.fc(out[:, -1, :])
return out

# Usage
rnn = VanillaRNN(input_size=10, hidden_size=20, output_size=5)
x = torch.randn(3, 15, 10) # (batch=3, seq_len=15, input_size=10)
output = rnn(x)


### 2. The Vanishing Gradient Problem
RNNs struggle with long sequences due to:
- Repeated multiplication of small gradients through time
- Exponential decay of gradient information

Solutions:
- Gradient clipping
- Architectural changes (LSTM, GRU)
- Skip connections

---

## 🔹 Long Short-Term Memory (LSTM) Networks
### 1. LSTM Core Concepts
![LSTM Architecture](https://miro.medium.com/max/1400/1*goJVQs-p9kgLODFNyhl9zA.gif)

Key Components:
- Forget Gate: Decides what information to discard
- Input Gate: Updates cell state with new information
- Output Gate: Determines next hidden state

### 2. PyTorch Implementation
class LSTMModel(nn.Module):
def __init__(self, input_size, hidden_size, num_layers, output_size):
super().__init__()
self.lstm = nn.LSTM(input_size, hidden_size, num_layers,
batch_first=True, dropout=0.2 if num_layers>1 else 0)
self.fc = nn.Linear(hidden_size, output_size)

def forward(self, x):
# Initialize hidden state and cell state
h0 = torch.zeros(self.lstm.num_layers, x.size(0),
self.lstm.hidden_size).to(x.device)
c0 = torch.zeros_like(h0)

out, (hn, cn) = self.lstm(x, (h0, c0))
out = self.fc(out[:, -1, :])
return out

# Bidirectional LSTM example
bidir_lstm = nn.LSTM(input_size=10, hidden_size=20, num_layers=2,
bidirectional=True, batch_first=True)
Data Science Machine Learning Data Analysis
Photo
# Learning rate scheduler for transformers
def lr_schedule(step, d_model=512, warmup_steps=4000):
arg1 = step ** -0.5
arg2 = step * (warmup_steps ** -1.5)
return (d_model ** -0.5) * min(step ** -0.5, step * warmup_steps ** -1.5)


---

### **📌 What's Next?
In **Part 5
, we'll cover:
➡️ Generative Models (GANs, VAEs)
➡️ Reinforcement Learning with PyTorch
➡️ Model Optimization & Deployment
➡️ PyTorch Lightning Best Practices

#PyTorch #DeepLearning #NLP #Transformers 🚀

Practice Exercises:
1. Implement a character-level language model with LSTM
2. Add attention visualization to a sentiment analysis model
3. Build a transformer from scratch for machine translation
4. Compare teacher forcing ratios in seq2seq training
5. Implement beam search for decoder inference

# Character-level LSTM starter
class CharLSTM(nn.Module):
def __init__(self, vocab_size, hidden_size, n_layers):
super().__init__()
self.embed = nn.Embedding(vocab_size, hidden_size)
self.lstm = nn.LSTM(hidden_size, hidden_size, n_layers, batch_first=True)
self.fc = nn.Linear(hidden_size, vocab_size)

def forward(self, x, hidden=None):
x = self.embed(x)
out, hidden = self.lstm(x, hidden)
return self.fc(out), hidden
🔥21
Data Science Machine Learning Data Analysis
Photo
# 📚 PyTorch Tutorial for Beginners - Part 5/6: Generative Models & Advanced Topics
#PyTorch #DeepLearning #GANs #VAEs #ReinforcementLearning #Deployment

Welcome to Part 5 of our PyTorch series! This comprehensive lesson explores generative modeling, reinforcement learning, model optimization, and deployment strategies with practical implementations.

---

## 🔹 Generative Adversarial Networks (GANs)
### 1. GAN Core Concepts
![GAN Architecture](https://miro.medium.com/max/1400/1*5q0q0jQ6Z5Z5Z5Z5Z5Z5Z5A.png)

Key Components:
- Generator: Creates fake samples from noise (typically a transposed CNN)
- Discriminator: Distinguishes real vs. fake samples (CNN classifier)
- Adversarial Training: The two networks compete in a minimax game

### 2. DCGAN Implementation
class Generator(nn.Module):
def __init__(self, latent_dim, img_channels, features_g):
super().__init__()
self.net = nn.Sequential(
# Input: N x latent_dim x 1 x 1
nn.ConvTranspose2d(latent_dim, features_g*8, 4, 1, 0, bias=False),
nn.BatchNorm2d(features_g*8),
nn.ReLU(),
# 4x4
nn.ConvTranspose2d(features_g*8, features_g*4, 4, 2, 1, bias=False),
nn.BatchNorm2d(features_g*4),
nn.ReLU(),
# 8x8
nn.ConvTranspose2d(features_g*4, features_g*2, 4, 2, 1, bias=False),
nn.BatchNorm2d(features_g*2),
nn.ReLU(),
# 16x16
nn.ConvTranspose2d(features_g*2, img_channels, 4, 2, 1, bias=False),
nn.Tanh()
# 32x32
)

def forward(self, x):
return self.net(x)

class Discriminator(nn.Module):
def __init__(self, img_channels, features_d):
super().__init__()
self.net = nn.Sequential(
# Input: N x img_channels x 32 x 32
nn.Conv2d(img_channels, features_d, 4, 2, 1, bias=False),
nn.LeakyReLU(0.2),
# 16x16
nn.Conv2d(features_d, features_d*2, 4, 2, 1, bias=False),
nn.BatchNorm2d(features_d*2),
nn.LeakyReLU(0.2),
# 8x8
nn.Conv2d(features_d*2, features_d*4, 4, 2, 1, bias=False),
nn.BatchNorm2d(features_d*4),
nn.LeakyReLU(0.2),
# 4x4
nn.Conv2d(features_d*4, 1, 4, 1, 0, bias=False),
nn.Sigmoid()
)

def forward(self, x):
return self.net(x)

# Initialize
gen = Generator(latent_dim=100, img_channels=3, features_g=64).to(device)
disc = Discriminator(img_channels=3, features_d=64).to(device)

# Loss and optimizers
criterion = nn.BCELoss()
opt_gen = optim.Adam(gen.parameters(), lr=0.0002, betas=(0.5, 0.999))
opt_disc = optim.Adam(disc.parameters(), lr=0.0002, betas=(0.5, 0.999))


### 3. GAN Training Loop
def train_gan(gen, disc, loader, num_epochs):
fixed_noise = torch.randn(32, 100, 1, 1).to(device)

for epoch in range(num_epochs):
for batch_idx, (real, _) in enumerate(loader):
real = real.to(device)
noise = torch.randn(real.size(0), 100, 1, 1).to(device)
fake = gen(noise)

# Train Discriminator
disc_real = disc(real).view(-1)
loss_disc_real = criterion(disc_real, torch.ones_like(disc_real))
disc_fake = disc(fake.detach()).view(-1)
loss_disc_fake = criterion(disc_fake, torch.zeros_like(disc_fake))
loss_disc = (loss_disc_real + loss_disc_fake) / 2
disc.zero_grad()
loss_disc.backward()
opt_disc.step()

# Train Generator
output = disc(fake).view(-1)
loss_gen = criterion(output, torch.ones_like(output))
gen.zero_grad()
loss_gen.backward()
opt_gen.step()

# Visualization
with torch.no_grad():
fake = gen(fixed_noise)
save_image(fake, f"gan_samples/epoch_{epoch}.png", normalize=True)
1
Data Science Machine Learning Data Analysis
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
Data Science Machine Learning Data Analysis
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
2
Data Science Machine Learning Data Analysis
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
🔥 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://yangx.top/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://yangx.top/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://yangx.top/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://yangx.top/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://yangx.top/DataScienceM