Class MultilayerPerceptronRegression<T>
- Namespace
- AiDotNet.Regression
- Assembly
- AiDotNet.dll
Represents a multilayer perceptron (neural network) for regression problems.
public class MultilayerPerceptronRegression<T> : NonLinearRegressionBase<T>, INonLinearRegression<T>, IRegression<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
TThe numeric type used for calculations, typically float or double.
- Inheritance
-
MultilayerPerceptronRegression<T>
- Implements
-
IRegression<T>
- Inherited Members
- Extension Methods
Remarks
The MultilayerPerceptronRegression is a neural network-based regression model that can capture complex non-linear relationships between features and the target variable. It consists of an input layer, one or more hidden layers, and an output layer, with each layer connected by weights and biases. The model learns by adjusting these weights and biases through a process called backpropagation, minimizing the prediction error.
For Beginners: A multilayer perceptron is like a digital brain that can learn complex patterns.
Think of it as a system of interconnected layers:
- The input layer receives your data (like house features if predicting house prices)
- The hidden layers process this information through a series of mathematical transformations
- The output layer produces the final prediction (like the predicted house price)
Each connection between neurons has a "weight" (importance) that gets adjusted as the network learns. For example, the network might learn that square footage has a bigger impact on house prices than the age of the house, so it assigns a larger weight to that feature.
The network improves by comparing its predictions to actual values and adjusting the weights to reduce the difference between them.
Constructors
MultilayerPerceptronRegression(MultilayerPerceptronOptions<T, Matrix<T>, Vector<T>>?, IRegularization<T, Matrix<T>, Vector<T>>?)
Initializes a new instance of the MultilayerPerceptronRegression<T> class with optional custom options and regularization.
public MultilayerPerceptronRegression(MultilayerPerceptronOptions<T, Matrix<T>, Vector<T>>? options = null, IRegularization<T, Matrix<T>, Vector<T>>? regularization = null)
Parameters
optionsMultilayerPerceptronOptions<T, Matrix<T>, Vector<T>>Custom options for the neural network. If null, default options are used.
regularizationIRegularization<T, Matrix<T>, Vector<T>>Regularization method to prevent overfitting. If null, no regularization is applied.
Remarks
This constructor creates a new neural network with the specified options and regularization. If no options are provided, default values are used. The network structure (weights and biases) is initialized based on the layer sizes specified in the options.
For Beginners: This creates a new neural network with your chosen settings.
When creating a neural network:
- You can provide custom settings (options) or use the defaults
- You can add regularization, which helps prevent the model from memorizing the training data
- The network is initialized with random weights, scaled to appropriate values
This is like setting up a new brain with the right number of connections, ready to learn but not yet trained on any data.
Methods
CreateInstance()
Creates a new instance of the MultilayerPerceptronRegression<T> class with the same options and regularization as this instance.
protected override IFullModel<T, Matrix<T>, Vector<T>> CreateInstance()
Returns
- IFullModel<T, Matrix<T>, Vector<T>>
A new instance of the MultilayerPerceptronRegression<T> class.
Remarks
This method creates a new, uninitialized instance of the multilayer perceptron regression model with the same configuration as the current instance. It is used for creating copies or clones of the model without copying the learned parameters.
For Beginners: This method creates a brand new neural network with the same architecture as this one.
The new network:
- Uses the same layer sizes and structure
- Uses the same activation functions and learning parameters
- Uses the same regularization approach to prevent overfitting
- Is uninitialized (not trained yet) with fresh random weights
It's like creating a duplicate blueprint of the current network structure without copying any of the learned knowledge, which is useful for ensemble methods or cross-validation.
Deserialize(byte[])
Deserializes the neural network model from a byte array.
public override void Deserialize(byte[] data)
Parameters
databyte[]A byte array containing the serialized model data.
Remarks
This method restores a neural network model from a serialized byte array, reconstructing its parameters, weights, biases, and configuration. This allows a previously trained model to be loaded from storage or after being received over a network.
For Beginners: This method rebuilds the model from a saved format.
Deserialization:
- Takes a sequence of bytes that represents a model
- Reconstructs the original neural network with all its learned knowledge
- Restores the weights, biases, layer sizes, and other settings
- Allows you to use a previously trained model without retraining
It's like unpacking a complete model that was packed up for storage or sharing, so you can use it again exactly as it was when saved, with all its learned patterns intact.
Exceptions
- InvalidOperationException
Thrown when the optimizer options cannot be deserialized.
GetModelType()
Gets the type of regression model.
protected override ModelType GetModelType()
Returns
- ModelType
The model type, in this case, MultilayerPerceptronRegression.
Remarks
This method returns an enumeration value indicating that this is a multilayer perceptron regression model. This is used for type identification when working with different regression models in a unified manner.
For Beginners: This method simply identifies what kind of model this is.
It returns a label (MultilayerPerceptronRegression) that:
- Identifies this specific type of model
- Helps other code handle the model appropriately
- Is used for model identification and categorization
It's like a name tag that lets other parts of the program know what kind of model they're working with.
OptimizeModel(Matrix<T>, Vector<T>)
Optimizes the model by training it on the provided data.
protected override void OptimizeModel(Matrix<T> x, Vector<T> y)
Parameters
xMatrix<T>The feature matrix for training.
yVector<T>The target vector for training.
Remarks
This method implements the abstract method from the base class by calling the Train method. It is used to optimize the model parameters based on the provided training data.
For Beginners: This method trains the model with your data.
It's a simple method that:
- Is required by the base class
- Calls the Train method to do the actual work
- Allows the model to fit into the standard optimization framework
This consistent interface makes it easier to work with different types of models in a uniform way.
Predict(Matrix<T>)
Generates predictions for new data points using the trained neural network.
public override Vector<T> Predict(Matrix<T> X)
Parameters
XMatrix<T>The feature matrix where each row is a sample to predict.
Returns
- Vector<T>
A vector containing the predicted values.
Remarks
This method performs a forward pass through the neural network for each sample in the input feature matrix to generate predictions. The forward pass applies the learned weights and biases along with the activation functions to transform the input features into a predicted output value.
For Beginners: This is where the network uses what it learned to make predictions on new data.
For each data point:
- The features are fed into the input layer
- The network processes them through the hidden layers
- The output layer produces the final prediction
It's like a factory assembly line where raw materials (features) go in one end, and a finished product (prediction) comes out the other end.
Serialize()
Serializes the neural network model to a byte array for storage or transmission.
public override byte[] Serialize()
Returns
- byte[]
A byte array containing the serialized model data.
Remarks
This method converts the entire neural network model, including its parameters, weights, biases, and configuration, into a byte array that can be stored in a file or database, or transmitted over a network. The model can later be restored using the Deserialize method.
For Beginners: This method saves the model to a format that can be stored or shared.
Serialization:
- Converts the model into a sequence of bytes
- Preserves all the important information (weights, biases, architecture, etc.)
- Allows you to save the trained model to a file
- Lets you load the model later without having to retrain it
It's like taking a complete snapshot of the model that you can use later or share with others.
Train(Matrix<T>, Vector<T>)
Trains the neural network using the provided features and target values.
public override void Train(Matrix<T> X, Vector<T> y)
Parameters
XMatrix<T>The feature matrix where each row is a sample and each column is a feature.
yVector<T>The target vector containing the values to predict.
Remarks
This method trains the neural network using mini-batch gradient descent. It iteratively processes batches of the training data, computes the gradients of the loss with respect to the weights and biases, and updates the model parameters accordingly. Training continues until the maximum number of epochs is reached or the loss falls below the specified tolerance.
For Beginners: This is where the network learns from your data.
During training:
- The data is divided into small batches
- For each batch:
- The network makes predictions
- The error between predictions and actual values is calculated
- The weights and biases are adjusted to reduce this error
- This process repeats for multiple passes (epochs) through the data
- Training stops when the error gets small enough or after a maximum number of epochs
It's like learning a skill through repeated practice and feedback, gradually improving performance by adjusting your approach based on the results.
Exceptions
- ArgumentException
Thrown when the input feature size does not match the first layer size.