tests/test.sh exit code (0 → resolved); the classification below is post-hoc and cannot change it.Explore app directory structure
ls -la && echo "---SUBMISSION---" && ls -la submission/ && echo "---DATA---" && ls -la data/ && echo "---PYTHON---" && python3 --version && python3 -c "import numpy; print(numpy.__version__)"
total 89 drwxr-xr-x 4 user user 4096 Jun 30 22:36 . drwxr-xr-x 21 root root 4096 Jun 30 22:36 .. -rw-r--r-- 1 user user 12853 Jun 30 22:36 PROMPT.txt -rw-r--r-- 1 user user 5 Jun 30 22:36 agent.pid -rw-r--r-- 1 user user 0 Jun 30 22:36 agent.stderr -rw-r--r-- 1 user user 4637 Jun 30 22:36 agent_trajectory.jsonl drwxr-xr-x 2 user user 60 Jun 30 22:36 data -rw-r--r-- 1 user user 0 Jun 30 22:36 run.log -rwxr-xr-x 1 user user 47612 Jun 30 11:01 run_checks.py drwxr-xr-x 3 user user 128 Jun 30 22:36 submission -rwxr-xr-x 1 user user 1376 Jun 30 11:01 test.sh -rwxr-xr-x 1 user user 629 Jun 30 11:01 timer.sh ---SUBMISSION--- total 100 drwxr-xr-x 3 user user 128 Jun 30 22:36 . drwxr-xr-x 4 user user 4096 Jun 30 22:36 .. drwxr-xr-x 2 user user 4096 Jun 30 22:36 __pycache__ -rw-r--r-- 1 user user 27741 Jun 30 11:01 autograd.py -rw-r--r-- 1 user user 2625 Jun 30 11:01 data.py -rw-r--r-- 1 user user 21670 Jun 30 11:01 nn.py -rw-r--r-- 1 user user 6253 Jun 30 11:01 optim.py -rw-r--r-- 1 user user 20833 Jun 30 11:01 ssl.py -rw-r--r-- 1 user user 3551 Jun 30 11:01 train.py ---DATA--- total 1804 drwxr-xr-x 2 user user 60 Jun 30 22:36 . drwxr-xr-x 4 user user 4096 Jun 30 22:36 .. -rw-r--r-- 1 user user 59874 Jun 30 11:01 labeled.npz -rw-r--r-- 1 user user 1778166 Jun 30 11:01 unlabeled.npz ---PYTHON--- Python 3.11.6 2.2.6
/app/submission/autograd.py
1 """Reverse-mode automatic differentiation over numpy arrays. 2 3 YOU MUST IMPLEMENT EVERYTHING MARKED ``# TODO`` / ``raise NotImplementedError`` using numpy 4 ONLY. No torch/jax/tensorflow/autograd/etc. The grader's conformance suite differentiates each 5 op below with central finite differences and compares against YOUR analytic backward, so every 6 backward must be correct (a single wrong reduction/Jacobian fails the gradient checks). 7 8 A ``Tensor`` wraps one ``numpy.ndarray`` (stored as ``float64`` in ``self.data``) and records 9 the op that produced it so ``.backward()`` can accumulate ``.grad`` into every leaf. 10 11 CONTRACT (the grader builds its own reference against this exact API): 12 * ``Tensor(data, requires_grad=False)`` -- ``self.data`` is ``float64``; ``self.grad`` is None 13 until backward, then a same-shape ``float64`` array. requires_grad leaves accumulate grad. 14 * Every op returns a NEW Tensor whose ``requires_grad`` is the OR of its differentiable inputs, 15 and whose ``_backward`` closure does ``input._accum(local_grad)`` (so multiple paths add up). 16 * ``t.backward(grad=None)`` -- seeds ``grad`` (all-ones for a scalar) and walks the graph in 17 reverse topological order, calling each node's ``_backward``. Repeated backward accumulates. 18 * BROADCASTING: binary ops broadcast like numpy; the backward MUST reduce (sum) the upstream 19 gradient back to each input's ORIGINAL shape (use the provided ``_unbroadcast`` helper). 20 * Numerically …[truncated 29481 chars]
/app/submission/nn.py
1 """Neural-network layers + several from-scratch models, built on YOUR autograd engine.
2
3 Implement every ``# TODO`` forward (the parameters + the ``named_params`` naming are already
4 wired for you; you compose the autograd ops). The grader checks each layer's forward against its
5 OWN reference AND finite-difference-checks the gradients that flow through your autograd, so the
6 composition must be exactly right.
7
8 LAYER / MODEL CHECKLIST (forward + grad checked):
9 Linear Embedding LayerNorm BatchNorm1d Dropout Conv2d MaxPool2d AvgPool2d
10 MultiHeadSelfAttention MLP TransformerBlock
11 RNNCell LSTMCell GRUCell
12 HiddenBlock + MLPClassifier (THE METRIC MODEL) | CNNClassifier (images)
13 | SeqClassifier (rnn/lstm/gru sequence)
14
15 CHECKPOINT NAMING CONTRACT for the METRIC model (model.npz; the grader loads YOUR ``.npz`` into
16 its own reference MLPClassifier by these exact keys/shapes -- do not rename):
17 MLPClassifier (model.npz):
18 layers.{i}.fc.weight (d_in_i, d_hidden) | layers.{i}.fc.bias (d_hidden,)
19 layers.{i}.ln.gamma/.beta (d_hidden,)
20 head.weight (d_hidden, num_classes) | head.bias (num_classes,)
21 (layer 0 has d_in_0 = in_dim; layers 1.. have d_in_i = d_hidden.)
22 CNNClassifier:
23 conv1.weight (C,1,3,3) | conv1.bias (C,) | conv2.weight (C,C,3,3) | conv2.bias (C,)
24 fc.weight (C*(img//4)*(img//4), n_classes) | fc.bias (n_classes,)
25 SeqClassifier, cell in {rnn (G=1), lstm (G=4), gru (G=3)}:
26 embed
…[truncated 22234 chars]/app/submission/optim.py
1 """Optimizers + LR schedulers over a flat list of autograd ``Tensor`` parameters. 2 3 Implement the ``# TODO`` update math + schedulers. Every optimizer updates ``param.data`` IN 4 PLACE using ``param.grad``. The grader runs one (and several) step(s) and compares your updated 5 params against its reference within a tight tolerance, so the math must be exact. ``t`` is the 6 1-based step count (track it yourself where bias correction is needed). 7 8 SGD (momentum ``mu``, coupled weight decay ``wd``, optional Nesterov): 9 g = grad + wd * param 10 v = mu * v + g 11 update = (g + mu * v) if nesterov else v 12 param -= lr * update 13 14 Adagrad: g = grad + wd*param ; s += g^2 ; param -= lr * g / (sqrt(s) + eps) 15 RMSprop: g = grad + wd*param ; s = alpha*s + (1-alpha)*g^2 ; param -= lr*g/(sqrt(s)+eps) 16 17 Adam (COUPLED / L2 weight decay folded into the gradient): 18 g = grad + wd*param 19 m = b1*m + (1-b1)*g ; v = b2*v + (1-b2)*g^2 20 mhat = m/(1-b1^t) ; vhat = v/(1-b2^t) 21 param -= lr * mhat / (sqrt(vhat) + eps) 22 23 AdamW (DECOUPLED weight decay -- decay the PARAM, not the moments): 24 m = b1*m + (1-b1)*g ; v = b2*v + (1-b2)*g^2 25 mhat = m/(1-b1^t) ; vhat = v/(1-b2^t) 26 param -= lr * ( mhat/(sqrt(vhat)+eps) + wd*param ) 27 28 Lion (sign of an interpolated momentum; decoupled weight decay): 29 update = sign( b1*m + (1-b1)*g ) 30 param -= lr * ( update + wd*param ) 31 m = b2*m + (1-b2)*g …[truncated 5297 chars]
/app/submission/data.py
1 """Data loading + minibatch iteration for the FixMatch task, plus tiny synthetic dataset builders
2 the component suite uses to exercise the CNN / sequence models.
3
4 Implement every ``# TODO`` body. ``/app/data/labeled.npz`` ships the FEW labeled examples (arrays
5 ``X`` (N_lab, dim) standardized features + ``y`` (N_lab,) integer labels) and
6 ``/app/data/unlabeled.npz`` ships the large UNLABELED pool (array ``X`` only).
7
8 Contract:
9 load_npz(path) -> (X float64 (N, dim), y int64 (N,) or None if the file has no ``y``)
10 n_classes(y) -> int (max label + 1)
11 epoch_batches(n, batch_size, rng) -> yields arrays of shuffled indices (last batch may be short)
12 get_batch(X, y, idx) -> (Xb float64 (B, dim), yb int64 (B,) or None, idx int64 (B,))
13 make_image_dataset(n, seed, img, n_classes) -> (X (n,1,img,img), y (n,)) for the CNN model
14 make_seq_dataset(n, seed, T, vocab, n_classes) -> (X (n,T) int, y (n,)) for the seq model
15 """
16 from __future__ import annotations
17
18 from typing import Optional, Tuple
19
20 import numpy as np
21
22
23 def load_npz(path: str) -> Tuple[np.ndarray, Optional[np.ndarray]]:
24 # TODO: np.load the file; return (X as float64, y as int64 reshaped to 1-D or None if absent).
25 raise NotImplementedError("load_npz")
26
27
28 def n_classes(y: np.ndarray) -> int:
29 # TODO: return int(max(y)) + 1
30 raise NotImplementedError("n_classes")
31
32
33 def epoch_batches(n: int, batch_size: int, rng: np.random.G
…[truncated 1281 chars]/app/submission/ssl.py
1 """FixMatch (+ related MixMatch / UDA) semi-supervised-learning machinery, on top of YOUR numpy 2 autograd engine. THIS IS THE DOMAIN DELIVERABLE -- implement every ``# TODO`` body. 3 4 Each differentiable loss below is an autograd OP: it must build a scalar Tensor whose ``_backward`` 5 adds the correct analytic gradient into the input logits (the grader finite-difference gradient- 6 checks each one). The augmentation / gating / alignment / EMA pieces are pure numpy recursions the 7 grader checks for exactness. A wrong gradient, gate, alignment, sharpen, mixup, interleave, or EMA 8 update fails the domain milestone -> reward 0. 9 10 CHECKLIST (each is a separate graded check): 11 AUGMENTATION (numpy, label-preserving, no grad): 12 weak_augment strong_augment cutout mixup interleave / de_interleave 13 PSEUDO-LABELING / DISTRIBUTION CONTROL (numpy, no grad): 14 pseudo_label confidence_mask sharpen DistributionAligner EMAScalar 15 DIFFERENTIABLE LOSSES (autograd ops, hand-written analytic backward): 16 soft_cross_entropy consistency_loss mse_consistency kl_divergence 17 entropy_loss class_balance_loss fixmatch_loss 18 EMA TEACHER (numpy recursion): 19 EMATeacher 20 21 NUMERICAL CONVENTIONS (MUST match): 22 * soft_cross_entropy / consistency: per-row loss ``-sum_c target[c]*logp[c]`` weighted by 23 ``weight[n]`` and AVERAGED OVER THE FULL BATCH N (divide by N, not by the number of confident 24 rows). Backward wrt logits: `` …[truncated 20837 chars]
/app/submission/train.py
1 """FixMatch semi-supervised training loop using YOUR from-scratch numpy framework + YOUR SSL 2 machinery in ssl.py. 3 4 Implement the ``# TODO`` training loop. Per step: 5 * supervised: cross-entropy on a WEAKLY-augmented labeled batch; 6 * unlabeled: forward a WEAKLY-augmented view -> softmax -> (distribution-align) -> confidence-gated 7 pseudo-labels; then minimize the masked consistency loss between the STRONGLY-augmented view and 8 those (stop-gradient) pseudo-labels, weighted by lambda_u; 9 * backward -> grad-clip -> optimizer step (an LR warmup + cosine schedule is a good choice); 10 * EMA-update a teacher copy of the weights. 11 12 Save an ``.npz`` checkpoint of the EMA-averaged classifier state_dict so the grader can load your 13 weights into ITS reference classifier and score top-1 on the SEALED held-out TEST split. 14 15 PIN the architecture to match the grader (it rebuilds exactly this graph and loads your weights): 16 in_dim = X.shape[1], num_classes = max(y)+1, d_hidden = 256, n_layer = 3. 17 18 NOTE: a supervised-only run on the few labels overfits them and generalizes near the supervised 19 floor; a naive / collapsed FixMatch run (no distribution alignment / no supervised warmup / 20 mis-tuned gate) collapses to a constant predictor near chance. You need the full FixMatch recipe, 21 with correct component gradients, to clear the threshold. 22 """ 23 from __future__ import annotations 24 25 import argparse 26 import json 27 import math 2 …[truncated 2303 chars]
/app/run_checks.py
1 #!/usr/bin/env python3 2 """PUBLIC component check runner for the from-scratch numpy DL framework + FixMatch SSL machinery. 3 4 Run this AS OFTEN AS YOU LIKE while you implement to get a per-check progress signal: 5 6 python3 /app/run_checks.py # check the framework in /app/submission 7 python3 /app/run_checks.py --submission /some/dir 8 python3 /app/run_checks.py --only fixmatch # run one milestone 9 python3 /app/run_checks.py --quiet # summary line only 10 11 It imports YOUR modules from /app/submission (autograd.py / nn.py / optim.py / data.py / ssl.py), 12 exactly as the grader does, and runs a PUBLIC portion of the component conformance suite, printing 13 a clear per-check report so you can implement -> run -> read failures -> fix -> repeat. 14 15 HOW THIS RELATES TO YOUR GRADE 16 ------------------------------ 17 This runner is a DEBUGGING AID, not the grader. It checks your framework against SELF-CONSISTENT 18 references it computes on its own: 19 * autograd_grads : central FINITE-DIFFERENCE of YOUR forward vs YOUR analytic backward, for every 20 op and every layer/cell/model. (A wrong backward fails here.) 21 * nn_forward : mathematical PROPERTIES your forward must satisfy (softmax rows sum to 1, 22 log_softmax == log(softmax), layernorm/batchnorm row stats, the attention mask 23 is causal, sigmoid/tanh/relu/gelu definitions, shape inva …[truncated 50050 chars]
Check test.sh and data shapes
cat /app/test.sh; echo "---"; python3 -c "
import numpy as np
d=np.load('/app/data/labeled.npz')
print('labeled X',d['X'].shape, d['X'].dtype, 'y',d['y'].shape, 'classes', np.unique(d['y']), 'counts', np.bincount(d['y']))
u=np.load('/app/data/unlabeled.npz')
print('unlabeled', u['X'].shape, u['X'].dtype)
"#!/usr/bin/env bash
# Smoke test (NOT the grader): confirms the environment is wired -- numpy imports, the few labeled
# split + the unlabeled pool load, and the submission stubs are present. It does NOT check
# correctness (the stubs raise NotImplementedError until you implement them).
set -uo pipefail
python3 - <<'PY'
import numpy as np, os
lp, up = "/app/data/labeled.npz", "/app/data/unlabeled.npz"
assert os.path.exists(lp), "missing labeled.npz"
assert os.path.exists(up), "missing unlabeled.npz"
dl = np.load(lp); du = np.load(up)
print(f"labeled.npz: X{dl['X'].shape} {dl['X'].dtype} y{dl['y'].shape} classes={len(set(dl['y'].tolist()))}")
print(f"unlabeled.npz: X{du['X'].shape} {du['X'].dtype} (no labels)")
for f in ("autograd.py", "nn.py", "optim.py", "data.py", "ssl.py", "train.py"):
assert os.path.exists(f"/app/submission/{f}"), f"missing /app/submission/{f}"
assert os.path.exists("/app/run_checks.py"), "missing /app/run_checks.py"
print("submission stubs present:", sorted(os.listdir("/app/submission")))
print("numpy", np.__version__)
PY
# The visible check runner is present + runnable (it exits non-zero on the unfilled stub, which is
# expected -- the stubs raise NotImplementedError until you implement them).
python3 /app/run_checks.py --quiet || true
echo "app_test smoke OK (run 'python3 /app/run_checks.py' to test your implementation)"
---
labeled X (200, 80) float32 y (200,) classes [0 1 2 3 4 5 6 7 8 9] counts [20 20 20 20 20 20 20 20 20 20]
unlabeled (600
…[truncated 14 chars]"""Reverse-mode automatic differentiation over numpy arrays (implemented)."""
from __future__ import annotations
from typing import Callable, Iterable, List, Optional
import numpy as np
def _as_f64(x) -> np.ndarray:
return np.asarray(x, dtype=np.float64)
def _unbroadcast(grad: np.ndarray, shape: tuple) -> np.ndarray:
while grad.ndim > len(shape):
grad = grad.sum(axis=0)
for ax, sz in enumerate(shape):
if sz == 1 and grad.shape[ax] != 1:
grad = grad.sum(axis=ax, keepdims=True)
return grad.reshape(shape)
class Tensor:
__slots__ = ("data", "grad", "requires_grad", "_backward", "_parents")
def __init__(self, data, requires_grad: bool = False, _parents: Iterable["Tensor"] = ()):
self.data: np.ndarray = _as_f64(data)
self.grad: Optional[np.ndarray] = None
self.requires_grad: bool = bool(requires_grad)
self._backward: Callable[[], None] = lambda: None
self._parents: tuple = tuple(_parents)
@property
def shape(self):
return self.data.shape
@property
def ndim(self):
return self.data.ndim
def zero_grad(self):
self.grad = None
def _accum(self, g: np.ndarray):
if self.grad is None:
self.grad = np.zeros_like(self.data)
self.grad += g
def backward(self, grad: Optional[np.ndarray] = None):
topo: List[Tensor] = []
visited = set()
def build(t):
if id(t) in visited:
…[truncated 26709 chars]The file /app/submission/autograd.py has been updated successfully. (file state is current in your context , no need to Read it back)
Run autograd gradient checks
python3 run_checks.py --only autograd_grads 2>&1 | tail -30
grad/batchnorm_x OK (max_abs=3.42e-10) grad/layernorm_gamma OK grad/layernorm_beta OK grad/batchnorm_gamma OK grad/batchnorm_beta OK grad/cross_entropy OK grad/embedding OK grad/nn.Linear_x FAIL (raised NotImplementedError: Linear.forward) grad/nn.RMSNorm_x FAIL (raised NotImplementedError: RMSNorm.forward) grad/nn.SwiGLU_x FAIL (raised NotImplementedError: SwiGLU.forward) grad/nn.LayerNorm_x FAIL (raised NotImplementedError: LayerNorm.forward) grad/nn.BatchNorm1d_x FAIL (raised NotImplementedError: BatchNorm1d.forward) grad/nn.HiddenBlock_x FAIL (raised NotImplementedError: HiddenBlock.forward) grad/nn.MLP_x FAIL (raised NotImplementedError: MLP.forward) grad/nn.MHA_x FAIL (raised NotImplementedError: MultiHeadSelfAttention.forward) grad/nn.Conv2d_x FAIL (raised NotImplementedError: Conv2d.forward) grad/nn.MaxPool2d_x FAIL (raised NotImplementedError: MaxPool2d.forward) grad/nn.AvgPool2d_x FAIL (raised NotImplementedError: AvgPool2d.forward) grad/nn.RNNCell_x FAIL (raised NotImplementedError: RNNCell.forward) grad/nn.LSTMCell_x FAIL (raised NotIm …[truncated 764 chars]
Check for autograd op failures
python3 run_checks.py --only autograd_grads 2>&1 | grep -i fail | grep -v "nn\.\|clf" || echo "NO autograd op failures"
Fix the FAIL lines above (each is a backward/forward/optimizer/SSL bug). Re-run this until everything is green, then train.
/app/submission/nn.py
def forward(self, x: Tensor) -> Tensor:
# TODO: matmul x@weight then add bias (if present). Use ag.matmul / ag.add.
raise NotImplementedError("Linear.forward") def forward(self, x: Tensor) -> Tensor:
out = ag.matmul(x, self.weight)
if self.bias is not None:
out = ag.add(out, self.bias)
return outThe file /app/submission/nn.py has been updated successfully. (file state is current in your context , no need to Read it back)
/app/submission/nn.py
# TODO: return ag.embedding(self.weight, idx)
raise NotImplementedError("Embedding.forward")return ag.embedding(self.weight, idx)
The file /app/submission/nn.py has been updated successfully. (file state is current in your context , no need to Read it back)
/app/submission/nn.py
# TODO: return ag.layernorm(x, self.gamma, self.beta, self.eps)
raise NotImplementedError("LayerNorm.forward")return ag.layernorm(x, self.gamma, self.beta, self.eps)
The file /app/submission/nn.py has been updated successfully. (file state is current in your context , no need to Read it back)
/app/submission/nn.py
# TODO: ms = mean(x*x, -1, keepdims); inv = (ms+eps)**-0.5; return x*inv*gamma (autograd ops)
raise NotImplementedError("RMSNorm.forward") ms = ag.mean(ag.mul(x, x), axis=-1, keepdims=True)
inv = ag.power(ag.add(ms, self.eps), -0.5)
return ag.mul(ag.mul(x, inv), self.gamma)The file /app/submission/nn.py has been updated successfully. (file state is current in your context , no need to Read it back)
/app/submission/nn.py
# TODO: g = ag.silu(self.w_gate(x)); u = self.w_up(x); return self.w_down(ag.mul(g, u))
raise NotImplementedError("SwiGLU.forward") g = ag.silu(self.w_gate(x))
u = self.w_up(x)
return self.w_down(ag.mul(g, u))The file /app/submission/nn.py has been updated successfully. (file state is current in your context , no need to Read it back)
/app/submission/nn.py
# TODO: return ag.batchnorm(x, self.gamma, self.beta, self.eps)
raise NotImplementedError("BatchNorm1d.forward")return ag.batchnorm(x, self.gamma, self.beta, self.eps)
The file /app/submission/nn.py has been updated successfully. (file state is current in your context , no need to Read it back)
/app/submission/nn.py
# TODO: return ag.dropout(x, self.p, training, rng)
raise NotImplementedError("Dropout.forward")return ag.dropout(x, self.p, training, rng)
The file /app/submission/nn.py has been updated successfully. (file state is current in your context , no need to Read it back)
/app/submission/nn.py
# TODO: return ag.conv2d(x, self.weight, self.bias, self.stride, self.padding)
raise NotImplementedError("Conv2d.forward")return ag.conv2d(x, self.weight, self.bias, self.stride, self.padding)
The file /app/submission/nn.py has been updated successfully. (file state is current in your context , no need to Read it back)
/app/submission/nn.py
# TODO: return ag.maxpool2d(x, self.kernel, self.stride)
raise NotImplementedError("MaxPool2d.forward")return ag.maxpool2d(x, self.kernel, self.stride)
The file /app/submission/nn.py has been updated successfully. (file state is current in your context , no need to Read it back)
/app/submission/nn.py
# TODO: return ag.avgpool2d(x, self.kernel, self.stride)
raise NotImplementedError("AvgPool2d.forward")return ag.avgpool2d(x, self.kernel, self.stride)
The file /app/submission/nn.py has been updated successfully. (file state is current in your context , no need to Read it back)
/app/submission/nn.py
# TODO: implement causal multi-head self-attention as described in the docstring.
raise NotImplementedError("MultiHeadSelfAttention.forward") B, Tlen, _ = x.shape
H, dh = self.n_head, self.d_head
qkv = self.qkv(x) # (B, T, 3*d_model)
qkv = ag.reshape(qkv, (B, Tlen, 3, H, dh))
qkv = ag.transpose(qkv, (2, 0, 3, 1, 4)) # (3, B, H, T, dh)
q = qkv[0]
k = qkv[1]
v = qkv[2]
kt = ag.transpose(k, (0, 1, 3, 2)) # (B, H, dh, T)
scores = ag.mul(ag.matmul(q, kt), 1.0 / np.sqrt(dh)) # (B, H, T, T)
mask = np.triu(np.ones((Tlen, Tlen), dtype=np.float64), k=1) * (-1e9)
scores = ag.add(scores, Tensor(mask.reshape(1, 1, Tlen, Tlen)))
att = ag.softmax(scores, axis=-1)
out = ag.matmul(att, v) # (B, H, T, dh)
out = ag.transpose(out, (0, 2, 1, 3)) # (B, T, H, dh)
out = ag.reshape(out, (B, Tlen, H * dh))
return self.proj(out)The file /app/submission/nn.py has been updated successfully. (file state is current in your context , no need to Read it back)
/app/submission/nn.py
# TODO: fc2(gelu(fc1(x)))
raise NotImplementedError("MLP.forward")return self.fc2(ag.gelu(self.fc1(x)))
The file /app/submission/nn.py has been updated successfully. (file state is current in your context , no need to Read it back)
/app/submission/nn.py
# TODO: pre-norm residual wiring (see docstring).
raise NotImplementedError("TransformerBlock.forward") x = ag.add(x, self.attn(self.ln1(x)))
x = ag.add(x, self.mlp(self.ln2(x)))
return xThe file /app/submission/nn.py has been updated successfully. (file state is current in your context , no need to Read it back)
/app/submission/nn.py
# TODO: tanh(x@W_ih + b_ih + h@W_hh + b_hh)
raise NotImplementedError("RNNCell.forward") z = ag.add(ag.add(ag.matmul(x, self.W_ih), self.b_ih),
ag.add(ag.matmul(h, self.W_hh), self.b_hh))
return ag.tanh(z)The file /app/submission/nn.py has been updated successfully. (file state is current in your context , no need to Read it back)
/app/submission/nn.py
# TODO: implement the LSTM cell update; return (h_new, c_new).
raise NotImplementedError("LSTMCell.forward") h, c = state
H = self.hidden
z = ag.add(ag.add(ag.matmul(x, self.W_ih), self.b_ih),
ag.add(ag.matmul(h, self.W_hh), self.b_hh))
i = ag.sigmoid(z[:, 0 * H:1 * H])
f = ag.sigmoid(z[:, 1 * H:2 * H])
g = ag.tanh(z[:, 2 * H:3 * H])
o = ag.sigmoid(z[:, 3 * H:4 * H])
c_new = ag.add(ag.mul(f, c), ag.mul(i, g))
h_new = ag.mul(o, ag.tanh(c_new))
return h_new, c_newThe file /app/submission/nn.py has been updated successfully. (file state is current in your context , no need to Read it back)
/app/submission/nn.py
# TODO: implement the GRU cell update; return h_new.
raise NotImplementedError("GRUCell.forward") H = self.hidden
gi = ag.add(ag.matmul(x, self.W_ih), self.b_ih)
gh = ag.add(ag.matmul(h, self.W_hh), self.b_hh)
r = ag.sigmoid(ag.add(gi[:, 0 * H:1 * H], gh[:, 0 * H:1 * H]))
zg = ag.sigmoid(ag.add(gi[:, 1 * H:2 * H], gh[:, 1 * H:2 * H]))
n = ag.tanh(ag.add(gi[:, 2 * H:3 * H], ag.mul(r, gh[:, 2 * H:3 * H])))
h_new = ag.add(ag.mul(ag.sub(1.0, zg), n), ag.mul(zg, h))
return h_newThe file /app/submission/nn.py has been updated successfully. (file state is current in your context , no need to Read it back)
/app/submission/nn.py
# TODO: relu(layernorm(linear(x))) -- compose self.fc, self.ln and ag.relu.
raise NotImplementedError("HiddenBlock.forward")return ag.relu(self.ln(self.fc(x)))
The file /app/submission/nn.py has been updated successfully. (file state is current in your context , no need to Read it back)
/app/submission/nn.py
# TODO: h = Tensor(X) if X is a raw array; for blk in self.layers: h = blk(h);
# return self.head(h)
raise NotImplementedError("MLPClassifier.forward") h = X if isinstance(X, Tensor) else Tensor(X)
for blk in self.layers:
h = blk(h)
return self.head(h)The file /app/submission/nn.py has been updated successfully. (file state is current in your context , no need to Read it back)
/app/submission/nn.py
# TODO: implement the conv->relu->maxpool x2 -> flatten -> fc pipeline (see docstring).
# Wrap a raw ndarray x in a Tensor first; reshape with the batch size N as dim 0.
raise NotImplementedError("CNNClassifier.forward") h = x if isinstance(x, Tensor) else Tensor(x)
N = h.shape[0]
h = ag.maxpool2d(ag.relu(self.conv1(h)), 2, 2)
h = ag.maxpool2d(ag.relu(self.conv2(h)), 2, 2)
h = ag.reshape(h, (N, self.feat))
return self.fc(h)The file /app/submission/nn.py has been updated successfully. (file state is current in your context , no need to Read it back)
/app/submission/nn.py
# TODO: embed idx, loop the cell over time from a zero state, classify the final hidden.
raise NotImplementedError("SeqClassifier.forward") idx = np.asarray(idx).astype(np.int64)
N, Tlen = idx.shape
emb = self.embed(idx) # (N, T, d_model)
h = Tensor(np.zeros((N, self.hidden)))
c = Tensor(np.zeros((N, self.hidden)))
for t in range(Tlen):
xt = emb[:, t, :]
if self.cell_kind == "lstm":
h, c = self.cell(xt, (h, c))
else:
h = self.cell(xt, h)
return self.fc(h)The file /app/submission/nn.py has been updated successfully. (file state is current in your context , no need to Read it back)
Run autograd and nn forward checks
python3 run_checks.py --only autograd_grads --quiet 2>&1 | tail -3 && python3 run_checks.py --only nn_forward --quiet 2>&1 | tail -3
autograd_grads 77/77 TOTAL 77/77 public checks passing All public checks pass. Now make sure model.npz clears the held-out test threshold, and remember the grader re-checks with DIFFERENT seeds/shapes. nn_forward 20/20 TOTAL 20/20 public checks passing All public checks pass. Now make sure model.npz clears the held-out test threshold, and remember the grader re-checks with DIFFERENT seeds/shapes.
/app/submission/optim.py
def step(self):
# TODO: SGD(+momentum, +coupled weight decay, +optional Nesterov) update.
raise NotImplementedError("SGD.step") def step(self):
for i, p in enumerate(self.params):
if p.grad is None:
continue
g = p.grad + self.weight_decay * p.data
self._v[i] = self.momentum * self._v[i] + g
upd = (g + self.momentum * self._v[i]) if self.nesterov else self._v[i]
p.data -= self.lr * updThe file /app/submission/optim.py has been updated successfully. (file state is current in your context , no need to Read it back)
/app/submission/optim.py
def step(self):
# TODO: Adagrad update (accumulate squared grads).
raise NotImplementedError("Adagrad.step") def step(self):
for i, p in enumerate(self.params):
if p.grad is None:
continue
g = p.grad + self.weight_decay * p.data
self._s[i] = self._s[i] + g * g
p.data -= self.lr * g / (np.sqrt(self._s[i]) + self.eps)The file /app/submission/optim.py has been updated successfully. (file state is current in your context , no need to Read it back)
/app/submission/optim.py
def step(self):
# TODO: RMSprop update (EMA of squared grads).
raise NotImplementedError("RMSprop.step") def step(self):
for i, p in enumerate(self.params):
if p.grad is None:
continue
g = p.grad + self.weight_decay * p.data
self._s[i] = self.alpha * self._s[i] + (1 - self.alpha) * (g * g)
p.data -= self.lr * g / (np.sqrt(self._s[i]) + self.eps)The file /app/submission/optim.py has been updated successfully. (file state is current in your context , no need to Read it back)
/app/submission/optim.py
def step(self):
# TODO: Adam update with bias correction + COUPLED (L2) weight decay.
raise NotImplementedError("Adam.step") def step(self):
self.t += 1
for i, p in enumerate(self.params):
if p.grad is None:
continue
g = p.grad + self.weight_decay * p.data
self._m[i] = self.b1 * self._m[i] + (1 - self.b1) * g
self._v[i] = self.b2 * self._v[i] + (1 - self.b2) * (g * g)
mhat = self._m[i] / (1 - self.b1 ** self.t)
vhat = self._v[i] / (1 - self.b2 ** self.t)
p.data -= self.lr * mhat / (np.sqrt(vhat) + self.eps)The file /app/submission/optim.py has been updated successfully. (file state is current in your context , no need to Read it back)
/app/submission/optim.py
def step(self):
# TODO: AdamW update with bias correction + DECOUPLED weight decay.
raise NotImplementedError("AdamW.step") def step(self):
self.t += 1
for i, p in enumerate(self.params):
if p.grad is None:
continue
g = p.grad
self._m[i] = self.b1 * self._m[i] + (1 - self.b1) * g
self._v[i] = self.b2 * self._v[i] + (1 - self.b2) * (g * g)
mhat = self._m[i] / (1 - self.b1 ** self.t)
vhat = self._v[i] / (1 - self.b2 ** self.t)
p.data -= self.lr * (mhat / (np.sqrt(vhat) + self.eps) + self.weight_decay * p.data)The file /app/submission/optim.py has been updated successfully. (file state is current in your context , no need to Read it back)
/app/submission/optim.py
def step(self):
# TODO: Lion update (sign of interpolated momentum; momentum updated AFTER the step).
raise NotImplementedError("Lion.step") def step(self):
for i, p in enumerate(self.params):
if p.grad is None:
continue
g = p.grad
upd = np.sign(self.b1 * self._m[i] + (1 - self.b1) * g)
p.data -= self.lr * (upd + self.weight_decay * p.data)
self._m[i] = self.b2 * self._m[i] + (1 - self.b2) * gThe file /app/submission/optim.py has been updated successfully. (file state is current in your context , no need to Read it back)
/app/submission/optim.py
# TODO: compute the global L2 norm over all param grads; scale in place if it exceeds max_norm.
raise NotImplementedError("clip_grad_norm") total = 0.0
for p in params:
if p.grad is not None:
total += float((p.grad ** 2).sum())
total = math.sqrt(total)
if total > max_norm:
scale = max_norm / (total + 1e-6)
for p in params:
if p.grad is not None:
p.grad = p.grad * scale
return totalThe file /app/submission/optim.py has been updated successfully. (file state is current in your context , no need to Read it back)
/app/submission/optim.py
# TODO
raise NotImplementedError("cosine_lr") if warmup > 0 and step < warmup:
return base_lr * (step + 1) / warmup
prog = min(1.0, max(0.0, (step - warmup) / max(1, total - warmup)))
return min_lr + 0.5 * (base_lr - min_lr) * (1 + math.cos(math.pi * prog))The file /app/submission/optim.py has been updated successfully. (file state is current in your context , no need to Read it back)
/app/submission/optim.py
# TODO
raise NotImplementedError("step_lr")return base_lr * gamma ** (step // step_size)
The file /app/submission/optim.py has been updated successfully. (file state is current in your context , no need to Read it back)
/app/submission/optim.py
# TODO
raise NotImplementedError("exponential_lr")return base_lr * gamma ** step
The file /app/submission/optim.py has been updated successfully. (file state is current in your context , no need to Read it back)
/app/submission/optim.py
# TODO
raise NotImplementedError("warmup_lr") if step < warmup:
return base_lr * (step + 1) / warmup
return base_lrThe file /app/submission/optim.py has been updated successfully. (file state is current in your context , no need to Read it back)
Run optim checks
python3 run_checks.py --only optim 2>&1 | tail -20
optim/SGD_momentum_5step OK (max_abs=0.0e+00) optim/SGD_nesterov_5step OK (max_abs=0.0e+00) optim/Adagrad_5step OK (max_abs=0.0e+00) optim/RMSprop_5step OK (max_abs=0.0e+00) optim/Adam_1step OK (max_abs=0.0e+00) optim/Adam_10step_wd OK (max_abs=0.0e+00) optim/AdamW_1step OK (max_abs=0.0e+00) optim/AdamW_10step_wd OK (max_abs=0.0e+00) optim/Lion_5step OK (max_abs=0.0e+00) optim/clip_grad_norm_clips OK (max_abs=0.0e+00) optim/clip_grad_norm_noop OK (max_abs=0.0e+00) optim/sched_cosine OK (max_abs=0.0e+00) optim/sched_warmup OK (max_abs=0.0e+00) optim/sched_step OK (max_abs=0.0e+00) optim/sched_exponential OK (max_abs=0.0e+00) ---------------------------------------------------------------------- optim 16/16 TOTAL 16/16 public checks passing All public checks pass. Now make sure model.npz clears the held-out test threshold, and remember the grader re-checks with DIFFERENT seeds/shapes.
/app/submission/data.py
def load_npz(path: str) -> Tuple[np.ndarray, Optional[np.ndarray]]:
# TODO: np.load the file; return (X as float64, y as int64 reshaped to 1-D or None if absent).
raise NotImplementedError("load_npz")
def n_classes(y: np.ndarray) -> int:
# TODO: return int(max(y)) + 1
raise NotImplementedError("n_classes")
def epoch_batches(n: int, batch_size: int, rng: np.random.Generator):
# TODO: yield successive slices of a single rng.permutation(n) of size batch_size.
raise NotImplementedError("epoch_batches")
def get_batch(X: np.ndarray, y: Optional[np.ndarray], idx: np.ndarray):
# TODO: gather X[idx] (float64) and y[idx] (int64, or None if y is None); return (Xb, yb, idx).
raise NotImplementedError("get_batch")def load_npz(path: str) -> Tuple[np.ndarray, Optional[np.ndarray]]:
d = np.load(path)
X = np.asarray(d["X"], dtype=np.float64)
y = None
if "y" in d.files:
y = np.asarray(d["y"], dtype=np.int64).reshape(-1)
return X, y
def n_classes(y: np.ndarray) -> int:
return int(np.max(y)) + 1
def epoch_batches(n: int, batch_size: int, rng: np.random.Generator):
perm = rng.permutation(n)
for start in range(0, n, batch_size):
yield perm[start:start + batch_size]
def get_batch(X: np.ndarray, y: Optional[np.ndarray], idx: np.ndarray):
idx = np.asarray(idx).astype(np.int64)
Xb = np.asarray(X[idx], dtype=np.float64)
yb = None if y is None else np.asarray(y[idx], dtype=np.int64)
return Xb, yb, idxThe file /app/submission/data.py has been updated successfully. (file state is current in your context , no need to Read it back)
/app/submission/data.py
# TODO: build per-class templates, pick labels, return template[y] + noise and the labels.
raise NotImplementedError("make_image_dataset") rng = np.random.default_rng(seed)
templates = rng.standard_normal((n_classes, 1, img, img))
y = rng.integers(0, n_classes, size=n)
X = templates[y] + rng.standard_normal((n, 1, img, img)) * 0.5
return np.asarray(X, dtype=np.float64), np.asarray(y, dtype=np.int64)The file /app/submission/data.py has been updated successfully. (file state is current in your context , no need to Read it back)
/app/submission/data.py
# TODO: sample (n, T) token ids in [0, vocab); label = (row sum) % n_classes.
raise NotImplementedError("make_seq_dataset") rng = np.random.default_rng(seed)
X = rng.integers(0, vocab, size=(n, T))
y = (X.sum(axis=1) % n_classes).astype(np.int64)
return np.asarray(X, dtype=np.int64), yThe file /app/submission/data.py has been updated successfully. (file state is current in your context , no need to Read it back)
/app/submission/ssl.py
# TODO: return X + rng.normal(0, sigma, X.shape) (X as float64)
raise NotImplementedError("weak_augment") X = np.asarray(X, dtype=np.float64)
return X + rng.normal(0.0, sigma, X.shape)The file /app/submission/ssl.py has been updated successfully. (file state is current in your context , no need to Read it back)
/app/submission/ssl.py
# TODO: add jitter, then for each row zero round(mask_frac*dim) randomly-chosen coordinates.
raise NotImplementedError("strong_augment") X = np.asarray(X, dtype=np.float64)
out = X + rng.normal(0.0, sigma, X.shape)
n, dim = out.shape
k = int(round(mask_frac * dim))
if k > 0:
for i in range(n):
cols = rng.choice(dim, size=k, replace=False)
out[i, cols] = 0.0
return outThe file /app/submission/ssl.py has been updated successfully. (file state is current in your context , no need to Read it back)
/app/submission/ssl.py
# TODO: copy X; for each row pick a random start and zero [start:start+length].
raise NotImplementedError("cutout") out = np.array(X, dtype=np.float64)
n, dim = out.shape
for i in range(n):
start = int(rng.integers(0, dim))
out[i, start:start + length] = 0.0
return outThe file /app/submission/ssl.py has been updated successfully. (file state is current in your context , no need to Read it back)
/app/submission/ssl.py
# TODO: implement the convex combination described above; return (X_mix, y_mix, lam, perm).
raise NotImplementedError("mixup") X = np.asarray(X, dtype=np.float64)
onehot = np.asarray(onehot, dtype=np.float64)
n = X.shape[0]
lam = float(rng.beta(alpha, alpha))
lam = builtins_max(lam, 1.0 - lam)
perm = rng.permutation(n)
X_mix = lam * X + (1.0 - lam) * X[perm]
y_mix = lam * onehot + (1.0 - lam) * onehot[perm]
return X_mix, y_mix, lam, permThe file /app/submission/ssl.py has been updated successfully. (file state is current in your context , no need to Read it back)
/app/submission/ssl.py
# TODO: stack to (K, M, ...), swap the leading block axis with K over full blocks, return list.
raise NotImplementedError("interleave") arrs = [np.asarray(a) for a in arrays]
K = len(arrs)
M = arrs[0].shape[0]
rest = arrs[0].shape[1:]
nb = M // batch_size
S = np.stack(arrs, axis=0) # (K, M, ...)
S = S.reshape(K, nb, batch_size, *rest)
S = np.swapaxes(S, 0, 1) # (nb, K, batch, ...)
S = S.reshape(K, M, *rest)
return [S[i] for i in range(K)]The file /app/submission/ssl.py has been updated successfully. (file state is current in your context , no need to Read it back)
/app/submission/ssl.py
# TODO: invert the interleave permutation.
raise NotImplementedError("de_interleave") arrs = [np.asarray(a) for a in arrays]
K = len(arrs)
M = arrs[0].shape[0]
rest = arrs[0].shape[1:]
nb = M // batch_size
S = np.stack(arrs, axis=0) # (K, M, ...)
S = S.reshape(nb, K, batch_size, *rest)
S = np.swapaxes(S, 0, 1) # (K, nb, batch, ...)
S = S.reshape(K, M, *rest)
return [S[i] for i in range(K)]The file /app/submission/ssl.py has been updated successfully. (file state is current in your context , no need to Read it back)
/app/submission/ssl.py
# TODO: compute hard/conf/mask/onehot as above.
raise NotImplementedError("pseudo_label") probs = np.asarray(weak_probs, dtype=np.float64)
n, c = probs.shape
hard = probs.argmax(axis=1)
conf = probs.max(axis=1)
mask = (conf >= tau).astype(np.float64)
onehot = np.zeros((n, c), dtype=np.float64)
onehot[np.arange(n), hard] = 1.0
return onehot, mask, hardThe file /app/submission/ssl.py has been updated successfully. (file state is current in your context , no need to Read it back)
/app/submission/ssl.py
# TODO: return (probs.max(axis=1) >= tau).astype(float)
raise NotImplementedError("confidence_mask") probs = np.asarray(probs, dtype=np.float64)
return (probs.max(axis=1) >= tau).astype(np.float64)The file /app/submission/ssl.py has been updated successfully. (file state is current in your context , no need to Read it back)
/app/submission/ssl.py
# TODO: pw = probs ** (1/temperature); return pw / pw.sum(axis=1, keepdims=True)
raise NotImplementedError("sharpen") probs = np.asarray(probs, dtype=np.float64)
pw = probs ** (1.0 / temperature)
return pw / pw.sum(axis=1, keepdims=True)The file /app/submission/ssl.py has been updated successfully. (file state is current in your context , no need to Read it back)
/app/submission/ssl.py
# TODO: update self.value with the EMA recursion and return it.
raise NotImplementedError("EMAScalar.update") self.value = self.decay * self.value + (1.0 - self.decay) * float(new)
return self.valueThe file /app/submission/ssl.py has been updated successfully. (file state is current in your context , no need to Read it back)
/app/submission/ssl.py
# TODO: update self.running, rescale by prior/(running+1e-6), renormalize rows, return.
raise NotImplementedError("DistributionAligner.align") p = np.asarray(weak_probs, dtype=np.float64)
self.running = self.momentum * self.running + (1.0 - self.momentum) * p.mean(axis=0)
aligned = p * (self.prior / (self.running + 1e-6))
aligned = aligned / aligned.sum(axis=1, keepdims=True)
return alignedThe file /app/submission/ssl.py has been updated successfully. (file state is current in your context , no need to Read it back)
/app/submission/ssl.py
# TODO: compute sigma (per-class confident-argmax counts), set self.sigma, return beta*tau
# with denom = max(sigma.max(), C - sigma.sum()) (guard against 0).
raise NotImplementedError("FlexMatchThresholder.update")
def flex_mask(self, weak_probs) -> np.ndarray:
# TODO: thr = self.update(weak_probs); return (max prob per row >= thr[argmax]) as 0/1 float.
raise NotImplementedError("FlexMatchThresholder.flex_mask") p = np.asarray(weak_probs, dtype=np.float64)
hard = p.argmax(axis=1)
conf = p.max(axis=1)
adm = conf >= self.tau
sigma = np.array([float(np.sum(adm & (hard == c))) for c in range(self.C)],
dtype=np.float64)
self.sigma = sigma
denom = builtins_max(float(sigma.max()), float(self.C - sigma.sum()))
if denom <= 0:
denom = 1.0
return (sigma / denom) * self.tau
def flex_mask(self, weak_probs) -> np.ndarray:
p = np.asarray(weak_probs, dtype=np.float64)
thr = self.update(p)
hard = p.argmax(axis=1)
conf = p.max(axis=1)
return (conf >= thr[hard]).astype(np.float64)The file /app/submission/ssl.py has been updated successfully. (file state is current in your context , no need to Read it back)
/app/submission/ssl.py
# TODO: clip(step/length, 0, 1) (handle length<=0 -> 1.0).
raise NotImplementedError("linear_rampup") if length <= 0:
return 1.0
return float(np.clip(step / length, 0.0, 1.0))The file /app/submission/ssl.py has been updated successfully. (file state is current in your context , no need to Read it back)
/app/submission/ssl.py
# TODO: per the docstring (1.0 once step>=length).
raise NotImplementedError("cosine_rampup") if length <= 0:
return 1.0
t = float(np.clip(step / length, 0.0, 1.0))
return 0.5 * (1.0 - np.cos(np.pi * t))The file /app/submission/ssl.py has been updated successfully. (file state is current in your context , no need to Read it back)
/app/submission/ssl.py
# TODO: per the docstring.
raise NotImplementedError("exp_rampup") if length <= 0:
return 1.0
t = float(np.clip(step / length, 0.0, 1.0))
return float(np.exp(-5.0 * (1.0 - t) ** 2))The file /app/submission/ssl.py has been updated successfully. (file state is current in your context , no need to Read it back)
/app/submission/ssl.py
# TODO: build the (N,C) complementary one-hot on argmin for confident rows only.
raise NotImplementedError("complement_distribution") p = np.asarray(weak_probs, dtype=np.float64)
n, c = p.shape
conf = p.max(axis=1)
least = p.argmin(axis=1)
sel = conf >= tau
out = np.zeros((n, c), dtype=np.float64)
idx = np.arange(n)[sel]
out[idx, least[sel]] = 1.0
return outThe file /app/submission/ssl.py has been updated successfully. (file state is current in your context , no need to Read it back)
/app/submission/ssl.py
# TODO: forward value via _stable_log_softmax_np; build out via _mk(val, (logits,)); set out._backward.
raise NotImplementedError("soft_cross_entropy") logp = _stable_log_softmax_np(x)
sm = np.exp(logp)
per_row = -(tgt * logp).sum(axis=1) # (N,)
val = float((w * per_row).sum() / n)
out = _mk(val, (logits,))
def _bw():
if logits.requires_grad:
tsum = tgt.sum(axis=1, keepdims=True) # (N,1)
grad = (w[:, None] / n) * (sm * tsum - tgt)
logits._accum(out.grad * grad)
out._backward = _bw
return outThe file /app/submission/ssl.py has been updated successfully. (file state is current in your context , no need to Read it back)
/app/submission/ssl.py
# TODO: forward (sum of squared diffs, weighted, /N); backward through the softmax (see docstring).
raise NotImplementedError("mse_consistency") s = _stable_softmax_np(x)
diff = s - tgt
per_row = (diff * diff).sum(axis=1)
val = float((w * per_row).sum() / n)
out = _mk(val, (student_logits,))
def _bw():
if student_logits.requires_grad:
v = (2.0 * w[:, None] / n) * diff
dot = (v * s).sum(axis=1, keepdims=True)
grad = s * (v - dot)
student_logits._accum(out.grad * grad)
out._backward = _bw
return outThe file /app/submission/ssl.py has been updated successfully. (file state is current in your context , no need to Read it back)
/app/submission/ssl.py
# TODO: forward (include the teacher-entropy term so KL is reported); backward as above.
raise NotImplementedError("kl_divergence") logp = _stable_log_softmax_np(x)
sm = np.exp(logp)
log_t = np.log(np.clip(tgt, 1e-12, None))
per_row = (tgt * (log_t - logp)).sum(axis=1)
val = float((w * per_row).sum() / n)
out = _mk(val, (student_logits,))
def _bw():
if student_logits.requires_grad:
tsum = tgt.sum(axis=1, keepdims=True)
grad = (w[:, None] / n) * (sm * tsum - tgt)
student_logits._accum(out.grad * grad)
out._backward = _bw
return outThe file /app/submission/ssl.py has been updated successfully. (file state is current in your context , no need to Read it back)
/app/submission/ssl.py
# TODO: forward mean entropy; backward per the docstring.
raise NotImplementedError("entropy_loss") logp = _stable_log_softmax_np(x)
p = np.exp(logp)
H = -(p * logp).sum(axis=1) # (N,)
val = float(H.sum() / n)
out = _mk(val, (logits,))
def _bw():
if logits.requires_grad:
grad = (1.0 / n) * p * (-logp - H[:, None])
logits._accum(out.grad * grad)
out._backward = _bw
return outThe file /app/submission/ssl.py has been updated successfully. (file state is current in your context , no need to Read it back)
/app/submission/ssl.py
# TODO: forward (-H of the batch marginal); backward per the docstring.
raise NotImplementedError("class_balance_loss") p = _stable_softmax_np(x)
pbar = p.mean(axis=0) # (C,)
pbar_c = np.clip(pbar, 1e-12, None)
val = float((pbar * np.log(pbar_c)).sum())
out = _mk(val, (logits,))
def _bw():
if logits.requires_grad:
a = 1.0 + np.log(pbar_c) # (C,)
dot = (p * a[None, :]).sum(axis=1, keepdims=True)
grad = (1.0 / n) * p * (a[None, :] - dot)
logits._accum(out.grad * grad)
out._backward = _bw
return outThe file /app/submission/ssl.py has been updated successfully. (file state is current in your context , no need to Read it back)
/app/submission/ssl.py
# TODO: stable sigmoid-BCE forward via _mk(val, (z,)); backward (w/N)*(sigmoid(z)-t).
raise NotImplementedError("bce_consistency") s = 1.0 / (1.0 + np.exp(-x))
# stable: -t*log(s) - (1-t)*log(1-s) = max(x,0) - x*t + log(1+exp(-|x|))
per_elem = np.maximum(x, 0.0) - x * t + np.log1p(np.exp(-np.abs(x)))
per_row = per_elem.sum(axis=1)
val = float((w * per_row).sum() / n)
out = _mk(val, (z,))
def _bw():
if z.requires_grad:
grad = (w[:, None] / n) * (s - t)
z._accum(out.grad * grad)
out._backward = _bw
return outThe file /app/submission/ssl.py has been updated successfully. (file state is current in your context , no need to Read it back)
/app/submission/ssl.py
# TODO: forward sum-squared-diff averaged over N; backward (2w/N)*(x-t).
raise NotImplementedError("pi_consistency") diff = x - t
per_row = (diff * diff).sum(axis=1)
val = float((w * per_row).sum() / n)
out = _mk(val, (z,))
def _bw():
if z.requires_grad:
grad = (2.0 * w[:, None] / n) * diff
z._accum(out.grad * grad)
out._backward = _bw
return outThe file /app/submission/ssl.py has been updated successfully. (file state is current in your context , no need to Read it back)
/app/submission/ssl.py
# TODO: forward via _stable_log_softmax_np; backward through softmax per the docstring.
raise NotImplementedError("focal_consistency") logp = _stable_log_softmax_np(x)
p = np.exp(logp)
pc = np.clip(p, 1e-12, 1.0)
one_m = np.clip(1.0 - p, 0.0, 1.0)
nll = -logp # (N,C)
phi = t * (one_m ** gm) * nll
per_row = phi.sum(axis=1)
val = float((w * per_row).sum() / n)
out = _mk(val, (z,))
def _bw():
if z.requires_grad:
om_gm = one_m ** gm
om_gm1 = one_m ** (gm - 1.0) if gm != 0 else np.zeros_like(one_m)
u = t * (-gm * om_gm1 * nll - om_gm / pc)
dot = (u * p).sum(axis=1, keepdims=True)
grad = (w[:, None] / n) * p * (u - dot)
z._accum(out.grad * grad)
out._backward = _bw
return outThe file /app/submission/ssl.py has been updated successfully. (file state is current in your context , no need to Read it back)
/app/submission/ssl.py
# TODO: forward JS (clip probs to 1e-12 before log); backward through softmax per the docstring.
raise NotImplementedError("js_divergence") p = _stable_softmax_np(x)
m = 0.5 * (p + t)
pc = np.clip(p, 1e-12, None)
tc = np.clip(t, 1e-12, None)
mc = np.clip(m, 1e-12, None)
js = 0.5 * (p * (np.log(pc) - np.log(mc))).sum(axis=1) \
+ 0.5 * (t * (np.log(tc) - np.log(mc))).sum(axis=1)
val = float((w * js).sum() / n)
out = _mk(val, (z,))
def _bw():
if z.requires_grad:
u = (w[:, None] / n) * 0.5 * (np.log(pc) - np.log(mc))
dot = (u * p).sum(axis=1, keepdims=True)
grad = p * (u - dot)
z._accum(out.grad * grad)
out._backward = _bw
return outThe file /app/submission/ssl.py has been updated successfully. (file state is current in your context , no need to Read it back)
/app/submission/ssl.py
# TODO: forward -(comp*log(1-p)) (clip 1-p to 1e-12); backward through softmax per the docstring.
raise NotImplementedError("negative_consistency") p = _stable_softmax_np(x)
one_m = np.clip(1.0 - p, 1e-12, None)
per_row = -(comp * np.log(one_m)).sum(axis=1)
val = float((w * per_row).sum() / n)
out = _mk(val, (z,))
def _bw():
if z.requires_grad:
u = comp / one_m
dot = (u * p).sum(axis=1, keepdims=True)
grad = (w[:, None] / n) * p * (u - dot)
z._accum(out.grad * grad)
out._backward = _bw
return outThe file /app/submission/ssl.py has been updated successfully. (file state is current in your context , no need to Read it back)
/app/submission/ssl.py
# TODO: onehot, mask = pseudo_label(...); loss = soft_cross_entropy(...); scale by lambda_u.
raise NotImplementedError("consistency_loss") wp = np.asarray(weak_probs, dtype=np.float64)
onehot, mask, hard = pseudo_label(wp, tau)
loss = soft_cross_entropy(strong_logits, onehot, weight=mask)
if lambda_u != 1.0:
loss = ag.mul(loss, lambda_u)
mask_rate = float(mask.mean())
return loss, mask_rateThe file /app/submission/ssl.py has been updated successfully. (file state is current in your context , no need to Read it back)
/app/submission/ssl.py
# TODO: sup = ag.cross_entropy(...); cons, mr = consistency_loss(...); return (ag.add(sup, cons), mr).
raise NotImplementedError("fixmatch_loss") sup = ag.cross_entropy(sup_logits, sup_y)
cons, mr = consistency_loss(strong_logits, weak_probs, tau, lambda_u=lambda_u)
return ag.add(sup, cons), mrThe file /app/submission/ssl.py has been updated successfully. (file state is current in your context , no need to Read it back)
/app/submission/ssl.py
def update(self, model):
# TODO: for each named param, shadow <- decay*shadow + (1-decay)*param.data (in place).
raise NotImplementedError("EMATeacher.update")
def copy_to(self, model):
# TODO: write self.shadow[k] into each model param's data.
raise NotImplementedError("EMATeacher.copy_to") def update(self, model):
d = self.decay
for k, v in model.named_params().items():
self.shadow[k] = d * self.shadow[k] + (1.0 - d) * v.data
def copy_to(self, model):
for k, v in model.named_params().items():
v.data[...] = self.shadow[k]The file /app/submission/ssl.py has been updated successfully. (file state is current in your context , no need to Read it back)
Run full check suite
python3 run_checks.py 2>&1 | tail -40
fixmatch/bce_consistency_bwd OK (max_abs=9.43e-12) fixmatch/pi_consistency_bwd OK (max_abs=8.00e-11) fixmatch/focal_bwd_onehot OK (max_abs=8.93e-12) fixmatch/focal_bwd_soft OK (max_abs=1.15e-11) fixmatch/js_divergence_bwd OK (max_abs=1.39e-12) fixmatch/negative_consistency_bwd OK (max_abs=7.06e-12) fixmatch/flexmatch_threshold OK fixmatch/rampup_schedules OK fixmatch/complement_distribution OK fixmatch/cons_bwd_tau0.3 OK (max_abs=2.06e-11) fixmatch/cons_bwd_tau0.5 OK (max_abs=0.00e+00) fixmatch/cons_mask_all_zero OK fixmatch/pseudo_label_gate OK fixmatch/pseudo_label_boundary OK fixmatch/confidence_mask OK fixmatch/sharpen_T0.5 OK fixmatch/sharpen_T2.0 OK fixmatch/sharpen_identity OK fixmatch/mixup_convex OK (lam=0.904) fixmatch/interleave_roundtrip OK fixmatch/da_recursion OK fixmatch/ema_scalar OK fixmatch/ema_init_copy OK fixmatch/ema_update_recursion OK fixmatch/fixmatch_loss_decomp OK [integration] 2/2 integration/fixmatch_finite OK (3.458->2.323) integration/fixmatch_decreases OK (3.4580->2.3232) [data] 4/4 data/load_npz OK data/epoch_batches …[truncated 492 chars]
/app/submission/train.py
in_dim = Xl.shape[1]
C = int(yl.max()) + 1
# TODO: build the model + optimizer + EMA teacher (+ distribution aligner), then run the
# FixMatch training loop (supervised CE + masked consistency with confidence-gated, optionally
# distribution-aligned pseudo-labels), and save_checkpoint(out_path, ema.state_dict(), cfg).
raise NotImplementedError("train") in_dim = Xl.shape[1]
C = int(yl.max()) + 1
return _fixmatch_train(Xl, yl, Xu, in_dim, C, out_path, steps=steps, seed=seed)
def _fixmatch_train(Xl, yl, Xu, in_dim, C, out_path, steps=5000, seed=0,
base_lr=2e-3, weight_decay=5e-4, ema_decay=0.999,
tau=0.95, lambda_u=1.0, warmup=200, rampup_len=1500,
bs_l=64, bs_u=448, weak_sigma=0.1, strong_sigma=0.5,
strong_mask=0.5, save=True, verbose=True):
model = MLPClassifier(in_dim, C, d_hidden=D_HIDDEN, n_layer=N_LAYER, seed=seed)
params = model.params()
opt = O.Adam(params, lr=base_lr, weight_decay=weight_decay)
ema = S.EMATeacher(model, decay=ema_decay)
aligner = S.DistributionAligner(C, momentum=0.999)
rng = np.random.default_rng(seed + 12345)
aug = np.random.default_rng(seed + 999)
n_l = Xl.shape[0]
n_u = Xu.shape[0]
for step in range(steps):
lr = lr_at(step, steps, base_lr, warmup)
opt.lr = lr
# supervised (weak-augmented labeled batch)
li = rng.integers(0, n_l, size=min(bs_l, n_l))
Xl_w = S.weak_augment(Xl[li], aug, sigma=weak_sigma)
sup_logits = model(Xl_w)
sup_loss = ag.cross_entropy(sup_logits, yl[li])
# unlabeled consistency
ui = rng.integers(0, n_u, size=min(bs_u, n_u))
Xu_b = Xu[ui]
Xu_w = S.weak_augment(Xu_b, aug, sigma=weak_sigma)
w_logits = model(Xu_w)
wp = S._stable_softmax_np(np.asa
…[truncated 1003 chars]The file /app/submission/train.py has been updated successfully. (file state is current in your context , no need to Read it back)
import sys, numpy as np
sys.path.insert(0, "/app/submission")
import autograd as ag
import ssl as S
from nn import MLPClassifier
import train as TR
def accuracy(sd, X, y, in_dim, C):
m = MLPClassifier(in_dim, C, d_hidden=256, n_layer=3, seed=0)
m.load_state_dict(sd)
logits = m(X).data
pred = logits.argmax(1)
return (pred == y).mean()
def main():
dl = np.load("/app/data/labeled.npz")
Xl = np.asarray(dl["X"], np.float64); yl = np.asarray(dl["y"], np.int64).reshape(-1)
Xu = np.asarray(np.load("/app/data/unlabeled.npz")["X"], np.float64)
in_dim = Xl.shape[1]; C = int(yl.max())+1
# split labeled: 15/class train, 5/class val (stratified)
rng = np.random.default_rng(7)
tr_idx=[]; va_idx=[]
for c in range(C):
idx = np.where(yl==c)[0]; rng.shuffle(idx)
tr_idx += idx[:15].tolist(); va_idx += idx[15:].tolist()
tr_idx=np.array(tr_idx); va_idx=np.array(va_idx)
Xtr, ytr = Xl[tr_idx], yl[tr_idx]
Xva, yva = Xl[va_idx], yl[va_idx]
steps = int(sys.argv[1]) if len(sys.argv)>1 else 3000
# supervised-only baseline (no unlabeled): set lambda_u=0
_,_,sd_sup = TR._fixmatch_train(Xtr, ytr, Xu, in_dim, C, "/tmp/sup.npz",
steps=steps, seed=1, lambda_u=0.0, save=False, verbose=False)
acc_sup_tr = accuracy(sd_sup, Xtr, ytr, in_dim, C)
acc_sup_va = accuracy(sd_sup, Xva, yva, in_dim, C)
print(f"SUPERVISED-ONLY: train={acc_sup_tr:.3f} val={acc_sup_va:.3f}")
# FixMatch
_,_,sd_fm = TR._f
…[truncated 332 chars]File created successfully at: /app/val.py (file state is current in your context , no need to Read it back)
Run validation harness 2000 steps
time python3 val.py 2000 2>&1 | tail -25
No file changes, the agent left the workspace untouched.
=== SyncVals verifier (local re-grade of the agent's own checkpoint) === COMPONENT CONFORMANCE SUITE: 197/197 cases passed (all milestones pass: True) FINAL METRIC: held-out worst-class top-1 = 1.0 >= threshold 0.72 REWARD = 1 (1 iff every component milestone passes AND the sealed held-out metric clears its threshold) NOTE: the in-sandbox training in the 481MB e2b box was OOM-killed (0-byte checkpoint), so the raw in-sandbox reward was an infrastructure artifact. This reward is from a faithful LOCAL re-train of the agent's OWN train.py (24GB, no OOM) with its own default hyperparameters, then re-graded by the real verifier.
Reproduce this trial: git checkout 2f94510 && PYTHONPATH=src python3 scripts/build_site.py , then open trial/trial_0b98abf059d14b88. Re-running the agent live requires EVAL_PLATFORM_ENABLE_OAUTH_SMOKE=1 and is non-deterministic.
Trial trial_0b98abf059d14b88 · verifier authoritative; classifier explanatory.