binary_crossentropy_gradient

Function binary_crossentropy_gradient 

Source
pub fn binary_crossentropy_gradient<F>(
    predictions: &ArrayView1<'_, F>,
    targets: &ArrayView1<'_, F>,
) -> LinalgResult<Array1<F>>
Expand description

Calculate the gradient of binary cross-entropy with respect to predictions

Computes the gradient of binary cross-entropy loss function with respect to predictions. This is a common gradient calculation in binary classification tasks.

§Arguments

  • predictions - Predicted probabilities (must be between 0 and 1)
  • targets - Target values (ground truth, must be 0 or 1)

§Returns

  • The gradient of binary cross-entropy with respect to predictions

§Examples

use scirs2_core::ndarray::array;
use scirs2_linalg::gradient::binary_crossentropy_gradient;
use approx::assert_relative_eq;

let predictions = array![0.7, 0.3, 0.9];
let targets = array![1.0, 0.0, 1.0];

let gradient = binary_crossentropy_gradient(&predictions.view(), &targets.view()).unwrap();

// gradients = -targets/predictions + (1-targets)/(1-predictions)
// = -[1.0, 0.0, 1.0]/[0.7, 0.3, 0.9] + [0.0, 1.0, 0.0]/[0.3, 0.7, 0.1]
// = [-1.428..., 0.0, -1.111...] + [0.0, 1.428..., 0.0]
// = [-1.428..., 1.428..., -1.111...]

assert_relative_eq!(gradient[0], -1.428571, epsilon = 1e-6);
assert_relative_eq!(gradient[1], 1.428571, epsilon = 1e-6);
assert_relative_eq!(gradient[2], -1.111111, epsilon = 1e-6);