pub struct MultiClassLogLoss {
pub num_classes: usize,
/* private fields */
}Expand description
Multi-class LogLoss (Softmax Cross-Entropy)
Used for multi-class classification where targets are class indices (0, 1, …, K-1).
§Note
This loss function works differently from regression/binary losses:
gradient()andhessian()compute for a single class given all raw predictions- Training builds K trees per round (one per class)
compute_gradient_for_class()is the primary method for multi-class training
Fields§
§num_classes: usizeNumber of classes
Implementations§
Source§impl MultiClassLogLoss
impl MultiClassLogLoss
Sourcepub fn gradient_hessian_for_class(
&self,
target_class: usize,
class_idx: usize,
raw_predictions: &[f32],
) -> (f32, f32)
pub fn gradient_hessian_for_class( &self, target_class: usize, class_idx: usize, raw_predictions: &[f32], ) -> (f32, f32)
Sourcepub fn gradient_hessian_all_classes(
&self,
target_class: usize,
raw_predictions: &[f32],
) -> (Vec<f32>, Vec<f32>)
pub fn gradient_hessian_all_classes( &self, target_class: usize, raw_predictions: &[f32], ) -> (Vec<f32>, Vec<f32>)
Compute gradients and hessians for all classes at once
More efficient when you need all K gradients for a sample.
Sourcepub fn compute_gradients_batch(
&self,
class_idx: usize,
targets: &[f32],
predictions: &[f32],
sample_indices: &[usize],
gradients: &mut [f32],
hessians: &mut [f32],
)
pub fn compute_gradients_batch( &self, class_idx: usize, targets: &[f32], predictions: &[f32], sample_indices: &[usize], gradients: &mut [f32], hessians: &mut [f32], )
Compute gradients and hessians for a batch of samples for a specific class
This is optimized for the training loop where we compute gradients for all training samples for one class at a time.
§Arguments
class_idx- The class we’re computing gradients fortargets- Target class indices for all samplespredictions- Flattened predictions matrix: predictions[sample * num_classes + class]sample_indices- Indices of samples to processgradients- Output buffer for gradients (indexed by sample)hessians- Output buffer for hessians (indexed by sample)
Sourcepub fn initial_predictions(&self, targets: &[f32]) -> Vec<f32>
pub fn initial_predictions(&self, targets: &[f32]) -> Vec<f32>
Compute initial predictions for all classes (log of class priors)
Uses the class frequencies as initial probabilities. Sets f_k = log(p_k) so that softmax(f) = class frequencies.
§Math
If f_k = log(p_k), then:
- exp(f_k) = p_k
- softmax_k = p_k / sum(p_j) = p_k / 1 = p_k
This ensures the model starts with predictions matching the class distribution.
Sourcepub fn num_classes(&self) -> usize
pub fn num_classes(&self) -> usize
Get number of classes
Trait Implementations§
Source§impl Clone for MultiClassLogLoss
impl Clone for MultiClassLogLoss
Source§fn clone(&self) -> MultiClassLogLoss
fn clone(&self) -> MultiClassLogLoss
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreAuto Trait Implementations§
impl Freeze for MultiClassLogLoss
impl RefUnwindSafe for MultiClassLogLoss
impl Send for MultiClassLogLoss
impl Sync for MultiClassLogLoss
impl Unpin for MultiClassLogLoss
impl UnwindSafe for MultiClassLogLoss
Blanket Implementations§
Source§impl<T> ArchivePointee for T
impl<T> ArchivePointee for T
Source§type ArchivedMetadata = ()
type ArchivedMetadata = ()
Source§fn pointer_metadata(
_: &<T as ArchivePointee>::ArchivedMetadata,
) -> <T as Pointee>::Metadata
fn pointer_metadata( _: &<T as ArchivePointee>::ArchivedMetadata, ) -> <T as Pointee>::Metadata
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
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
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> ⓘ
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> ⓘ
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 moreSource§impl<T> Key for Twhere
T: Clone,
impl<T> Key for Twhere
T: Clone,
Source§impl<T> LayoutRaw for T
impl<T> LayoutRaw for T
Source§fn layout_raw(_: <T as Pointee>::Metadata) -> Result<Layout, LayoutError>
fn layout_raw(_: <T as Pointee>::Metadata) -> Result<Layout, LayoutError>
Source§impl<T, N1, N2> Niching<NichedOption<T, N1>> for N2
impl<T, N1, N2> Niching<NichedOption<T, N1>> for N2
Source§unsafe fn is_niched(niched: *const NichedOption<T, N1>) -> bool
unsafe fn is_niched(niched: *const NichedOption<T, N1>) -> bool
Source§fn resolve_niched(out: Place<NichedOption<T, N1>>)
fn resolve_niched(out: Place<NichedOption<T, N1>>)
out indicating that a T is niched.