#[derive(Debug)]
pub enum MetricsStdError {
MetricError,
}
impl From<native_neural_network::metrics::MetricError> for MetricsStdError {
fn from(_e: native_neural_network::metrics::MetricError) -> Self {
MetricsStdError::MetricError
}
}
pub fn mse(a: &[f32], b: &[f32]) -> Result<f32, MetricsStdError> {
native_neural_network::metrics::mse_f32(a, b).map_err(|e| e.into())
}
pub fn mae(a: &[f32], b: &[f32]) -> Result<f32, MetricsStdError> {
native_neural_network::metrics::mae_f32(a, b).map_err(|e| e.into())
}
pub fn argmax(slice: &[f32]) -> Result<usize, MetricsStdError> {
native_neural_network::metrics::argmax_f32(slice).ok_or(MetricsStdError::MetricError)
}
pub fn accuracy_top1_from_one_hot(preds: &[f32], targets: &[f32]) -> Result<f32, MetricsStdError> {
native_neural_network::metrics::accuracy_top1_from_one_hot_f32(preds, targets)
.map_err(|e| e.into())
}
pub fn cross_entropy_from_probabilities(
preds: &[f32],
targets: &[f32],
eps: f32,
) -> Result<f32, MetricsStdError> {
native_neural_network::metrics::cross_entropy_from_probabilities_f32(preds, targets, eps)
.map_err(|e| e.into())
}
impl core::fmt::Display for MetricsStdError {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
write!(f, "MetricsStdError::{:?}", self)
}
}
impl std::error::Error for MetricsStdError {}
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct RunningMeanStd {
pub sum: f32,
pub count: u64,
}
impl RunningMeanStd {
pub const fn new() -> Self {
RunningMeanStd { sum: 0.0, count: 0 }
}
pub fn update(&mut self, value: f32) {
let mut inner = native_neural_network::metrics::RunningMean {
sum: self.sum,
count: self.count,
};
inner.update(value);
self.sum = inner.sum;
self.count = inner.count;
}
pub fn merge(&mut self, other: RunningMeanStd) {
let mut inner = native_neural_network::metrics::RunningMean {
sum: self.sum,
count: self.count,
};
inner.merge(native_neural_network::metrics::RunningMean {
sum: other.sum,
count: other.count,
});
self.sum = inner.sum;
self.count = inner.count;
}
pub fn value(&self) -> Option<f32> {
let inner = native_neural_network::metrics::RunningMean {
sum: self.sum,
count: self.count,
};
inner.value()
}
}
impl Default for RunningMeanStd {
fn default() -> Self {
Self::new()
}
}