Train a tiny next-token language model
Train a bigram language model by hand with batch SGD.
What’s going wrong
Original LLM Architecture lab. Implement the missing behavior in solve(data) and keep the supplied JSON interface. Use Python’s standard library.
Input documents contains token ID lists, weights is a square vocabulary-by-vocabulary matrix of logits, steps is nonnegative and learning_rate is nonnegative. Each adjacent pair within a document selects one logit row and one target class. At every step compute mean cross-entropy before the update, scatter-add gradients into shared rows, then apply full-batch SGD. Return weights and loss_history with one mean per update plus the final loss. Empty pair sets produce only zero losses and preserve weights.
Small, well-formed JSON inputs and standard-library Python arithmetic. Keep full precision during computation. The supplied output wrapper rounds results to six decimal places.
Expected behavior
Train a bigram language model by hand with batch SGD.
Reproduction
{"documents":[[0,1,0,1]],"weights":[[0,0],[0,0]],"steps":3,"learning_rate":0.5}{"weights":[[-0.42304,0.42304],[0.229872,-0.229872]],"loss_history":[0.693147,0.564631,0.470962,0.401182]}{"documents":[[0],[1],[]],"weights":[[0,0],[0,0]],"steps":2,"learning_rate":0.5}{"weights":[[0,0],[0,0]],"loss_history":[0,0,0]}{"documents":[[0,1]],"weights":[[0,0],[0,0]],"steps":0,"learning_rate":0.5}{"weights":[[0,0],[0,0]],"loss_history":[0.693147]}Before you begin
- Python functions, lists and dictionaries
Need a nudge?
Reveal one investigation hint at a time. Try each clue before opening the next.
Read the report, then sign in to start your investigation.