Class VectorModel<T>
Represents a linear model that uses a vector of coefficients to make predictions.
public class VectorModel<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>, IInterpretableModel<T>
Type Parameters
TThe numeric type used for calculations, typically float or double.
- Inheritance
-
VectorModel<T>
- Implements
- Inherited Members
- Extension Methods
Remarks
This class implements a simple linear model where predictions are made by computing the dot product of the input features and a vector of coefficients. It provides methods for training the model using linear regression, evaluating predictions, and genetic algorithm operations like mutation and crossover. This model is useful for linear regression problems and can serve as a building block for more complex models.
For Beginners: This is a simple linear model that makes predictions by multiplying each input by a weight and adding them up.
When using this model:
- Each input feature has a corresponding coefficient (weight)
- Predictions are made by multiplying each input by its coefficient and summing the results
- The model can be trained using linear regression on example data
- It supports genetic algorithm operations for optimization
For example, if predicting house prices, the model might learn that: price = 50,000 * bedrooms + 100 * square_feet + 20,000 * bathrooms
This is one of the simplest and most interpretable machine learning models, making it a good starting point for many problems.
Constructors
VectorModel(Vector<T>, ILossFunction<T>?)
public VectorModel(Vector<T> coefficients, ILossFunction<T>? lossFunction = null)
Parameters
coefficientsVector<T>lossFunctionILossFunction<T>
Fields
_baseModel
protected IFullModel<T, Matrix<T>, Vector<T>>? _baseModel
Field Value
- IFullModel<T, Matrix<T>, Vector<T>>
_enabledMethods
protected readonly HashSet<InterpretationMethod> _enabledMethods
Field Value
_fairnessMetrics
protected readonly List<FairnessMetric> _fairnessMetrics
Field Value
_sensitiveFeatures
protected Vector<int>? _sensitiveFeatures
Field Value
- Vector<int>
Properties
Coefficients
Gets the vector of coefficients used by the model.
public Vector<T> Coefficients { get; }
Property Value
- Vector<T>
A Vector<T> containing the model's coefficients.
Remarks
This property contains the vector of coefficients that the model uses to make predictions. Each coefficient corresponds to a feature in the input data and represents the weight or importance of that feature in the model's predictions. The coefficients are learned during training and are used to compute the dot product with input features when making predictions.
For Beginners: This contains the weights that the model applies to each input feature.
The coefficients:
- Are the numbers that multiply each input feature
- Determine how much each feature contributes to the prediction
- Are learned during training to minimize prediction error
For example, in a house price prediction model:
- A coefficient of 50,000 for bedrooms means each bedroom adds $50,000 to the predicted price
- A coefficient of 100 for square feet means each square foot adds $100 to the predicted price
These coefficients make the model interpretable - you can see exactly how each feature affects the prediction.
Complexity
Gets the complexity of the model.
public int Complexity { get; }
Property Value
- int
An integer representing the model's complexity.
Remarks
This property returns the number of non-zero coefficients in the model, which provides a simple measure of the model's complexity. Lower complexity can indicate a more generalizable model that's less likely to overfit.
For Beginners: This property tells you how complex the model is.
The complexity:
- Is measured by counting how many non-zero coefficients the model uses
- Lower complexity often means better generalization to new data
- Higher complexity might mean the model is overfitting to training data
For example, if a model has coefficients [0.5, 0, 0, 2.0, 0.3], its complexity would be 3, since only three coefficients are non-zero and actually contribute to predictions.
This is useful for comparing different models and for regularization approaches that aim to reduce model complexity.
DefaultLossFunction
Gets the default loss function used by this model for gradient computation.
public ILossFunction<T> DefaultLossFunction { get; }
Property Value
Remarks
For VectorModel (linear regression), the default loss function is Mean Squared Error (MSE), which is the standard loss function for regression problems.
FeatureCount
Gets the number of features used by the model.
public int FeatureCount { get; }
Property Value
- int
An integer representing the number of input features.
Remarks
This property returns the number of features that the model uses, which is determined by the length of the coefficients vector. Each coefficient corresponds to a feature in the input data, so the number of coefficients equals the number of features the model expects when making predictions.
For Beginners: This tells you how many input variables the model uses.
The feature count:
- Is equal to the length of the coefficients vector
- Tells you how many input values the model expects
- Must match the number of features in your input data
For example, if FeatureCount is 3, the model expects three input values for each prediction (like bedrooms, bathrooms, and square footage).
This property is useful when:
- Checking if your input data has the right number of features
- Understanding the complexity of the model
- Preparing data for prediction
ParameterCount
Gets the number of trainable parameters in the model.
public int ParameterCount { get; }
Property Value
- int
An integer representing the number of parameters.
Remarks
For a VectorModel, the number of parameters equals the number of coefficients, as each coefficient is a trainable parameter.
For Beginners: This tells you how many weights the model has to learn.
For a linear model:
- Each coefficient is a parameter that can be trained
- More parameters generally means more complexity
- The parameter count equals the number of features
SupportsJitCompilation
Gets a value indicating whether this model supports JIT compilation.
public bool SupportsJitCompilation { get; }
Property Value
Remarks
VectorModel supports JIT compilation by converting its linear regression computation (matrix-vector multiplication) to a computation graph. This enables 5-10x faster inference.
For Beginners: JIT compilation makes predictions much faster.
Linear regression is simple: output = input @ coefficients With JIT, this computation is compiled to optimized native code for maximum speed.
Especially beneficial for:
- Processing large datasets
- Real-time prediction systems
- Production deployments
Methods
ApplyGradients(Vector<T>, T)
Applies pre-computed gradients to update the model parameters (coefficients).
public void ApplyGradients(Vector<T> gradients, T learningRate)
Parameters
gradientsVector<T>The gradient vector to apply.
learningRateTThe learning rate for the update.
Remarks
Updates coefficients using: coefficients = coefficients - learningRate * gradients
For Beginners: After computing gradients (seeing which direction to adjust each coefficient), this method actually adjusts them. The learning rate controls how big of an adjustment to make.
In distributed training, this applies the averaged gradients from multiple machines to ensure all machines keep their models synchronized.
Exceptions
- ArgumentNullException
If gradients is null.
- ArgumentException
If gradient vector length doesn't match coefficient count.
Clone()
Creates a shallow copy of this model.
public IFullModel<T, Matrix<T>, Vector<T>> Clone()
Returns
- IFullModel<T, Matrix<T>, Vector<T>>
A new instance with the same coefficients.
Remarks
This method creates a shallow copy of the model. For VectorModel, this is equivalent to DeepCopy because the only state that needs to be copied is the Coefficients vector.
For Beginners: This method creates a duplicate of the model.
For the VectorModel:
- Clone and DeepCopy do the same thing
- Both create a new model with a copy of the coefficients
The Clone method is provided for compatibility with the IFullModel interface.
ComputeGradients(Matrix<T>, Vector<T>, ILossFunction<T>?)
Computes gradients of the loss function with respect to model parameters WITHOUT updating parameters.
public Vector<T> ComputeGradients(Matrix<T> input, Vector<T> target, ILossFunction<T>? lossFunction = null)
Parameters
inputMatrix<T>The input data matrix.
targetVector<T>The target/expected output vector.
lossFunctionILossFunction<T>The loss function to use. If null, uses the model's default loss function.
Returns
- Vector<T>
A vector containing gradients with respect to all model parameters (coefficients).
Remarks
This method computes the gradient of the loss function with respect to the model's coefficients. For a linear model: y_pred = coefficients · x The gradient is computed as: ∂L/∂coefficients = (1/n) * X^T * ∂L/∂y_pred
For Beginners: This calculates how to adjust each coefficient to reduce the prediction error, but it doesn't actually change the coefficients. This is useful for: - Distributed training: average gradients from multiple machines before updating - Custom optimization: use advanced optimizers like Adam or RMSprop - Analysis: understand which coefficients need the most adjustment
Exceptions
- ArgumentNullException
If input or target is null.
- ArgumentException
If input and target dimensions don't match.
ConfigureFairness(Vector<int>, params FairnessMetric[])
Configures fairness evaluation settings.
public virtual void ConfigureFairness(Vector<int> sensitiveFeatures, params FairnessMetric[] fairnessMetrics)
Parameters
sensitiveFeaturesVector<int>fairnessMetricsFairnessMetric[]
DeepCopy()
Creates a deep copy of this model.
public IFullModel<T, Matrix<T>, Vector<T>> DeepCopy()
Returns
- IFullModel<T, Matrix<T>, Vector<T>>
A new instance with the same coefficients.
Remarks
This method creates a deep copy of the model by creating a new VectorModel with a new coefficients vector that has the same values as the original. This ensures that modifications to the copy do not affect the original model. This method is useful when you need to create a duplicate of a model for experimentation or as part of genetic algorithm operations.
For Beginners: This method creates an exact duplicate of the model.
The DeepCopy method:
- Creates a new model with the same coefficients as this one
- Ensures the new model is completely independent of the original
- Creates a "deep copy" where all data is duplicated, not just references
This method is useful when:
- You need to create a duplicate of a model for experimentation
- You want to ensure changes to one model don't affect another
- You're implementing algorithms that require model copies
For example, you might copy a model before mutating it to preserve the original.
Deserialize(byte[])
Deserializes the model from a byte array.
public void Deserialize(byte[] data)
Parameters
databyte[]The byte array containing the serialized model.
Remarks
This method deserializes the model from a byte array by reading the number of coefficients and then each coefficient value. It expects the same format as produced by the Serialize method: first an integer indicating the number of coefficients, followed by each coefficient as a double. This allows a model that was previously serialized to be reconstructed.
For Beginners: This method reconstructs a model from a byte array created by Serialize.
The Deserialize method:
- Takes a byte array containing a serialized model
- Reads the number of coefficients and each coefficient value
- Updates the model's coefficients with the deserialized values
It expects the same format created by Serialize:
- An integer with the number of coefficients
- Each coefficient value as a double
This method is useful when:
- Loading models from files or databases
- Receiving models over a network
- Restoring models from persistent storage
Note that this method updates the existing model's coefficients rather than creating a new model, which is different from most other methods in this class.
Exceptions
- ArgumentNullException
Thrown when data is null.
- ArgumentException
Thrown when data is empty or invalid.
- InvalidOperationException
Thrown when the serialized coefficients count doesn't match the model's coefficients count.
EnableMethod(params InterpretationMethod[])
Enables specific interpretation methods.
public virtual void EnableMethod(params InterpretationMethod[] methods)
Parameters
methodsInterpretationMethod[]
Evaluate(Vector<T>)
Evaluates the model for a given input vector.
public T Evaluate(Vector<T> input)
Parameters
inputVector<T>The input vector.
Returns
- T
The model's prediction for the input.
Remarks
This method evaluates the model for a given input vector by computing the dot product of the input and the coefficients. This is the core prediction function of the linear model, where each feature value is multiplied by its corresponding coefficient and the results are summed to produce the final prediction.
For Beginners: This method calculates the model's prediction for a single input.
The Evaluate method:
- Takes a vector of input values
- Multiplies each input by its corresponding coefficient
- Adds up all these products to get the final prediction
- Throws an error if the input has the wrong number of features
This is the core of how a linear model works - it's just a weighted sum: prediction = (input1 × coefficient1) + (input2 × coefficient2) + ...
For example, with coefficients [50000, 100, 20000] and input [3, 1500, 2], the prediction would be: 3×50000 + 1500×100 + 2×20000 = 350,000
Exceptions
- ArgumentException
Thrown when the input vector length doesn't match the coefficients length.
ExportComputationGraph(List<ComputationNode<T>>)
Exports the linear regression model as a computation graph for JIT compilation.
public ComputationNode<T> ExportComputationGraph(List<ComputationNode<T>> inputNodes)
Parameters
inputNodesList<ComputationNode<T>>List to populate with input computation nodes.
Returns
- ComputationNode<T>
The output computation node representing the prediction.
Remarks
This method converts the linear regression computation into a computation graph: output = input @ coefficients
The graph represents a simple matrix-vector multiplication that the JIT compiler can optimize and compile to native code.
For Beginners: This converts your linear model into a form the JIT compiler can optimize.
The conversion:
- Converts Matrix/Vector to Tensor (JIT works with Tensors)
- Creates computation nodes for input and coefficients
- Builds a graph: output = MatMul(input, coefficients)
- Returns the output node
Once converted, the JIT compiler can:
- Optimize the computation
- Generate fast native code
- Provide 5-10x faster predictions
GenerateTextExplanationAsync(Matrix<T>, Vector<T>)
Generates a text explanation for a prediction.
public virtual Task<string> GenerateTextExplanationAsync(Matrix<T> input, Vector<T> prediction)
Parameters
inputMatrix<T>predictionVector<T>
Returns
GenerateTextExplanationAsync(Tensor<T>, Tensor<T>)
Generates a text explanation for a prediction (IInterpretableModel implementation).
public virtual Task<string> GenerateTextExplanationAsync(Tensor<T> input, Tensor<T> prediction)
Parameters
inputTensor<T>predictionTensor<T>
Returns
GetActiveFeatureIndices()
Gets the indices of all features used by this model.
public IEnumerable<int> GetActiveFeatureIndices()
Returns
- IEnumerable<int>
A collection of feature indices for features with non-zero coefficients.
Remarks
This method returns the indices of all features that have non-zero coefficients. These are the features that actually contribute to the model's predictions. Features with zero coefficients have no effect on the output.
For Beginners: This method tells you which input features actually matter to the model.
It returns:
- The positions (indices) of all features with non-zero weights
- Only the features that actually affect the prediction
For example, if your model has coefficients [0.5, 0, 0, 2.0, 0.3], this method would return [0, 3, 4], since only those positions have non-zero values.
This is useful for:
- Understanding which features the model considers important
- Feature selection (identifying which features to keep)
- Simplifying the model by focusing only on important features
GetAnchorExplanationAsync(Matrix<T>, T)
Gets anchor explanation for a given input.
public virtual Task<AnchorExplanation<T>> GetAnchorExplanationAsync(Matrix<T> input, T threshold)
Parameters
inputMatrix<T>thresholdT
Returns
GetAnchorExplanationAsync(Tensor<T>, T)
Gets anchor explanation for a given input (IInterpretableModel implementation).
public virtual Task<AnchorExplanation<T>> GetAnchorExplanationAsync(Tensor<T> input, T threshold)
Parameters
inputTensor<T>thresholdT
Returns
GetCounterfactualAsync(Matrix<T>, Vector<T>, int)
Gets counterfactual explanation for a given input and desired output.
public virtual Task<CounterfactualExplanation<T>> GetCounterfactualAsync(Matrix<T> input, Vector<T> desiredOutput, int maxChanges = 5)
Parameters
inputMatrix<T>desiredOutputVector<T>maxChangesint
Returns
GetCounterfactualAsync(Tensor<T>, Tensor<T>, int)
Gets counterfactual explanation for a given input and desired output (IInterpretableModel implementation).
public virtual Task<CounterfactualExplanation<T>> GetCounterfactualAsync(Tensor<T> input, Tensor<T> desiredOutput, int maxChanges = 5)
Parameters
inputTensor<T>desiredOutputTensor<T>maxChangesint
Returns
GetFeatureImportance()
Gets the feature importance scores.
public Dictionary<string, T> GetFeatureImportance()
Returns
- Dictionary<string, T>
A dictionary mapping feature names to importance scores.
Remarks
This method returns the feature importance scores for the model. For a VectorModel, the importance is based on the absolute value of each coefficient, as larger absolute values have a greater impact on predictions.
For Beginners: This method shows which features matter most to the model.
For a linear model:
- Features with larger coefficients (positive or negative) are more important
- The importance is the absolute value of each coefficient
- Features are named "Feature_0", "Feature_1", etc.
This is useful for:
- Understanding which features drive predictions
- Feature selection (identifying which features to keep)
- Model interpretation and explanation
GetFeatureInteractionAsync(int, int)
Gets feature interaction effects between two features.
public virtual Task<T> GetFeatureInteractionAsync(int feature1Index, int feature2Index)
Parameters
Returns
- Task<T>
GetGlobalFeatureImportanceAsync()
Gets the global feature importance across all predictions.
public virtual Task<Dictionary<int, T>> GetGlobalFeatureImportanceAsync()
Returns
- Task<Dictionary<int, T>>
GetLimeExplanationAsync(Matrix<T>, int)
Gets LIME explanation for a specific input.
public virtual Task<LimeExplanation<T>> GetLimeExplanationAsync(Matrix<T> input, int numFeatures = 10)
Parameters
inputMatrix<T>numFeaturesint
Returns
- Task<LimeExplanation<T>>
GetLimeExplanationAsync(Tensor<T>, int)
Gets LIME explanation for a specific input (IInterpretableModel implementation).
public virtual Task<LimeExplanation<T>> GetLimeExplanationAsync(Tensor<T> input, int numFeatures = 10)
Parameters
inputTensor<T>numFeaturesint
Returns
- Task<LimeExplanation<T>>
GetLocalFeatureImportanceAsync(Matrix<T>)
Gets the local feature importance for a specific input.
public virtual Task<Dictionary<int, T>> GetLocalFeatureImportanceAsync(Matrix<T> input)
Parameters
inputMatrix<T>
Returns
- Task<Dictionary<int, T>>
GetLocalFeatureImportanceAsync(Tensor<T>)
Gets the local feature importance for a specific input (IInterpretableModel implementation).
public virtual Task<Dictionary<int, T>> GetLocalFeatureImportanceAsync(Tensor<T> input)
Parameters
inputTensor<T>
Returns
- Task<Dictionary<int, T>>
GetModelMetadata()
Gets metadata about the model.
public ModelMetadata<T> GetModelMetadata()
Returns
- ModelMetadata<T>
A ModelMetadata object containing information about the model.
Remarks
This method returns metadata about the model, including its type, feature count, complexity, and additional information about the coefficients. The metadata includes the model type (Vector), the number of features, the complexity (which for a vector model is the number of features), a description, and additional information such as the norm of the coefficients, the number of non-zero coefficients, and the mean, maximum, and minimum coefficient values. This metadata is useful for model selection, analysis, and visualization.
For Beginners: This method returns detailed information about the model.
The GetModelMetadata method:
- Creates a ModelMetadata object with information about the model
- Includes basic properties like model type, feature count, and complexity
- Adds additional statistics about the coefficients
The additional information includes:
- CoefficientNorm: A measure of the magnitude of all coefficients
- NonZeroCoefficients: How many features are actually used (have non-zero weights)
- MeanCoefficient: The average coefficient value
- MaxCoefficient: The largest coefficient value
- MinCoefficient: The smallest coefficient value
This information is useful for:
- Analyzing the model's characteristics
- Comparing different models
- Visualizing or reporting on the model
GetModelSpecificInterpretabilityAsync()
Gets model-specific interpretability information.
public virtual Task<Dictionary<string, object>> GetModelSpecificInterpretabilityAsync()
Returns
GetParameters()
Gets all trainable parameters of the model as a single vector.
public Vector<T> GetParameters()
Returns
- Vector<T>
A vector containing all trainable parameters (the coefficients).
Remarks
This method returns the coefficients of the model as a vector, which represents all trainable parameters of the model. For a vector model, the parameters are simply the coefficients vector.
For Beginners: This method gives you access to all the weights the model uses.
For a linear model:
- The parameters are simply the coefficients (weights)
- This method returns a copy of those coefficients
This is useful for:
- Saving the model for later use
- Analyzing the learned weights
- Transferring weights to another model
- Implementing optimization algorithms
GetPartialDependenceAsync(Vector<int>, int)
Gets partial dependence data for specified features.
public virtual Task<PartialDependenceData<T>> GetPartialDependenceAsync(Vector<int> featureIndices, int gridResolution = 20)
Parameters
Returns
GetShapValuesAsync(Matrix<T>)
Gets SHAP values for the given inputs.
public virtual Task<Matrix<T>> GetShapValuesAsync(Matrix<T> inputs)
Parameters
inputsMatrix<T>
Returns
- Task<Matrix<T>>
GetShapValuesAsync(Tensor<T>)
Gets SHAP values for the given inputs (IInterpretableModel implementation).
public virtual Task<Matrix<T>> GetShapValuesAsync(Tensor<T> inputs)
Parameters
inputsTensor<T>
Returns
- Task<Matrix<T>>
IsFeatureUsed(int)
Determines whether a specific feature is used by the model.
public bool IsFeatureUsed(int featureIndex)
Parameters
featureIndexintThe index of the feature to check.
Returns
- bool
True if the feature has a non-zero coefficient, false otherwise.
Remarks
This method determines whether a specific feature is used by the model by checking if its corresponding coefficient is non-zero. A zero coefficient means that the feature has no impact on the model's predictions, effectively removing it from the model. This method is useful for feature selection and for understanding which features the model considers important.
For Beginners: This method checks if a particular input variable actually affects the model's predictions.
The IsFeatureUsed method:
- Checks if the coefficient for a specific feature is non-zero
- Returns true if the feature affects predictions, false if it doesn't
- Helps identify which features are actually important to the model
For example, if the coefficient for square footage is 0, then IsFeatureUsed(2) would return false (assuming square footage is the third feature).
This method is useful when:
- Analyzing which features matter to the model
- Simplifying the model by removing unused features
- Understanding the model's behavior
LoadModel(string)
Loads a model from a file into the current instance.
public void LoadModel(string filePath)
Parameters
filePathstringThe path of the file containing the serialized model.
Remarks
This method loads a model from a file by reading the byte array and deserializing it. The model must have been saved using the SaveModel method.
For Beginners: This method loads a saved model from a file.
To use this method:
- Provide the file path where the model was saved
- The model will be loaded and replace the current model's coefficients
- The file must have been created with SaveModel
For example: model.LoadModel("my_model.bin");
Exceptions
- ArgumentNullException
Thrown when filePath is null.
- FileNotFoundException
Thrown when the file does not exist.
LoadState(Stream)
Loads the model's state (parameters and configuration) from a stream.
public virtual void LoadState(Stream stream)
Parameters
streamStreamThe stream to read the model state from.
Remarks
This method deserializes model state that was previously saved with SaveState, restoring all coefficients and configuration to recreate the saved model. It uses the existing Deserialize method after reading data from the stream.
For Beginners: This is like loading a saved snapshot of your linear model.
When you call LoadState:
- All the coefficients are read from the stream
- The model is configured to match the saved state
- The model becomes identical to when SaveState was called
After loading, the model can:
- Make predictions using the restored coefficients
- Continue training from where it left off
- Be used as a teacher model in knowledge distillation
This is essential for:
- Resuming interrupted training sessions
- Loading the best checkpoint after training
- Deploying trained models to production
- Knowledge distillation workflows
Exceptions
- ArgumentNullException
Thrown when stream is null.
- IOException
Thrown when there's an error reading from the stream.
- InvalidOperationException
Thrown when the stream contains invalid or incompatible data.
Predict(Matrix<T>)
Predicts outputs for the provided input.
public Vector<T> Predict(Matrix<T> input)
Parameters
inputMatrix<T>The input data to make predictions for.
Returns
- Vector<T>
A vector of predictions.
Remarks
This method implements the IModel interface's Predict method by delegating to the internal Predict method. It accepts a matrix where each row is a separate data point and returns a vector of predictions.
For Beginners: This method makes predictions using your input data.
When using this method:
- You provide a matrix where each row is a separate data point
- Each row has the same features you trained the model with
- The model returns one prediction for each row
This is the main method you'll use to make predictions once your model is trained.
Exceptions
- ArgumentNullException
Thrown when input is null.
- ArgumentException
Thrown when input has the wrong number of columns.
SaveModel(string)
Saves the model to a file.
public void SaveModel(string filePath)
Parameters
filePathstringThe path where the model will be saved.
Remarks
This method saves the model to a file by serializing it to a byte array and writing it to disk. The model can later be loaded using the LoadModel method.
For Beginners: This method saves the model to a file so you can use it later.
To use this method:
- Provide a file path where the model should be saved
- The model will be serialized and written to disk
- You can load it later with LoadModel
For example: model.SaveModel("my_model.bin");
Exceptions
- ArgumentNullException
Thrown when filePath is null.
SaveState(Stream)
Saves the model's current state (parameters and configuration) to a stream.
public virtual void SaveState(Stream stream)
Parameters
streamStreamThe stream to write the model state to.
Remarks
This method serializes all the information needed to recreate the model's current state, including the model's coefficients. It uses the existing Serialize method and writes the data to the provided stream.
For Beginners: This is like creating a snapshot of your trained linear model.
When you call SaveState:
- All the learned coefficients (weights) are written to the stream
- The model's configuration is preserved
This is particularly useful for:
- Checkpointing during long training sessions
- Knowledge distillation (saving teacher/student models)
- Resuming interrupted training
- Creating model ensembles
You can later use LoadState to restore the model to this exact state.
Exceptions
- ArgumentNullException
Thrown when stream is null.
- IOException
Thrown when there's an error writing to the stream.
Serialize()
Serializes the model to a byte array.
public byte[] Serialize()
Returns
- byte[]
A byte array containing the serialized model.
Remarks
This method serializes the model to a byte array by writing the number of coefficients and then each coefficient value. The serialization format is simple: first an integer indicating the number of coefficients, followed by each coefficient as a double. This allows the model to be stored or transmitted and later reconstructed using the Deserialize method.
For Beginners: This method converts the model to a byte array that can be saved or transmitted.
The Serialize method:
- Converts the model to a compact binary format
- Writes the number of coefficients and each coefficient value
- Returns a byte array that can be stored or transmitted
The serialization format is:
- An integer with the number of coefficients
- Each coefficient value as a double
This method is useful when:
- Saving models to files or databases
- Sending models over a network
- Persisting models between application runs
The resulting byte array can be converted back to a model using Deserialize.
SetActiveFeatureIndices(IEnumerable<int>)
Sets the active feature indices for this model.
public void SetActiveFeatureIndices(IEnumerable<int> featureIndices)
Parameters
featureIndicesIEnumerable<int>
SetBaseModel(IFullModel<T, Matrix<T>, Vector<T>>)
Sets the base model for interpretability analysis.
public virtual void SetBaseModel(IFullModel<T, Matrix<T>, Vector<T>> model)
Parameters
modelIFullModel<T, Matrix<T>, Vector<T>>
SetBaseModel<TInput, TOutput>(IFullModel<T, TInput, TOutput>)
Sets the base model for interpretability analysis (IInterpretableModel implementation).
public virtual void SetBaseModel<TInput, TOutput>(IFullModel<T, TInput, TOutput> model)
Parameters
modelIFullModel<T, TInput, TOutput>
Type Parameters
TInputTOutput
SetParameters(Vector<T>)
Sets the parameters of the model.
public void SetParameters(Vector<T> parameters)
Parameters
parametersVector<T>The parameters to set.
Remarks
This method sets the coefficients of the model from the provided parameters vector. For a vector model, the parameters are simply the coefficients vector.
For Beginners: This method updates all the weights the model uses.
For a linear model:
- The parameters are simply the coefficients (weights)
- This method updates those coefficients
This is useful for:
- Loading a saved model
- Updating weights during optimization
- Implementing learning algorithms
Exceptions
- ArgumentNullException
Thrown when parameters is null.
- ArgumentException
Thrown when parameters has a different length than the model's coefficients.
Train(Matrix<T>, Vector<T>)
Trains the model on the provided generic input and expected output.
public void Train(Matrix<T> input, Vector<T> expectedOutput)
Parameters
inputMatrix<T>The input data, which must be a Matrix of type T.
expectedOutputVector<T>The expected output, which must be a Vector of type T.
Remarks
This method implements the IModel interface's Train method by delegating to the internal Train method. It validates the input and output types before proceeding with training.
For Beginners: This method teaches the model using your data.
When using this method:
- You provide a matrix of input features and a vector of expected outputs
- The model learns to predict the outputs from the inputs
- It finds the weights (coefficients) that work best for your data
This is the main method you'll use to train the model on your dataset.
Exceptions
- ArgumentNullException
Thrown when input or expectedOutput is null.
- InvalidOperationException
Thrown when the input or output types are incompatible with the model.
ValidateFairnessAsync(Matrix<T>, int)
Validates fairness metrics for the given inputs.
public virtual Task<FairnessMetrics<T>> ValidateFairnessAsync(Matrix<T> inputs, int sensitiveFeatureIndex)
Parameters
inputsMatrix<T>sensitiveFeatureIndexint
Returns
- Task<FairnessMetrics<T>>
ValidateFairnessAsync(Tensor<T>, int)
Validates fairness metrics for the given inputs (IInterpretableModel implementation).
public virtual Task<FairnessMetrics<T>> ValidateFairnessAsync(Tensor<T> inputs, int sensitiveFeatureIndex)
Parameters
inputsTensor<T>sensitiveFeatureIndexint
Returns
- Task<FairnessMetrics<T>>
WithParameters(Vector<T>)
Updates the model with new parameter values.
public IFullModel<T, Matrix<T>, Vector<T>> WithParameters(Vector<T> parameters)
Parameters
parametersVector<T>The new parameter values to use.
Returns
- IFullModel<T, Matrix<T>, Vector<T>>
A new model with the updated parameters.
Remarks
This method creates a new model with the provided parameter values. For a vector model, the parameters are simply the coefficients, so this creates a new model with new coefficients.
For Beginners: This method creates a new model with different weights.
The WithParameters method:
- Takes a vector of new weights (parameters)
- Creates a new model using these weights
- Returns the new model without modifying the original
This is useful for:
- Testing different sets of weights
- Implementing optimization algorithms
- Creating variations of a model
For example, you might try different weight values to see which ones give the best predictions or modify weights to simplify the model.
Exceptions
- ArgumentNullException
Thrown when parameters is null.
- ArgumentException
Thrown when parameters has a different length than the model's coefficients.