Beam Search
Greedy search discards every alternative immediately. Keep a bounded set of complete prefixes, sum log probabilities and prune globally after each step.
import math
greedy_path = math.log(0.6)+math.log(0.5)
alternative = math.log(0.4)+math.log(1.0)
print(alternative > greedy_path)From arithmetic to an experiment
Greedy search discards every alternative immediately. Keep a bounded set of complete prefixes, sum log probabilities and prune globally after each step. The trace reads the local variables of your function as it executes. Change one input or parameter, predict the result, then run the checks again. A small deterministic example makes arithmetic mistakes easier to locate than a long training run. State the shape and meaning of the returned value before coding. Keep preprocessing, evaluation and model updates in their proper order, and examine the boundary case in each check. The companion lab lets you replay intermediate values while the same algorithm runs.
Your task
beam_search(transitions, start, steps, width) returns the highest log-score token path (including start) after exactly steps expansions. transitions maps a token to (next_token, probability) pairs; all expanded tokens have successors and probabilities > 0. Keep width paths; ties use lexicographic path order.
The explanation opens here once every check passes.