Week 6 • Day 7Week 6

Day 7:Week 6, Day 7 (Mini-Project): End-to-End PyTorch SRE Incident Severity Neural Classifier

Battle-tested daily lesson note • 100% runnable code, interview Q&A, test suite & interactive quiz

Week 6, Day 7 (Mini-Project): End-to-End PyTorch SRE Incident Severity Neural Classifier


1. Project Overview

In this Week 6 capstone mini-project, we combine every PyTorch concept from Days 1–6 into a clean, modular, production-grade PyTorch Incident Severity Neural Classifier:

  1. Custom SimpleVocab & IncidentTextDataset with dynamic batch padding (dynamic_pad_collate_fn).
  2. Neural Architecture (IncidentNeuralClassifier):
    • nn.Embedding(vocab_size, embed_dim, padding_idx=0)
    • Masked Mean Pooling over non-pad tokens (∑MtEt∑Mt\frac{\sum M_t E_t}{\sum M_t})
    • nn.LayerNorm(embed_dim) →\rightarrow nn.Dropout(p) →\rightarrow nn.Linear(embed_dim, hidden_dim) →\rightarrow GELU →\rightarrow nn.Linear(hidden_dim, num_classes)
  3. Production Training Loop:
    • nn.CrossEntropyLoss()
    • torch.optim.AdamW with decoupled weight decay
    • Global gradient clipping (torch.nn.utils.clip_grad_norm_)
  4. Checkpoint Serialization (state_dict + Vocabulary) & Deterministic Inference under model.eval() + torch.inference_mode().
  5. Root .env Integration via load_root_dotenv().

In Simple English: Putting Week 6 Together (Your First Neural Net App)

This mini-project bridges raw text and a real PyTorch Multi-Layer Perceptron (MLP) classifier:

  1. Tokenizer & Vocabulary: Converts raw SRE alerts ("PostgreSQL connection pool exhausted") into integer IDs.
  2. nn.Embedding + Mean Pooling: Looks up a dense semantic vector for each word, uses an Attention Mask to ignore <PAD> tokens, and averages the sentence into one clean feature vector.
  3. Linear + ReLU + Linear Classifier Head: Predicts severity probabilities (P1_CRITICAL, P2_HIGH, P3_LOW).
  4. Full 5-Step Training Loop: Trains with AdamW, gradient clipping, and validation accuracy tracking!

Interview Prep Note

In senior ML interviews, expect: "Why did you use an attention mask before mean pooling instead of naive x.mean(dim=1)?" Answer: Without masking, short padded sentences get dragged toward the <PAD> vector!