← 返回 openai 的题目列表Implement and Debug a Minimal Neural Network Training Loop (PyTorch)
类型:online_judge
Problem: Implement and Debug a Minimal Trainable Neural Network (PyTorch)
Given a toy binary classification dataset (feature matrix X and labels y), implement a 2-layer MLP (Linear -> ReLU -> Linear) in PyTorch and write a working training loop.
You must:
Implement the forward pass and output logits.
Compute the loss using BCEWithLogitsLoss.
Update parameters with a gradient-based optimizer (e.g., torch.optim.SGD or Adam).
Print training loss and ensure it decreases substantially.
If provided with buggy code, identify and fix common issues (e.g., missing zero_grad(), shape mismatches, incorrect sigmoid + BCELoss usage, improper detach(), wrong train()/eval() mode).
Input
X: float tensor of shape (N, D)
y: label tensor of shape (N,) or (N, 1) with values in {0,1}
Hyperparameters: learning rate, batch size, epochs (your choice)
Output
Final loss (or a loss curve)
Final training accuracy (or AUC)
Constraints
Must use PyTorch; autograd is allowed.
Must run on CPU.
Example test setup
You may validate with synthetic data:
N=256, D=20, X ~ N(0,1)
Sample random true weights w, set p = sigmoid(X @ w), y = Bernoulli(p)
Training should reduce loss significantly (e.g., >30% from the initial loss).
Example
Input
(synthetic) N=256 D=20 seed=0
Output
loss decreases significantly; prints loss/acc every 10 epochs