concision_traits/entropy.rs
1/*
2 Appellation: entropy <module>
3 Created At: 2025.11.26:13:13:33
4 Contrib: @FL03
5*/
6
7/// A trait for computing the cross-entropy loss of a tensor or array
8pub trait CrossEntropy {
9 type Output;
10
11 fn cross_entropy(&self) -> Self::Output;
12}
13
14/*
15 ************* Implementations *************
16*/
17use ndarray::{ArrayBase, Data, Dimension};
18use num_traits::{Float, FromPrimitive};
19
20impl<A, S, D> CrossEntropy for ArrayBase<S, D, A>
21where
22 A: 'static + Float + FromPrimitive,
23 D: Dimension,
24 S: Data<Elem = A>,
25{
26 type Output = A;
27
28 fn cross_entropy(&self) -> Self::Output {
29 self.ln().mean().unwrap()
30 }
31}