Class ConvolutionalNeuralNetwork<T>
- Namespace
- AiDotNet.NeuralNetworks
- Assembly
- AiDotNet.dll
Represents a Convolutional Neural Network (CNN) that processes multi-dimensional data.
public class ConvolutionalNeuralNetwork<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
TThe numeric type used for calculations (typically float or double).
- Inheritance
-
ConvolutionalNeuralNetwork<T>
- Implements
- Inherited Members
- Extension Methods
Remarks
A Convolutional Neural Network is specialized for processing data with a grid-like structure, such as images. It uses convolutional layers to automatically detect important features without manual feature extraction.
For Beginners: Think of a CNN as an image recognition system that works similarly to how your eyes and brain process visual information. Just as your brain automatically notices patterns like edges, shapes, and textures without conscious effort, a CNN automatically learns to detect these features in images. This makes CNNs excellent for tasks like recognizing objects in photos, detecting faces, or reading handwritten text.
Constructors
ConvolutionalNeuralNetwork(NeuralNetworkArchitecture<T>, IGradientBasedOptimizer<T, Tensor<T>, Tensor<T>>?, ILossFunction<T>?, double)
Initializes a new instance of the ConvolutionalNeuralNetwork class.
public ConvolutionalNeuralNetwork(NeuralNetworkArchitecture<T> architecture, IGradientBasedOptimizer<T, Tensor<T>, Tensor<T>>? optimizer = null, ILossFunction<T>? lossFunction = null, double maxGradNorm = 1)
Parameters
architectureNeuralNetworkArchitecture<T>The architecture defining the structure of the neural network.
optimizerIGradientBasedOptimizer<T, Tensor<T>, Tensor<T>>lossFunctionILossFunction<T>maxGradNormdouble
Remarks
CNNs are typically used with three-dimensional input data (height, width, and channels for images), but this implementation accepts any rank and lets layers adapt the dimensions as needed.
For Beginners: When creating a CNN, you need to provide a blueprint (architecture) that defines how your network will be structured. This implementation supports inputs of different ranks (1D, 2D, 3D, or batched 4D+), and the layers will handle reshaping and dimension adaptation internally.
Methods
Backward(Tensor<T>)
Performs a backward pass through the network to calculate gradients.
public Tensor<T> Backward(Tensor<T> outputGradient)
Parameters
outputGradientTensor<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. Think of it like learning from feedback - you make a guess, see how close you were, and adjust your thinking for next time.
CreateNewInstance()
Creates a new instance of the convolutional neural network model.
protected override IFullModel<T, Tensor<T>, Tensor<T>> CreateNewInstance()
Returns
- IFullModel<T, Tensor<T>, Tensor<T>>
A new instance of the convolutional neural network model with the same configuration.
Remarks
This method creates a new instance of the convolutional neural network model with the same configuration as the current instance. It is used internally during serialization/deserialization processes to create a fresh instance that can be populated with the serialized data.
For Beginners: This method creates a copy of the network structure without copying the learned data. Think of it like making a blank copy of the original network's blueprint - it has the same structure, same learning strategy, and same error measurement, but none of the knowledge that the original network has gained through training. This is primarily used when saving or loading models, creating an empty framework that can later be filled with the saved knowledge from the original network.
DeserializeNetworkSpecificData(BinaryReader)
Deserializes convolutional neural network-specific data from a binary reader.
protected override void DeserializeNetworkSpecificData(BinaryReader reader)
Parameters
readerBinaryReaderThe BinaryReader to read the data from.
Remarks
This method reads the specific parameters and state of the convolutional neural network from a binary stream.
For Beginners: This is like loading a saved network state from a file. It rebuilds the network exactly as it was when you saved it, including all its learned information.
Forward(Tensor<T>)
Performs a forward pass through the network with the given input tensor.
public Tensor<T> Forward(Tensor<T> input)
Parameters
inputTensor<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 (already in the right 3D format) 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 convolutional 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.
GetParameterCount()
public int GetParameterCount()
Returns
Remarks
For CNNs, the parameter count includes weights and biases from convolutional layers, pooling layers, and fully connected layers. The computation typically includes:
- Convolutional layer parameters: (kernel_height × kernel_width × input_channels × output_channels) + output_channels (biases)
- Fully connected layer parameters: (input_size × output_size) + output_size (biases)
- Pooling layers typically have no trainable parameters
For Beginners: CNNs usually have parameters in their convolutional filters (which detect features like edges and patterns) and in their fully connected layers (which make the final classification). The total number depends on the filter sizes, number of filters, and size of the fully connected layers. Larger kernels and more filters mean more parameters and thus more computational requirements.
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 CNN 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 in image recognition tasks. Think of this as either following your custom recipe or using a tried-and-tested recipe if you haven't provided one.
Predict(Tensor<T>)
Makes a prediction using the convolutional neural network for the given input.
public override Tensor<T> Predict(Tensor<T> input)
Parameters
inputTensor<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.
For Beginners: This is like asking the network to recognize an image. You give it the image data, and it processes it through all its layers to give you its best guess about what's in the image.
SerializeNetworkSpecificData(BinaryWriter)
Serializes convolutional neural network-specific data to a binary writer.
protected override void SerializeNetworkSpecificData(BinaryWriter writer)
Parameters
writerBinaryWriterThe BinaryWriter to write the data to.
Remarks
This method writes the specific parameters and state of the convolutional 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.
Train(Tensor<T>, Tensor<T>)
Trains the convolutional neural network using the provided input and expected output.
public override void Train(Tensor<T> input, Tensor<T> expectedOutput)
Parameters
inputTensor<T>The input tensor for training.
expectedOutputTensor<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 an image (input) and tell it what should be in that image (expected output). The network makes a guess, compares it to the correct answer, and then adjusts its internal settings to do better next time.
UpdateParameters(Vector<T>)
Updates the parameters of all layers in the network.
public override void UpdateParameters(Vector<T> parameters)
Parameters
parametersVector<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. Think of it like fine-tuning each part of a machine based on performance feedback. This method is called repeatedly during training to gradually improve the network's accuracy.