bugger.
All challenges/Pythonhardllm-train-bigram-v1
THE INVESTIGATION

Train a tiny next-token language model

Train a bigram language model by hand with batch SGD.

LLM ArchitectureLLM training

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

01 One source row receives repeated targets
{"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]}
02 No pairs across separate singleton documents
{"documents":[[0],[1],[]],"weights":[[0,0],[0,0]],"steps":2,"learning_rate":0.5}
{"weights":[[0,0],[0,0]],"loss_history":[0,0,0]}
03 Next-token loss at zero steps
{"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.

    The bug is intentional. Run the starter code to see it fail, then work toward a fix. Difficulty is provisional.
    Read-only preview
    Ready when you are
    Every fix starts with a failing test.

    Read the report, then sign in to start your investigation.