Struct PerplexityMetric
pub struct PerplexityMetric<B>where
B: Backend,{ /* private fields */ }Expand description
The perplexity metric.
Perplexity is a measure of how well a probability distribution or probability model predicts a sample. It’s commonly used to evaluate language models. A lower perplexity indicates that the model is more confident in its predictions.
Mathematically, perplexity is defined as the exponentiation of the cross-entropy loss: PPL = exp(H(p, q)) = exp(-1/N * Σ log(p(x_i)))
where:
- H(p, q) is the cross-entropy between the true distribution p and predicted distribution q
- N is the number of tokens
- p(x_i) is the predicted probability of the i-th token
§Aggregation
Unlike other metrics, perplexity cannot be simply averaged across batches. This implementation correctly accumulates the total negative log-likelihood and total token count across batches, then computes perplexity as exp(total_nll / total_tokens).
Implementations§
§impl<B> PerplexityMetric<B>where
B: Backend,
impl<B> PerplexityMetric<B>where
B: Backend,
pub fn new() -> PerplexityMetric<B>
pub fn new() -> PerplexityMetric<B>
Creates the metric.
pub fn with_pad_token(self, index: usize) -> PerplexityMetric<B>
pub fn with_pad_token(self, index: usize) -> PerplexityMetric<B>
Sets the pad token to exclude from perplexity calculation.
When a pad token is set, predictions for padding tokens are masked out and do not contribute to the perplexity calculation. This is important for variable-length sequences where padding is used.
Trait Implementations§
§impl<B> Clone for PerplexityMetric<B>
impl<B> Clone for PerplexityMetric<B>
§fn clone(&self) -> PerplexityMetric<B>
fn clone(&self) -> PerplexityMetric<B>
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more§impl<B> Default for PerplexityMetric<B>where
B: Backend,
impl<B> Default for PerplexityMetric<B>where
B: Backend,
§fn default() -> PerplexityMetric<B>
fn default() -> PerplexityMetric<B>
§impl<B> Metric for PerplexityMetric<B>where
B: Backend,
impl<B> Metric for PerplexityMetric<B>where
B: Backend,
§type Input = PerplexityInput<B>
type Input = PerplexityInput<B>
§fn update(
&mut self,
input: &PerplexityInput<B>,
_metadata: &MetricMetadata,
) -> MetricEntry
fn update( &mut self, input: &PerplexityInput<B>, _metadata: &MetricMetadata, ) -> MetricEntry
§impl<B> Numeric for PerplexityMetric<B>where
B: Backend,
impl<B> Numeric for PerplexityMetric<B>where
B: Backend,
§fn value(&self) -> NumericEntry
fn value(&self) -> NumericEntry
Auto Trait Implementations§
impl<B> Freeze for PerplexityMetric<B>
impl<B> RefUnwindSafe for PerplexityMetric<B>where
B: RefUnwindSafe,
impl<B> Send for PerplexityMetric<B>
impl<B> Sync for PerplexityMetric<B>
impl<B> Unpin for PerplexityMetric<B>where
B: Unpin,
impl<B> UnwindSafe for PerplexityMetric<B>where
B: UnwindSafe,
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
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more