Code Room
Vibe codingMediumvc-g271
Subject Ai code reviewLevel Mid–Senior~17 minCommon in Algorithms & data structures interviewsIndustries Technology

Question

An AI wrote this PyTorch training step for a 1:200 imbalanced defect-detection model. Loss decreases smoothly but the model predicts 'no defect' almost always. Explain why and fix it.

python
import torch.nn as nn criterion = nn.BCEWithLogitsLoss()        # no weightingfor xb, yb in train_loader:               # yb ~0.5% positive    logits = model(xb).squeeze(1)    loss = criterion(logits, yb.float())    loss.backward(); opt.step(); opt.zero_grad()# threshold predictions at 0.5preds = (torch.sigmoid(model(X_val)) > 0.5).int()
What a strong answer looks like

Treat the AI’s output as a draft to verify, not an answer to trust. Name the specific flaw and the input that triggers it, say how you’d catch it — tests, edge cases, reading critically — and how you’d re-prompt or decompose to get it right.

Describe your solution

Vibe coding: describe the solution in plain language (or narrate it) and the coach grades your approach. Generating runnable code from your description is coming next.

Run or narrate your approach, then ask the coach.