Skip to main content

Module generation

Module generation 

Source
Expand description

Sequence generation and decoding algorithms.

This module provides decoding strategies for sequence-to-sequence models:

  • Greedy decoding: Select the most likely token at each step
  • Beam search: Maintain top-k candidates at each step
  • Nucleus (top-p) sampling: Sample from the smallest set of tokens with cumulative probability >= p
  • Top-k sampling: Sample from the k most likely tokens
  • Temperature sampling: Scale logits before sampling

§Example

use aprender::nn::generation::{BeamSearch, NucleusSampler, GenerationConfig};

// Beam search decoding
let beam = BeamSearch::new(5);  // beam_size=5
let output = beam.generate(&model, &input, 50);

// Nucleus sampling
let sampler = NucleusSampler::new(0.95);  // top_p=0.95
let output = sampler.sample(&logits);

§References

  • Holtzman, A., et al. (2020). The Curious Case of Neural Text Degeneration. ICLR. (Nucleus sampling)
  • Freitag, M., & Al-Onaizan, Y. (2017). Beam Search Strategies for Neural Machine Translation. ACL Workshop.

Structs§

BeamHypothesis
A single beam hypothesis in beam search.
BeamSearch
Beam search decoder for sequence generation.
GenerationConfig
Configuration for text generation.
GreedyDecoder
Greedy decoder - always selects the most likely token.
NucleusSampler
Nucleus (top-p) sampler for diverse text generation.
TeacherForcing
Teacher Forcing scheduler for seq2seq training.
TopKSampler
Top-k sampler for text generation.

Enums§

TeacherForcingSchedule
Teacher forcing schedule type.

Functions§

apply_repetition_penalty
Apply repetition penalty to logits.
apply_temperature
Apply temperature scaling to logits.