Interface IInputGradientComputable<T>
- Namespace
- AiDotNet.Interfaces
- Assembly
- AiDotNet.dll
Interface for models that support computing gradients with respect to input data.
public interface IInputGradientComputable<T>
Type Parameters
TThe numeric data type (e.g., float, double).
Remarks
This interface enables models to compute how their output changes with respect to input modifications. Unlike IGradientComputable<T, TInput, TOutput> which computes gradients for model parameters, this interface computes gradients for the input itself.
Use Cases:
- Adversarial Examples: Generate minimal perturbations that cause misclassification
- Saliency Maps: Visualize which input features most affect the output
- Input Attribution: Understand model predictions through input sensitivity
- Gradient Penalties: WGAN-GP and other regularization techniques
For Beginners: When training a model, we compute gradients to adjust the model's internal parameters (weights). This interface instead computes how sensitive the output is to changes in the input data.
For example, if you have an image classifier:
- Parameter gradients tell us how to adjust weights to improve accuracy
- Input gradients tell us which pixels, if changed, would most affect the prediction
This is essential for adversarial robustness testing - we can find the smallest image change that fools the classifier.
Methods
ComputeInputGradient(Tensor<T>, Tensor<T>)
Computes the gradient of the model output with respect to the input using tensor format.
Tensor<T> ComputeInputGradient(Tensor<T> input, Tensor<T> outputGradient)
Parameters
inputTensor<T>The input tensor for which to compute gradients.
outputGradientTensor<T>The gradient tensor with respect to the output.
Returns
- Tensor<T>
The gradient tensor with respect to the input.
ComputeInputGradient(Vector<T>, Vector<T>)
Computes the gradient of the model output with respect to the input.
Vector<T> ComputeInputGradient(Vector<T> input, Vector<T> outputGradient)
Parameters
inputVector<T>The input for which to compute gradients.
outputGradientVector<T>The gradient with respect to the output (typically from a loss function).
Returns
- Vector<T>
The gradient with respect to the input.
Remarks
This method performs backpropagation through the model to compute input gradients.
The outputGradient represents how much we "care" about each output dimension,
typically derived from a loss function.
For Adversarial Attacks:
Set outputGradient to emphasize the target class (for targeted attacks)
or the true class (for untargeted attacks), then use the returned input gradient
to perturb the input in a direction that maximizes misclassification.