pub struct TensorLossFunction { /* private fields */ }Expand description
Common loss functions for tensor computations with gradient support.
§Examples
use ipfrs_tensorlogic::loss_function::{TensorLossFunction, LossConfig, LossType, Reduction};
let config = LossConfig {
loss_type: LossType::MSE,
reduction: Reduction::Mean,
..LossConfig::default()
};
let mut loss_fn = TensorLossFunction::new(config);
let preds = vec![1.0, 2.0, 3.0];
let targets = vec![1.5, 2.5, 3.5];
let loss = loss_fn.forward(&preds, &targets).expect("example: should succeed in docs");
assert!((loss - 0.25).abs() < 1e-10); // mean of [0.25, 0.25, 0.25]Implementations§
Source§impl TensorLossFunction
impl TensorLossFunction
Sourcepub fn new(config: LossConfig) -> Self
pub fn new(config: LossConfig) -> Self
Create a new loss function with the given configuration.
Sourcepub fn compute(
&mut self,
predictions: &[f64],
targets: &[f64],
) -> Result<Vec<f64>, String>
pub fn compute( &mut self, predictions: &[f64], targets: &[f64], ) -> Result<Vec<f64>, String>
Compute per-element loss values.
Returns an error if predictions and targets have different lengths.
Sourcepub fn reduce(&self, losses: &[f64]) -> f64
pub fn reduce(&self, losses: &[f64]) -> f64
Apply the configured reduction to a slice of per-element losses.
Sourcepub fn forward(
&mut self,
predictions: &[f64],
targets: &[f64],
) -> Result<f64, String>
pub fn forward( &mut self, predictions: &[f64], targets: &[f64], ) -> Result<f64, String>
Compute loss and reduce in one call.
Sourcepub fn gradient(
&mut self,
predictions: &[f64],
targets: &[f64],
) -> Result<Vec<f64>, String>
pub fn gradient( &mut self, predictions: &[f64], targets: &[f64], ) -> Result<Vec<f64>, String>
Compute dL/d(prediction) per element.
Returns an error if predictions and targets have different lengths.
Sourcepub fn mse(a: &[f64], b: &[f64]) -> f64
pub fn mse(a: &[f64], b: &[f64]) -> f64
Static helper: compute mean squared error between two slices.
Returns 0.0 if slices are empty or have different lengths.
Sourcepub fn mae(a: &[f64], b: &[f64]) -> f64
pub fn mae(a: &[f64], b: &[f64]) -> f64
Static helper: compute mean absolute error between two slices.
Returns 0.0 if slices are empty or have different lengths.
Sourcepub fn stats(&self) -> LossFunctionStats
pub fn stats(&self) -> LossFunctionStats
Return a snapshot of accumulated statistics.
Auto Trait Implementations§
impl Freeze for TensorLossFunction
impl RefUnwindSafe for TensorLossFunction
impl Send for TensorLossFunction
impl Sync for TensorLossFunction
impl Unpin for TensorLossFunction
impl UnsafeUnpin for TensorLossFunction
impl UnwindSafe for TensorLossFunction
Blanket Implementations§
impl<T> Allocation for T
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
Mutably borrows from an owned value. Read more
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§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>
Converts
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>
Converts
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