Table of Contents

Class MetaClassifierBase<T>

Namespace
AiDotNet.Classification.Meta
Assembly
AiDotNet.dll

Base class for meta classifiers that wrap other classifiers.

public abstract class MetaClassifierBase<T> : ProbabilisticClassifierBase<T>, IProbabilisticClassifier<T>, IClassifier<T>, IFullModel<T, Matrix<T>, Vector<T>>, IModel<Matrix<T>, Vector<T>, ModelMetadata<T>>, IModelSerializer, ICheckpointableModel, IParameterizable<T, Matrix<T>, Vector<T>>, IFeatureAware, IFeatureImportance<T>, ICloneable<IFullModel<T, Matrix<T>, Vector<T>>>, IGradientComputable<T, Matrix<T>, Vector<T>>, IJitCompilable<T>

Type Parameters

T

The numeric data type used for calculations.

Inheritance
MetaClassifierBase<T>
Implements
IFullModel<T, Matrix<T>, Vector<T>>
IModel<Matrix<T>, Vector<T>, ModelMetadata<T>>
IParameterizable<T, Matrix<T>, Vector<T>>
ICloneable<IFullModel<T, Matrix<T>, Vector<T>>>
IGradientComputable<T, Matrix<T>, Vector<T>>
Derived
Inherited Members
Extension Methods

Remarks

Meta classifiers use other classifiers as base estimators to provide enhanced functionality like multi-class support, multi-label support, or ensemble voting.

For Beginners: Meta classifiers are "classifiers about classifiers":

Examples:

  • OneVsRest: Trains one classifier per class
  • OneVsOne: Trains one classifier per pair of classes
  • Voting: Combines predictions from multiple classifiers
  • Stacking: Uses classifier outputs as features for another classifier

They extend the capabilities of base classifiers.

Constructors

MetaClassifierBase(MetaClassifierOptions<T>?, Func<IClassifier<T>>?, IRegularization<T, Matrix<T>, Vector<T>>?)

Initializes a new instance of the MetaClassifierBase class.

protected MetaClassifierBase(MetaClassifierOptions<T>? options = null, Func<IClassifier<T>>? estimatorFactory = null, IRegularization<T, Matrix<T>, Vector<T>>? regularization = null)

Parameters

options MetaClassifierOptions<T>

Configuration options for the meta classifier.

estimatorFactory Func<IClassifier<T>>

Factory function to create base estimators.

regularization IRegularization<T, Matrix<T>, Vector<T>>

Optional regularization strategy.

Properties

EstimatorFactory

The base estimator factory function.

protected Func<IClassifier<T>>? EstimatorFactory { get; set; }

Property Value

Func<IClassifier<T>>

Options

Gets the meta classifier specific options.

protected MetaClassifierOptions<T> Options { get; }

Property Value

MetaClassifierOptions<T>

Methods

ApplyGradients(Vector<T>, T)

Applies pre-computed gradients to update the model parameters.

public override void ApplyGradients(Vector<T> gradients, T learningRate)

Parameters

gradients Vector<T>

The gradient vector to apply.

learningRate T

The learning rate for the update.

Remarks

Updates parameters using: θ = θ - learningRate * gradients

For Beginners: After computing gradients (seeing which direction to move), this method actually moves the model in that direction. The learning rate controls how big of a step to take.

Distributed Training: In DDP/ZeRO-2, this applies the synchronized (averaged) gradients after communication across workers. Each worker applies the same averaged gradients to keep parameters consistent.

ComputeGradients(Matrix<T>, Vector<T>, ILossFunction<T>?)

Computes gradients of the loss function with respect to model parameters for the given data, WITHOUT updating the model parameters.

public override Vector<T> ComputeGradients(Matrix<T> input, Vector<T> target, ILossFunction<T>? lossFunction = null)

Parameters

input Matrix<T>

The input data.

target Vector<T>

The target/expected output.

lossFunction ILossFunction<T>

The loss function to use for gradient computation. If null, uses the model's default loss function.

Returns

Vector<T>

A vector containing gradients with respect to all model parameters.

Remarks

This method performs a forward pass, computes the loss, and back-propagates to compute gradients, but does NOT update the model's parameters. The parameters remain unchanged after this call.

Distributed Training: In DDP/ZeRO-2, each worker calls this to compute local gradients on its data batch. These gradients are then synchronized (averaged) across workers before applying updates. This ensures all workers compute the same parameter updates despite having different data.

For Meta-Learning: After adapting a model on a support set, you can use this method to compute gradients on the query set. These gradients become the meta-gradients for updating the meta-parameters.

For Beginners: Think of this as "dry run" training: - The model sees what direction it should move (the gradients) - But it doesn't actually move (parameters stay the same) - You get to decide what to do with this information (average with others, inspect, modify, etc.)

Exceptions

InvalidOperationException

If lossFunction is null and the model has no default loss function.

CreateBaseEstimator()

Creates a new base estimator instance.

protected IClassifier<T> CreateBaseEstimator()

Returns

IClassifier<T>

A new classifier instance.

GetParameters()

Gets all model parameters as a single vector.

public override Vector<T> GetParameters()

Returns

Vector<T>

A vector containing all model parameters.

Remarks

This method returns a vector containing all model parameters for use with optimization algorithms or model comparison.

For Beginners: This method packages all the model's parameters into a single collection. This is useful for optimization algorithms that need to work with all parameters at once.

SetParameters(Vector<T>)

Sets the parameters for this model.

public override void SetParameters(Vector<T> parameters)

Parameters

parameters Vector<T>

A vector containing all model parameters.

Exceptions

ArgumentException

Thrown when the parameters vector has an incorrect length.

WithParameters(Vector<T>)

Creates a new instance of the model with specified parameters.

public override IFullModel<T, Matrix<T>, Vector<T>> WithParameters(Vector<T> parameters)

Parameters

parameters Vector<T>

A vector containing all model parameters.

Returns

IFullModel<T, Matrix<T>, Vector<T>>

A new model instance with the specified parameters.

Exceptions

ArgumentException

Thrown when the parameters vector has an incorrect length.