Class NeuralNetworkRegression<T>
- Namespace
- AiDotNet.Regression
- Assembly
- AiDotNet.dll
A neural network regression model that can learn complex non-linear relationships in data.
public class NeuralNetworkRegression<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 data type used for calculations (e.g., float, double).
- Inheritance
-
NeuralNetworkRegression<T>
- Implements
-
IRegression<T>
- Inherited Members
- Extension Methods
Remarks
This class implements a fully connected feedforward neural network for regression tasks. It supports multiple hidden layers with customizable activation functions and uses gradient-based optimization to learn from data.
The neural network architecture is defined by specifying the number of neurons in each layer, with the first layer corresponding to the input features and the last layer to the output.
For Beginners: A neural network is a machine learning model inspired by the human brain. It consists of layers of interconnected "neurons" that process input data to make predictions. Each connection has a "weight" that determines its importance, and these weights are adjusted during training to improve the model's accuracy. This process is similar to how we learn from experience.
Constructors
NeuralNetworkRegression(NeuralNetworkRegressionOptions<T, Matrix<T>, Vector<T>>?, IRegularization<T, Matrix<T>, Vector<T>>?)
Initializes a new instance of the NeuralNetworkRegression class with the specified options and regularization.
public NeuralNetworkRegression(NeuralNetworkRegressionOptions<T, Matrix<T>, Vector<T>>? options = null, IRegularization<T, Matrix<T>, Vector<T>>? regularization = null)
Parameters
optionsNeuralNetworkRegressionOptions<T, Matrix<T>, Vector<T>>Configuration options for the neural network. If null, default options will be used.
regularizationIRegularization<T, Matrix<T>, Vector<T>>Regularization method to prevent overfitting. If null, no regularization will be applied.
Remarks
The constructor initializes the neural network with random weights and biases according to the Xavier/Glorot initialization method, which helps with training stability.
If no options are provided, a default configuration will be used. If no optimizer is specified in the options, the Adam optimizer will be used by default.
For Beginners: When you create a neural network, it starts with random values for its internal parameters (weights and biases). The Xavier/Glorot initialization is a smart way to set these initial random values to help the network learn more effectively. Think of it like setting up a balanced starting point for the learning process.
Methods
CreateInstance()
Creates a new instance of the Neural Network Regression model with the same configuration.
protected override IFullModel<T, Matrix<T>, Vector<T>> CreateInstance()
Returns
- IFullModel<T, Matrix<T>, Vector<T>>
A new instance of the Neural Network Regression model.
Remarks
This method creates a deep copy of the current Neural Network Regression model, including its layer structure, weights, biases, optimizer configuration, and regularization settings. The new instance is completely independent of the original, allowing modifications without affecting the original model.
For Beginners: This method creates an exact copy of your trained neural network.
Think of it like cloning your neural network with all its learned knowledge:
- It copies the structure (how many neurons in each layer)
- It duplicates all the connection strengths (weights) between neurons
- It preserves all the bias values for each neuron
- It maintains the same learning algorithm (optimizer) and settings
Creating a copy is useful when you want to:
- Create a backup before further training or experimentation
- Create variations of the same model for different purposes
- Share the model with others while keeping your original intact
Exceptions
- InvalidOperationException
Thrown when the creation fails or required components are null.
Deserialize(byte[])
Deserializes the model from a byte array.
public override void Deserialize(byte[] data)
Parameters
databyte[]The byte array containing the serialized model data.
Remarks
This method deserializes both the base class data and the neural network specific data, reconstructing the layer sizes, weights, and biases.
For Beginners: Deserialization is the opposite of serialization - it takes the saved model data and reconstructs the model's internal state. This allows you to load a previously trained model and use it to make predictions without having to retrain it. It's like loading a saved game to continue where you left off.
GetModelType()
Gets the type of the model.
protected override ModelType GetModelType()
Returns
- ModelType
The model type identifier for neural network regression.
Remarks
This method is used for model identification and serialization purposes.
For Beginners: This method simply returns an identifier that indicates this is a neural network regression model. It's used internally by the library to keep track of different types of models.
OptimizeModel(Matrix<T>, Vector<T>)
Optimizes the model parameters using the provided training data.
protected override void OptimizeModel(Matrix<T> x, Vector<T> y)
Parameters
xMatrix<T>The input features matrix.
yVector<T>The target values vector.
Remarks
This method is called by the base class during the fitting process to optimize the model parameters. It simply calls the Train method to perform the optimization.
For Beginners: This is an internal method that's part of the model fitting process. It delegates the actual training to the Train method, which handles the details of updating the model parameters to fit the data.
Predict(Matrix<T>)
Makes predictions for the given input data.
public override Vector<T> Predict(Matrix<T> X)
Parameters
XMatrix<T>The input features matrix where each row is an example and each column is a feature.
Returns
- Vector<T>
A vector of predicted values for each input example.
Remarks
This method performs a forward pass through the network for each input example and returns the predicted values.
For Beginners: After training, this method is used to make predictions on new data. It runs each example through the trained network and returns the predicted values. This is like using what the network has learned to make educated guesses about new situations it hasn't seen before.
Serialize()
Serializes the model to a byte array.
public override byte[] Serialize()
Returns
- byte[]
A byte array containing the serialized model data.
Remarks
This method serializes both the base class data and the neural network specific data, including the layer sizes, weights, and biases.
For Beginners: Serialization converts the model's internal state into a format that can be saved to disk or transmitted over a network. This allows you to save a trained model and load it later without having to retrain it. Think of it like saving your progress in a video game.
Train(Matrix<T>, Vector<T>)
Trains the neural network on the provided data.
public override void Train(Matrix<T> X, Vector<T> y)
Parameters
XMatrix<T>The input features matrix where each row is a training example and each column is a feature.
yVector<T>The target values vector corresponding to each training example.
Remarks
This method trains the neural network using mini-batch gradient descent. The data is divided into batches, and for each batch, the model parameters are updated based on the computed gradients.
The training process runs for the number of epochs specified in the options, and the data is shuffled before each epoch to improve training stability.
For Beginners: Training a neural network is like teaching it to make accurate predictions by showing it many examples. An "epoch" is one complete pass through all the training data. The data is divided into smaller "batches" to make the training more efficient. Before each epoch, the data is shuffled (like shuffling flashcards) to help the network learn better patterns rather than memorizing the order of examples.