Struct PerplexityMetric
pub struct PerplexityMetric { /* 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 PerplexityMetric
impl PerplexityMetric
pub fn new() -> PerplexityMetric
pub fn new() -> PerplexityMetric
Creates the metric.
pub fn with_pad_token(self, index: usize) -> PerplexityMetric
pub fn with_pad_token(self, index: usize) -> PerplexityMetric
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 Clone for PerplexityMetric
impl Clone for PerplexityMetric
§fn clone(&self) -> PerplexityMetric
fn clone(&self) -> PerplexityMetric
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more§impl Default for PerplexityMetric
impl Default for PerplexityMetric
§fn default() -> PerplexityMetric
fn default() -> PerplexityMetric
§impl Metric for PerplexityMetric
impl Metric for PerplexityMetric
§type Input = PerplexityInput
type Input = PerplexityInput
§fn update(
&mut self,
input: &PerplexityInput,
_metadata: &MetricMetadata,
) -> Result<SerializedEntry, TensorReadError>
fn update( &mut self, input: &PerplexityInput, _metadata: &MetricMetadata, ) -> Result<SerializedEntry, TensorReadError>
§fn compute(&mut self) -> Result<SerializedEntry, TensorReadError>
fn compute(&mut self) -> Result<SerializedEntry, TensorReadError>
§fn attributes(&self) -> MetricAttributes
fn attributes(&self) -> MetricAttributes
§fn description(&self) -> Option<String>
fn description(&self) -> Option<String>
§impl Numeric for PerplexityMetric
impl Numeric for PerplexityMetric
§fn value(&self) -> Option<NumericEntry>
fn value(&self) -> Option<NumericEntry>
None if this metric has
no meaningful value yet (e.g. it only accumulates state per batch and
computes its value once all state has been accumulated).§fn running_value(&self) -> Option<NumericEntry>
fn running_value(&self) -> Option<NumericEntry>
None if this metric has no meaningful value yet (e.g. it only accumulates state
per batch and computes its value once all state has been accumulated).§fn final_value(&self) -> NumericEntry
fn final_value(&self) -> NumericEntry
Metric::compute has been called.Auto Trait Implementations§
impl Freeze for PerplexityMetric
impl RefUnwindSafe for PerplexityMetric
impl Send for PerplexityMetric
impl Sync for PerplexityMetric
impl Unpin for PerplexityMetric
impl UnsafeUnpin for PerplexityMetric
impl UnwindSafe for PerplexityMetric
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
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
§impl<K, Q> Comparable<Q> for K
impl<K, Q> Comparable<Q> for K
§impl<K, Q> Equivalent<Q> for K
impl<K, Q> Equivalent<Q> for K
§fn equivalent(&self, key: &Q) -> bool
fn equivalent(&self, key: &Q) -> bool
key and return true if they are equal.impl<T> ErasedDestructor for Twhere
T: 'static,
§impl<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
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§impl<T> Pointable for T
impl<T> Pointable for T
§impl<T> PolicyExt for Twhere
T: ?Sized,
impl<T> PolicyExt for Twhere
T: ?Sized,
impl<T> Read<Exclusive, BecauseExclusive> for Twhere
T: ?Sized,
Source§impl<R, P> ReadPrimitive<R> for P
impl<R, P> ReadPrimitive<R> for P
Source§fn read_from_little_endian(read: &mut R) -> Result<Self, Error>
fn read_from_little_endian(read: &mut R) -> Result<Self, Error>
ReadEndian::read_from_little_endian().