Interface ISecondOrderGradientComputable<T, TInput, TOutput>
- Namespace
- AiDotNet.Interfaces
- Assembly
- AiDotNet.dll
Extended gradient computation interface for MAML meta-learning algorithms.
public interface ISecondOrderGradientComputable<T, TInput, TOutput> : IGradientComputable<T, TInput, TOutput>
Type Parameters
TThe numeric data type (e.g., float, double).
TInputThe input data type.
TOutputThe output data type.
- Inherited Members
Remarks
This interface extends IGradientComputable<T, TInput, TOutput> with second-order gradient computation capability required for full MAML (Model-Agnostic Meta-Learning).
For Beginners: While basic gradient computation tells you how to improve on a single task, second-order gradients tell you how changing your starting point would affect learning on that task.
Think of it like this:
- First-order: "If I start here, which direction improves this task?"
- Second-order: "If I started slightly differently, how would my entire learning trajectory change?"
This is computationally expensive but more accurate for meta-learning.
MAML Use Case: Full MAML uses second-order gradients to backpropagate through the inner loop adaptation, computing true meta-gradients that account for how the adaptation process itself changes. Reptile and first-order MAML approximate this with only first-order gradients.
Methods
ComputeSecondOrderGradients(List<(TInput input, TOutput target)>, TInput, TOutput, ILossFunction<T>, T)
Computes second-order gradients (Hessian-vector product) for full MAML meta-learning.
Vector<T> ComputeSecondOrderGradients(List<(TInput input, TOutput target)> adaptationSteps, TInput queryInput, TOutput queryTarget, ILossFunction<T> lossFunction, T innerLearningRate)
Parameters
adaptationStepsList<(TInput input, TOutput target)>The sequence of adaptation steps (support set training).
queryInputTInputThe query set input for evaluation after adaptation.
queryTargetTOutputThe query set target for evaluation after adaptation.
lossFunctionILossFunction<T>The loss function to use for gradient computation.
innerLearningRateTThe inner loop learning rate for adaptation.
Returns
- Vector<T>
The meta-gradient computed through the entire adaptation process.
Remarks
This computes the true MAML gradient by backpropagating through the inner loop adaptation. This requires computing second-order derivatives (gradients of gradients).
Algorithm: 1. Record the adaptation trajectory (parameter updates during inner loop) 2. Compute query loss gradient at adapted parameters 3. Backpropagate through each adaptation step using chain rule 4. Return gradient w.r.t. original (pre-adaptation) parameters
For Beginners: Full MAML asks: "If I change my starting point slightly, how does that affect my performance after adaptation?"
This requires tracking not just how to improve on this task, but how changing the starting point would have changed the whole adaptation process.
Example: If your starting point makes you learn faster, the second-order gradient captures that benefit. First-order approximations (like Reptile) miss this.
Computational Cost: Significantly more expensive than first-order MAML: - Requires storing intermediate computations from adaptation - Performs additional backward passes through adaptation steps - Memory scales with number of adaptation steps
Use only when accuracy is more important than speed.
Exceptions
- ArgumentNullException
If any parameter is null.
- ArgumentException
If adaptationSteps is empty.