Class FlattenLayer<T>
- Namespace
- AiDotNet.NeuralNetworks.Layers
- Assembly
- AiDotNet.dll
Represents a flatten layer that reshapes multi-dimensional input data into a 1D vector.
public class FlattenLayer<T> : LayerBase<T>, ILayer<T>, IJitCompilable<T>, IDiagnosticsProvider, IWeightLoadable<T>, IDisposable
Type Parameters
TThe numeric type used for calculations, typically float or double.
- Inheritance
-
LayerBase<T>FlattenLayer<T>
- Implements
-
ILayer<T>
- Inherited Members
Remarks
A flatten layer transforms multi-dimensional input data (such as images or feature maps) into a one-dimensional vector. This is often necessary when transitioning from convolutional layers to fully connected layers in a neural network. The flatten operation preserves all values and their order, just changing the way they are arranged from a multi-dimensional tensor to a single vector.
For Beginners: A flatten layer converts multi-dimensional data into a simple list of numbers.
Imagine you have a 2D grid of numbers (like a small image):
[
[1, 2, 3],
[4, 5, 6]
]
The flatten layer turns this into a single row:
[1, 2, 3, 4, 5, 6]
This transformation is needed because:
- Convolutional layers work with 2D or 3D data (like images)
- Fully connected layers expect a simple list of numbers
- Flatten layers bridge these two types of layers
Think of it like taking a book (a 3D object with pages) and reading all the text in order from beginning to end (a 1D sequence). All the information is preserved, but it's rearranged into a different shape.
Constructors
FlattenLayer(int[])
public FlattenLayer(int[] inputShape)
Parameters
inputShapeint[]
Properties
SupportsGpuExecution
Gets a value indicating whether this layer supports GPU execution.
protected override bool SupportsGpuExecution { get; }
Property Value
- bool
Always
truebecause flatten is a zero-copy reshape that can be done via GPU tensor view.
SupportsJitCompilation
Gets a value indicating whether this layer supports JIT compilation.
public override bool SupportsJitCompilation { get; }
Property Value
- bool
Always
truebecause flatten is a simple reshape operation that can be JIT compiled.
SupportsTraining
Gets a value indicating whether this layer supports training.
public override bool SupportsTraining { get; }
Property Value
- bool
Always
falsebecause flatten layers have no trainable parameters.
Remarks
This property indicates that the flatten layer does not have any trainable parameters. The layer simply performs a reshape operation and does not learn during training. However, it still participates in backpropagation by passing gradients back to previous layers in the correct shape.
For Beginners: This property tells you that this layer doesn't learn or change during training.
A value of false means:
- The layer has no weights or biases to adjust
- It performs the same operation regardless of training
- It's a fixed transformation layer, not a learning layer
Unlike convolutional or fully connected layers (which learn patterns from data), the flatten layer just reorganizes data without changing its content.
It's like rearranging furniture in a room - you're not adding or removing anything, just changing how it's organized.
Methods
Backward(Tensor<T>)
Performs the backward pass of the flatten layer, reshaping gradients back to the original input shape.
public override Tensor<T> Backward(Tensor<T> outputGradient)
Parameters
outputGradientTensor<T>The gradient tensor from the next layer. Shape: [batchSize, outputSize].
Returns
- Tensor<T>
The gradient tensor reshaped to the original input shape. Shape: [batchSize, ...inputShape].
Exceptions
- InvalidOperationException
Thrown when backward is called before forward.
BackwardGpu(IGpuTensor<T>)
Performs GPU-resident backward pass for the flatten layer. Reshapes the gradient back to the original input shape.
public override IGpuTensor<T> BackwardGpu(IGpuTensor<T> outputGradient)
Parameters
outputGradientIGpuTensor<T>GPU-resident gradient from the next layer.
Returns
- IGpuTensor<T>
GPU-resident gradient to pass to the previous layer.
Exceptions
- InvalidOperationException
Thrown if ForwardGpu was not called first.
ExportComputationGraph(List<ComputationNode<T>>)
Exports the flatten layer's forward pass as a JIT-compilable computation graph.
public override 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 flattened result.
Remarks
This method builds a computation graph for the flatten operation using a reshape node. The flatten operation is equivalent to reshaping the input to [batchSize, product of dimensions].
Forward(Tensor<T>)
public override Tensor<T> Forward(Tensor<T> input)
Parameters
inputTensor<T>
Returns
- Tensor<T>
ForwardGpu(params IGpuTensor<T>[])
Performs the forward pass on GPU using a zero-copy view reshape.
public override IGpuTensor<T> ForwardGpu(params IGpuTensor<T>[] inputs)
Parameters
inputsIGpuTensor<T>[]
Returns
- IGpuTensor<T>
A GPU tensor view with the flattened shape.
Remarks
This method implements GPU-resident flatten by creating a view into the input tensor with the flattened shape. No data is copied - only the shape interpretation changes.
For Beginners: The GPU version of flatten is very efficient because: - It doesn't move any data - It just tells the GPU "interpret this same data with a different shape" - This is called a "view" operation
For example, if input has shape [32, 7, 7, 64], the view will have shape [32, 3136] but still points to the exact same memory on the GPU.
GetParameters()
Gets the trainable parameters of the layer.
public override Vector<T> GetParameters()
Returns
- Vector<T>
An empty vector since flatten layers have no trainable parameters.
Remarks
This method is a required override from the base class, but the flatten layer has no trainable parameters to retrieve, so it returns an empty vector.
For Beginners: This method returns an empty list because flatten layers have no learnable values.
Unlike layers with weights and biases:
- Flatten layers don't have any parameters that change during training
- They perform a fixed operation (reshaping) that doesn't involve learning
- There are no values to save when storing a trained model
This method returns an empty vector, indicating there are no parameters to collect.
ResetState()
Resets the internal state of the layer.
public override void ResetState()
Remarks
This method resets the internal state of the layer by clearing the cached input from the previous forward pass. This is useful when starting to process a new batch of data or when switching between training and inference modes.
For Beginners: This method clears the layer's memory to start fresh.
When resetting the state:
- The saved input is cleared
- The layer forgets the previous data it processed
- This frees up memory and prepares for new data
This is typically called:
- Between training batches
- When switching from training to evaluation mode
- When starting to process completely new data
It's like wiping a whiteboard clean before starting a new calculation.
UpdateParameters(T)
Updates the parameters of the layer based on the calculated gradients.
public override void UpdateParameters(T learningRate)
Parameters
learningRateTThe learning rate to use for parameter updates.
Remarks
This method is a required override from the base class, but the flatten layer has no trainable parameters to update, so it performs no operation.
For Beginners: This method does nothing for flatten layers because they have no adjustable weights.
Unlike most layers (like convolutional or fully connected layers):
- Flatten layers don't have weights or biases to learn
- They just rearrange the data without modifying it
- There's nothing to update during training
This method exists only to fulfill the requirements of the base layer class. The flatten layer participates in training by reorganizing activations and gradients, not by updating internal parameters.