Table of Contents

Class FeedForwardNeuralNetwork<T>

Namespace
AiDotNet.NeuralNetworks
Assembly
AiDotNet.dll

Represents a Feed-Forward Neural Network (FFNN) for processing data in a forward path.

public class FeedForwardNeuralNetwork<T> : NeuralNetworkBase<T>, INeuralNetworkModel<T>, INeuralNetwork<T>, IFullModel<T, Tensor<T>, Tensor<T>>, IModel<Tensor<T>, Tensor<T>, ModelMetadata<T>>, IModelSerializer, ICheckpointableModel, IParameterizable<T, Tensor<T>, Tensor<T>>, IFeatureAware, IFeatureImportance<T>, ICloneable<IFullModel<T, Tensor<T>, Tensor<T>>>, IGradientComputable<T, Tensor<T>, Tensor<T>>, IJitCompilable<T>, IInterpretableModel<T>, IInputGradientComputable<T>, IDisposable

Type Parameters

T

The numeric type used for calculations (typically float or double).

Inheritance
FeedForwardNeuralNetwork<T>
Implements
IFullModel<T, Tensor<T>, Tensor<T>>
IModel<Tensor<T>, Tensor<T>, ModelMetadata<T>>
IParameterizable<T, Tensor<T>, Tensor<T>>
ICloneable<IFullModel<T, Tensor<T>, Tensor<T>>>
IGradientComputable<T, Tensor<T>, Tensor<T>>
Inherited Members
Extension Methods

Remarks

A Feed-Forward Neural Network is the simplest type of artificial neural network, where connections between nodes do not form a cycle. Information moves in only one direction -- forward -- from the input nodes, through the hidden nodes (if any), and to the output nodes.

For Beginners: Think of a Feed-Forward Neural Network like a series of information-processing stages arranged in a line. Data flows only forward through these stages, never backward. Each stage (or layer) processes the information and passes it to the next stage. This simple structure makes FFNNs great for many common tasks like classification (deciding which category something belongs to) or regression (predicting a numerical value).

Constructors

FeedForwardNeuralNetwork(NeuralNetworkArchitecture<T>, IGradientBasedOptimizer<T, Tensor<T>, Tensor<T>>?, ILossFunction<T>?, double)

Initializes a new instance of the FeedForwardNeuralNetwork class.

public FeedForwardNeuralNetwork(NeuralNetworkArchitecture<T> architecture, IGradientBasedOptimizer<T, Tensor<T>, Tensor<T>>? optimizer = null, ILossFunction<T>? lossFunction = null, double maxGradNorm = 1)

Parameters

architecture NeuralNetworkArchitecture<T>

The architecture defining the structure of the neural network.

optimizer IGradientBasedOptimizer<T, Tensor<T>, Tensor<T>>

The optimization algorithm to use for training. If null, Adam optimizer is used.

lossFunction ILossFunction<T>

The loss function to use for training. If null, an appropriate loss function is selected based on the task type.

maxGradNorm double

The maximum gradient norm for gradient clipping during training.

Remarks

Feed-Forward Neural Networks can work with various input dimensions and are typically used for classification and regression tasks with structured data.

For Beginners: When creating a Feed-Forward Neural Network, you need to provide a blueprint (architecture) that defines the structure of your network. This constructor sets up the network based on that blueprint. It also prepares the learning strategy (optimizer) and the way to measure mistakes (loss function). If you don't specify these, it chooses reasonable defaults based on the type of task you're trying to solve.

Properties

SupportsTraining

Indicates whether this network supports training.

public override bool SupportsTraining { get; }

Property Value

bool

Remarks

Feed-forward neural networks support training through backpropagation.

For Beginners: This indicates that the network can learn from data. Feed-forward networks are designed to be trained, so this property returns true.

Methods

Backward(Tensor<T>)

Performs a backward pass through the network to calculate gradients.

public Tensor<T> Backward(Tensor<T> outputGradient)

Parameters

outputGradient Tensor<T>

The gradient of the loss with respect to the network's output.

Returns

Tensor<T>

The gradient of the loss with respect to the network's input.

Remarks

The backward pass is used during training to update the network's parameters. It propagates the gradient backward through each layer, starting from the output layer. This process is known as "backpropagation" and is essential for training neural networks.

For Beginners: While the forward pass makes predictions, the backward pass is how the network learns from its mistakes. After making a prediction, we calculate how wrong the prediction was (the error). This method takes that error and works backward through the network, calculating how each part contributed to the mistake. This information is then used to adjust the network's internal settings to make better predictions next time.

CreateNewInstance()

Creates a new instance of the FeedForwardNeuralNetwork with the same configuration as the current instance.

protected override IFullModel<T, Tensor<T>, Tensor<T>> CreateNewInstance()

Returns

IFullModel<T, Tensor<T>, Tensor<T>>

A new FeedForwardNeuralNetwork instance with the same architecture, optimizer, and loss function as the current instance.

Remarks

This method creates a new instance of the FeedForwardNeuralNetwork with the same architecture, optimizer, and loss function as the current instance. This is useful for model cloning, ensemble methods, or cross-validation scenarios where multiple instances of the same model with identical configurations are needed.

For Beginners: This method creates a fresh copy of the neural network's blueprint.

When you need multiple versions of the same type of neural network with identical settings:

  • This method creates a new, empty network with the same configuration
  • It's like making a copy of a recipe before you start cooking
  • The new network has the same structure but no trained data
  • This is useful for techniques that need multiple models, like ensemble methods

For example, when testing your model on different subsets of data, you'd want each test to use a model with identical settings.

DeserializeNetworkSpecificData(BinaryReader)

Deserializes network-specific data that was not covered by the general deserialization process.

protected override void DeserializeNetworkSpecificData(BinaryReader reader)

Parameters

reader BinaryReader

The BinaryReader to read the data from.

Remarks

This method is called at the end of the general deserialization process to allow derived classes to read any additional data specific to their implementation.

For Beginners: Continuing the suitcase analogy, this is like unpacking that special compartment. After the main deserialization method has unpacked the common items (layers, parameters), this method allows each specific type of neural network to unpack its own unique items that were stored during serialization.

Forward(Tensor<T>)

Performs a forward pass through the network with the given input tensor.

public Tensor<T> Forward(Tensor<T> input)

Parameters

input Tensor<T>

The input tensor to process.

Returns

Tensor<T>

The output tensor after processing through all layers.

Remarks

The forward pass sequentially processes the input through each layer of the network. This is the core operation for making predictions with the neural network.

For Beginners: This method takes your input data and passes it through each layer of the neural network in sequence. Think of it like an assembly line where each station (layer) processes the data and passes it to the next station. The final output contains the network's prediction. This is the engine that powers the prediction process.

Exceptions

TensorShapeMismatchException

Thrown when the input shape doesn't match the expected input shape.

GetModelMetadata()

Retrieves metadata about the feed-forward neural network model.

public override ModelMetadata<T> GetModelMetadata()

Returns

ModelMetadata<T>

A ModelMetaData object containing information about the network.

Remarks

This method collects and returns various pieces of information about the network's structure and configuration.

For Beginners: This is like getting a summary of the network's blueprint. It tells you how many layers it has, what types of layers they are, and other important details about how the network is set up. This can be useful for documentation or debugging purposes.

InitializeLayers()

Initializes the layers of the neural network based on the provided architecture.

protected override void InitializeLayers()

Remarks

This method either uses custom layers provided in the architecture or creates default feed-forward layers if none are specified.

For Beginners: This method sets up the different processing stages (layers) of your neural network. If you've specified custom layers in your architecture, it will use those. If not, it will create a standard set of layers commonly used for feed-forward networks, with the right number of neurons at each stage based on your architecture settings.

Predict(Tensor<T>)

Makes a prediction using the feed-forward neural network for the given input tensor.

public override Tensor<T> Predict(Tensor<T> input)

Parameters

input Tensor<T>

The input tensor to make a prediction for.

Returns

Tensor<T>

The predicted output tensor.

Remarks

This method performs a forward pass through the network to generate a prediction. Unlike the vector-based Predict method, this takes a tensor directly as input.

For Beginners: This method makes predictions using the trained neural network. It accepts input data in tensor format (multi-dimensional arrays), processes it through all the network's layers, and returns the prediction as a tensor. This is useful when working with data that naturally has multiple dimensions.

SerializeNetworkSpecificData(BinaryWriter)

Serializes feed-forward neural network-specific data to a binary writer.

protected override void SerializeNetworkSpecificData(BinaryWriter writer)

Parameters

writer BinaryWriter

The BinaryWriter to write the data to.

Remarks

This method writes the specific parameters and state of the feed-forward neural network to a binary stream.

For Beginners: This is like saving the network's current state to a file. It records all the important information about the network so you can reload it later exactly as it is now. This is useful when you want to save a trained model for later use.

Train(Tensor<T>, Tensor<T>)

Trains the feed-forward neural network using the provided input and expected output.

public override void Train(Tensor<T> input, Tensor<T> expectedOutput)

Parameters

input Tensor<T>

The input tensor for training.

expectedOutput Tensor<T>

The expected output tensor for the given input.

Remarks

This method performs one training iteration, including forward pass, loss calculation, backward pass, and parameter update.

For Beginners: This is how the network learns. You show it some input data and tell it what the correct output should be. The network makes a guess, compares it to the correct answer, and then adjusts its internal settings to do better next time. This process is repeated many times with different examples to train the network.

UpdateParameters(Vector<T>)

Updates the parameters of all layers in the network.

public override void UpdateParameters(Vector<T> parameters)

Parameters

parameters Vector<T>

A vector containing all parameters for the network.

Remarks

This method distributes the parameters to each layer based on their parameter count. It's typically called during training after calculating parameter updates.

For Beginners: After the backward pass calculates how to improve the network, this method actually applies those improvements. It takes a list of updated settings (parameters) and distributes them to each layer in the network. This method is called repeatedly during training to gradually improve the network's accuracy.