OnlineLearningModelBase<T>

Base Classes · AiDotNet.OnlineLearning

Abstract base class for online (incremental) learning models.

Implements: IModelShape, IOnlineLearningModel<T>, IParameterChunkSource<T>, IParameterManifestProvider

Known derived types: OnlinePassiveAggressiveClassifier<T>, OnlinePassiveAggressiveRegressor<T>, OnlineSGDClassifier<T>, OnlineSGDRegressor<T>

For Beginners

This class contains shared code that all online learning models need, so each specific model doesn’t have to reimplement it.

Key shared functionality:

  • Tracking how many samples have been seen
  • Managing the learning rate (step size for updates)
  • Converting between single-sample and batch updates
  • Standard IFullModel interface implementation

How It Works

This base class provides common functionality for online learning models including sample counting, learning rate scheduling, and incremental updates.

Properties

Property Summary
ILossFunction<T> DefaultLossFunction { get; } Gets the default loss function.
ParameterLayoutSnapshot ParameterLayout { get; }
bool IsTrained { get; } Gets whether the model is trained (has seen at least one sample).
bool SupportsParameterInitialization { get; }
long ParameterCount { get; }
string[] FeatureNames { get; set; } Gets or sets the feature names.

Methods

Method Summary
Dictionary<string, T> GetFeatureImportance() Gets the feature importance scores.
DynamicShapeInfo GetDynamicShapeInfo()
IEnumerable<ParameterChunk<T>> GetParameterStateChunks() Enumerates canonical persistent state without flattening tensor-backed components.
IEnumerable<Tensor<T>> GetParameterChunks() Streams state in the same order as the parameter manifest.
IEnumerable<int> GetActiveFeatureIndices() Gets the indices of features that are actively used in the model.
IFullModel<T, Matrix<T>, Vector<T>> Clone() Creates a clone of the model.
IFullModel<T, Matrix<T>, Vector<T>> DeepCopy() Creates a deep copy of the model.
IFullModel<T, Matrix<T>, Vector<T>> WithParameters(Vector<T> parameters) Creates a new instance of the model with specified parameters.
ModelMetadata<T> GetModelMetadata() Gets the model type.
T GetLearningRate() Gets the current learning rate based on the schedule.
T PredictSingle(Vector<T> x) Predicts the target value for a single sample.
Vector<T> ComputeGradients(Matrix<T> input, Vector<T> target, ILossFunction<T> lossFunction) Computes gradients for the given input and target.
Vector<T> GetParameters()
Vector<T> Predict(Matrix<T> input) Standard prediction - returns predictions for all samples.
Vector<T> SanitizeParameters(Vector<T> parameters)
bool IsFeatureUsed(int featureIndex) Determines whether a specific feature is used in the model.
byte[] Serialize() Serializes the model to a byte array.
int[] GetInputShape() Saves the model to a file.
int[] GetOutputShape()
long GetSampleCount() Gets the number of samples the model has seen.
void ApplyGradients(Vector<T> gradients, T learningRate) Applies gradients to update model parameters.
void Deserialize(byte[] modelData) Deserializes the model from a byte array.
void Dispose()
void LoadModel(string filePath) Loads the model from a file.
void LoadState(Stream stream) Loads the model’s state from a stream.
void PartialFit(Matrix<T> x, Vector<T> y) Updates the model with a mini-batch of training examples.
void PartialFit(Vector<T> x, T y) Updates the model with a mini-batch of training examples.
void Reset() Resets the model to its initial state.
void SaveModel(string filePath)
void SaveState(Stream stream) Saves the model’s state to a stream.
void SetActiveFeatureIndices(IEnumerable<int> featureIndices) Sets the active feature indices for this model.
void SetParameterChunks(IEnumerable<Tensor<T>> chunks) Restores tensor-backed chunks on their device and retains custom-setter semantics.
void SetParameters(Vector<T> parameters)
void Train(Matrix<T> x, Vector<T> y) Standard model training - equivalent to PartialFit.