InputGradientHelper<T>

Helpers & Utilities · AiDotNet.Interpretability.Helpers

Provides unified input gradient computation for interpretability explainers.

For Beginners

This helper computes how the model’s output changes when you slightly change each input feature. These “input gradients” are essential for many interpretability methods like Integrated Gradients, GradCAM, and DeepLIFT.

The helper automatically chooses the best available method:

  1. Native Backpropagation: If the model is a neural network with backprop support,

uses the efficient built-in gradient computation.

  1. GradientTape: If the model is a tensor function written in engine ops, uses the

AiDotNet.Tensors gradient tape, verified once against a directional finite difference.

  1. Numerical Gradients: As a fallback, computes approximate gradients by

slightly perturbing each input (slower but works with any model).

Why input gradients matter:

  • They show the sensitivity of the output to each input feature
  • Positive gradient: increasing this feature increases the output
  • Negative gradient: increasing this feature decreases the output
  • Large magnitude: the feature has strong influence on the output

Constructors

Constructor Summary
InputGradientHelper(Func<Tensor<T>, Tensor<T>> tensorPredictFunction, double epsilon) Creates a gradient helper from a tensor prediction function.
InputGradientHelper(Func<Vector<T>, Vector<T>> predictFunction, double epsilon) Creates a gradient helper from a tensor prediction function.
InputGradientHelper(INeuralNetwork<T> neuralNetwork, double epsilon) Creates a gradient helper from a tensor prediction function.

Properties

Property Summary
bool SupportsExactGradients { get; } Gets whether this helper can compute exact gradients (via backprop or autodiff).
string GradientMethod { get; } Gets the method name used for gradient computation.

Methods

Method Summary
Func<Tensor<T>, int, Tensor<T>> CreateTensorGradientFunction() Creates a tensor gradient function for image-based explainers.
Func<Vector<T>, int, Vector<T>> CreateGradientFunction() Creates a gradient function suitable for Integrated Gradients or similar methods.
Tensor<T> ComputeGradientTensor(Tensor<T> input, int outputIndex) Computes gradients for a tensor input.
Vector<T> ComputeGradient(Vector<T> input, int outputIndex) Computes gradients of the output with respect to the input.
Vector<T> ComputeIntegratedGradients(Vector<T> input, Vector<T> baseline, int numSteps, int outputIndex) Computes Integrated Gradients attributions directly.
Vector<T>[] ComputePathGradients(Vector<T> input, Vector<T> baseline, int numSteps, int outputIndex) Computes gradients along a path from baseline to input (for Integrated Gradients).