pub fn compute_gradients_and_hessians(
	class_index: usize,
	// (n_examples)
	gradients: &mut [f32],
	// (n_examples)
	hessians: &mut [f32],
	// (n_examples)
	labels: &[Option<NonZeroUsize>],
	// (n_trees_per_round, n_examples)
	logits: ArrayView2<f32>,
) {
	pzip!(gradients, hessians, logits.axis_iter(Axis(0)), labels).for_each(
		|(gradient, hessian, logits, label)| {
			let max = logits.iter().fold(std::f32::MIN, |a, &b| f32::max(a, b));
			let mut sum = 0.0;
			for logit in logits.iter() {
				sum += (*logit - max).exp();
			}
			let prediction = (logits[class_index] - max).exp() / sum;
			let label = label.unwrap().get() - 1;
			let label = if label == class_index { 1.0 } else { 0.0 };
			*gradient = prediction - label;
			*hessian = prediction * (1.0 - prediction);
		},
	);
}
/// This function is used by the common train function to compute the gradients and hessian after each round.
This function is used by the common train function to compute the gradients and hessian after each round.
This function is used by the common train function to compute the gradients and hessian after each round .
pub fn compute_gradients_and_hessians(
	class_index: usize,
	// (n_examples)
	gradients: &mut [f32],
	// (n_examples)
	hessians: &mut [f32],
	// (n_examples)
	labels: &[Option<NonZeroUsize>],
	// (n_trees_per_round, n_examples)
	logits: ArrayView2<f32>,
) {
	pzip!(gradients, hessians, logits.axis_iter(Axis(0)), labels).for_each(
		|(gradient, hessian, logits, label)| {
			let max = logits.iter().fold(std::f32::MIN, |a, &b| f32::max(a, b));
			let mut sum = 0.0;
			for logit in logits.iter() {
				sum += (*logit - max).exp();
			}
			let prediction = (logits[class_index] - max).exp() / sum;
			let label = label.unwrap().get() - 1;
			let label = if label == class_index { 1.0 } else { 0.0 };
			*gradient = prediction - label;
			*hessian = prediction * (1.0 - prediction);
		},
	);
}
pub fn compute_gradients_and_hessians ( class_index : usize , // (n_examples) gradients : & mut [ f32 ] , // (n_examples) hessians : & mut [ f32 ] , // (n_examples) labels : & [ Option < NonZeroUsize > ] , // (n_trees_per_round, n_examples) logits : ArrayView2 < f32 > , ) { pzip ! ( gradients hessians logits axis_iter ( Axis ( 0 ) ) labels ) . for_each ( | ( gradient , hessian , logits , label ) | { let max = logits . iter ( ) . fold ( std :: f32 :: MIN , | a , & b | f32 :: max ( a , b ) ) ; let mut sum = 0.0 ; for logit in logits . iter ( ) { sum += ( * logit - max ) . exp ( ) ; } let prediction = ( logits [ class_index ] - max ) . exp ( ) / sum ; let label = label . unwrap ( ) . get ( ) - 1 ; let label = if label == class_index { 1.0 } else { 0.0 } ; * gradient = prediction - label ; * hessian = prediction * ( 1.0 - prediction ) ; } , ) ; }
This function is used by the common train function to compute the gradients and hessian after each round.
This function is used by the common train function to compute the gradients and hessian after each round .
class_index
usize
gradients
&mut [f32]
hessians
&mut [f32]
labels
&[Option<NonZeroUsize>]
logits
ArrayView2<f32>