pub struct CrossEntropyLoss { /* private fields */ }
Expand description
Cross-entropy loss function.
The cross-entropy loss is defined as: L = -sum(y_true * log(y_pred)) where y_true are target probabilities and y_pred are predicted probabilities.
It is commonly used for classification problems.
§Examples
use scirs2_neural::losses::CrossEntropyLoss;
use scirs2_neural::losses::Loss;
use ndarray::{Array, arr1, arr2};
let ce = CrossEntropyLoss::new(1e-10);
// One-hot encoded targets and softmax'd predictions for a 3-class problem
let predictions = arr2(&[
[0.7, 0.2, 0.1], // First sample, class probabilities
[0.3, 0.6, 0.1] // Second sample, class probabilities
]).into_dyn();
let targets = arr2(&[
[1.0, 0.0, 0.0], // First sample, true class is 0
[0.0, 1.0, 0.0] // Second sample, true class is 1
]).into_dyn();
// Forward pass to calculate loss
let loss = ce.forward(&predictions, &targets).unwrap();
// Backward pass to calculate gradients
let gradients = ce.backward(&predictions, &targets).unwrap();
Implementations§
Trait Implementations§
Source§impl Clone for CrossEntropyLoss
impl Clone for CrossEntropyLoss
Source§fn clone(&self) -> CrossEntropyLoss
fn clone(&self) -> CrossEntropyLoss
Returns a copy of the value. Read more
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source
. Read moreSource§impl Debug for CrossEntropyLoss
impl Debug for CrossEntropyLoss
Source§impl Default for CrossEntropyLoss
impl Default for CrossEntropyLoss
Source§impl<F: Float + Debug> Loss<F> for CrossEntropyLoss
impl<F: Float + Debug> Loss<F> for CrossEntropyLoss
impl Copy for CrossEntropyLoss
Auto Trait Implementations§
impl Freeze for CrossEntropyLoss
impl RefUnwindSafe for CrossEntropyLoss
impl Send for CrossEntropyLoss
impl Sync for CrossEntropyLoss
impl Unpin for CrossEntropyLoss
impl UnwindSafe for CrossEntropyLoss
Blanket Implementations§
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
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
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