concision_core/func/loss/
utils.rs

1/*
2    Appellation: utils <loss>
3    Contrib: FL03 <jo3mccain@icloud.com>
4*/
5use crate::math::{Abs, Squared};
6use nd::prelude::*;
7use nd::{Data, ScalarOperand};
8use num::traits::{FromPrimitive, Num, Pow, Signed};
9
10/// A functional implementation of the mean absolute error loss function which compares two similar
11/// [arrays](ndarray::ArrayBase)
12pub fn mae<A, S, D>(pred: &ArrayBase<S, D>, target: &ArrayBase<S, D>) -> Option<A>
13where
14    A: FromPrimitive + Num + ScalarOperand + Signed,
15    D: Dimension,
16    S: Data<Elem = A>,
17{
18    (pred - target).abs().mean()
19}
20/// A functional implementation of the mean squared error loss function that compares two similar
21/// [arrays](ndarray::ArrayBase)
22pub fn mse<A, S, D>(pred: &ArrayBase<S, D>, target: &ArrayBase<S, D>) -> Option<A>
23where
24    A: FromPrimitive + Num + Pow<i32, Output = A> + ScalarOperand,
25    D: Dimension,
26    S: Data<Elem = A>,
27{
28    (pred - target).sqrd().mean()
29}