burn-train 0.22.1

Training crate for the Burn framework
Documentation
use std::sync::Arc;

use super::super::{
    MetricAttributes, MetricMetadata, NumericAttributes, NumericEntry,
    state::{FormatOptions, NumericMetricState},
};
use crate::metric::{Metric, MetricName, Numeric, SerializedEntry};
use burn_core::tensor::TensorReadError;

/// Metric for the length of the last completed episode.
#[derive(Clone)]
pub struct EpisodeLengthMetric {
    name: MetricName,
    state: NumericMetricState,
}

impl EpisodeLengthMetric {
    /// Creates a new episode length metric.
    pub fn new() -> Self {
        Self {
            name: Arc::new("Episode length".to_string()),
            state: NumericMetricState::new(),
        }
    }
}

impl Default for EpisodeLengthMetric {
    fn default() -> Self {
        Self::new()
    }
}

/// The [EpisodeLengthMetric](EpisodeLengthMetric) input type.
#[derive(new)]
pub struct EpisodeLengthInput {
    ep_len: f64,
}

impl Metric for EpisodeLengthMetric {
    type Input = EpisodeLengthInput;

    fn update(
        &mut self,
        item: &EpisodeLengthInput,
        _metadata: &MetricMetadata,
    ) -> Result<SerializedEntry, TensorReadError> {
        self.state.update(item.ep_len, 1);
        Ok(self
            .state
            .compute_update(FormatOptions::new(self.name()).precision(0)))
    }

    fn compute(&mut self) -> Result<SerializedEntry, TensorReadError> {
        Ok(self
            .state
            .compute_final(FormatOptions::new(self.name()).precision(0)))
    }

    fn clear(&mut self) {
        self.state.reset()
    }

    fn name(&self) -> MetricName {
        self.name.clone()
    }

    fn attributes(&self) -> MetricAttributes {
        NumericAttributes {
            unit: Some(String::from("steps")),
            higher_is_better: true,
        }
        .into()
    }
}

impl Numeric for EpisodeLengthMetric {
    fn value(&self) -> Option<NumericEntry> {
        Some(self.state.current_value())
    }

    fn running_value(&self) -> Option<NumericEntry> {
        Some(self.state.running_value())
    }

    fn final_value(&self) -> NumericEntry {
        self.state.final_value()
    }
}