Hypothesis expansion, pruning, eos handling
Medium InferenceImplement beam search โ the classic decoding algorithm for sequence generation.
1. Start with [(0.0, [start_token])]
2. Each step: expand each beam with top-k next tokens
3. Keep top beam_width beams by total log-probability
4. Stop when best beam ends with eos_token or max_len reached
Implement the function below. Use only basic PyTorch operations.
Use this code to debug before submitting.
Try solving it yourself first! Click below to reveal the solution.
For interactive practice with auto-grading, run TorchCode locally:pip install torch-judge then use check("beam_search")
Hypothesis expansion, pruning, eos handling