pub struct LengthPenalty { /* private fields */ }Expand description
Computes GNMT-style length and coverage penalties for beam-search hypothesis scoring.
Implementations§
Source§impl LengthPenalty
impl LengthPenalty
Sourcepub fn new(config: LengthPenaltyConfig) -> SeqResult<Self>
pub fn new(config: LengthPenaltyConfig) -> SeqResult<Self>
Create a new LengthPenalty. Returns Err if alpha < 0 or beta < 0.
Sourcepub fn lp(&self, length: usize) -> f64
pub fn lp(&self, length: usize) -> f64
GNMT length penalty: ((5 + length) / (5 + 1))^alpha.
At length=1: returns 1.0.
Monotonically increasing for alpha > 0.
Sourcepub fn cp(&self, coverage_probs: &[f64], n_source: usize, seq_len: usize) -> f64
pub fn cp(&self, coverage_probs: &[f64], n_source: usize, seq_len: usize) -> f64
Coverage penalty: Σ_i log(min(Σ_t p_{t,i}, 1.0)).
coverage_probs has layout [seq_len × n_source]
(for each target step t, attention over n_source source tokens).
Returns 0.0 when all source tokens are fully covered (sum ≥ 1.0). Returns a negative value when coverage is partial.
Trait Implementations§
Source§impl Clone for LengthPenalty
impl Clone for LengthPenalty
Source§fn clone(&self) -> LengthPenalty
fn clone(&self) -> LengthPenalty
Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source. Read moreAuto Trait Implementations§
impl Freeze for LengthPenalty
impl RefUnwindSafe for LengthPenalty
impl Send for LengthPenalty
impl Sync for LengthPenalty
impl Unpin for LengthPenalty
impl UnsafeUnpin for LengthPenalty
impl UnwindSafe for LengthPenalty
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more