Class ReshapeLayer<T>
- Namespace
- AiDotNet.NeuralNetworks.Layers
- Assembly
- AiDotNet.dll
Represents a reshape layer that transforms the dimensions of input data without changing its content.
public class ReshapeLayer<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>ReshapeLayer<T>
- Implements
-
ILayer<T>
- Inherited Members
Remarks
The ReshapeLayer rearranges the elements of the input tensor into a new shape without changing the data itself. This operation is useful for connecting layers with different shape requirements or for preparing data for specific layer types. The total number of elements must remain the same between the input and output shapes.
For Beginners: This layer changes how your data is organized without changing the data itself.
Think of the ReshapeLayer like reorganizing a deck of playing cards:
- If you have cards arranged in 4 rows of 13 cards (representing the 4 suits)
- You could reorganize them into 13 rows of 4 cards (representing the 13 ranks)
- The cards themselves haven't changed, just how they're arranged
For example, in image processing:
- You might have an image of shape [height, width, channels]
- But a particular layer might need the data as a flat vector
- A reshape layer can convert between these formats without losing information
Common use cases include:
- Flattening data (e.g., converting a 2D image to a 1D vector for a dense layer)
- Reshaping for convolutional operations (e.g., turning a vector into a 3D tensor)
- Batch dimension manipulation (e.g., splitting or combining batch items)
The key requirement is that the total number of elements stays the same - you're just reorganizing them into a different dimensional structure.
Constructors
ReshapeLayer(int[], int[])
Initializes a new instance of the ReshapeLayer<T> class.
public ReshapeLayer(int[] inputShape, int[] outputShape)
Parameters
inputShapeint[]The shape of the input tensor, excluding the batch dimension.
outputShapeint[]The shape of the output tensor, excluding the batch dimension.
Remarks
This constructor creates a new ReshapeLayer with the specified input and output shapes. It validates that the total number of elements remains the same between the input and output shapes, as the layer can only rearrange elements, not create or remove them.
For Beginners: This creates a new reshape layer for your neural network.
When you create this layer, you specify:
- inputShape: The current organization of your data (not including the batch dimension)
- outputShape: The desired organization of your data (not including the batch dimension)
For example:
- If inputShape is [28, 28] (like a 28×28 image)
- You could set outputShape to [784] to flatten it into a single vector
The constructor checks that the total number of elements stays the same:
- For the example above, 28×28 = 784, so the shapes are compatible
- If the total elements don't match, you'll get an error
The batch dimension (first dimension) is handled automatically and not included in these shapes.
Exceptions
- ArgumentException
Thrown when the total number of elements in the input and output shapes are not equal.
Properties
SupportsGpuExecution
Gets a value indicating whether this layer supports GPU execution.
protected override bool SupportsGpuExecution { get; }
Property Value
- bool
Always
truebecause reshape is a zero-copy operation 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 reshape 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
truefor ReshapeLayer, indicating that the layer can participate in backpropagation.
Remarks
This property indicates that the ReshapeLayer supports the training process through backpropagation. While the layer itself has no trainable parameters, it needs to properly propagate gradients during the backward pass, reshaping them to match the input shape.
For Beginners: This property tells you if the layer can participate in the learning process.
A value of true means:
- The layer can pass learning signals (gradients) backward through it
- It contributes to the training of the entire network
While this layer doesn't have any internal values that it learns directly, it's designed to let learning signals flow through it to previous layers. It just needs to reshape these signals to match the original input shape.
Methods
Backward(Tensor<T>)
Performs the backward pass of the reshape layer.
public override Tensor<T> Backward(Tensor<T> outputGradient)
Parameters
outputGradientTensor<T>The gradient of the loss with respect to the layer's output.
Returns
- Tensor<T>
The gradient of the loss with respect to the layer's input.
Remarks
This method implements the backward pass of the reshape layer, which is used during training to propagate error gradients back through the network. It reshapes the output gradient tensor back to the original input shape, effectively reversing the transformation done in the forward pass. This ensures that gradients flow correctly to previous layers.
For Beginners: This method transforms the learning signals back to the original shape.
During the backward pass:
- The layer receives gradients in the reshaped format (output shape)
- It needs to convert these gradients back to the original format (input shape)
- This allows previous layers to properly use these gradients for learning
Essentially, this is the reverse of the forward pass - it takes the learning signals and reorganizes them to match the original data structure, without changing their values.
This is necessary because each layer in the network expects gradients in the same shape as its original output.
Exceptions
- InvalidOperationException
Thrown when backward is called before forward.
BackwardGpu(IGpuTensor<T>)
Performs the backward pass on GPU using a zero-copy view reshape.
public override IGpuTensor<T> BackwardGpu(IGpuTensor<T> outputGradient)
Parameters
outputGradientIGpuTensor<T>The GPU-resident gradient tensor.
Returns
- IGpuTensor<T>
A GPU tensor view with the original input shape.
Remarks
The backward pass simply reshapes the gradient back to the original input shape. This is also a zero-copy view operation.
ExportComputationGraph(List<ComputationNode<T>>)
Exports the reshape 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 reshaped result.
Remarks
This method builds a computation graph for the reshape operation using a reshape node.
Forward(Tensor<T>)
Performs the forward pass of the reshape layer.
public override Tensor<T> Forward(Tensor<T> input)
Parameters
inputTensor<T>The input tensor to reshape.
Returns
- Tensor<T>
The reshaped output tensor.
Remarks
This method implements the forward pass of the reshape layer. It creates a new tensor with the specified output shape and copies the elements from the input tensor into the output tensor while preserving their order. The input tensor is cached for use during the backward pass.
For Beginners: This method reorganizes your data into the new shape.
During the forward pass:
- The layer saves the original input for later use in the backward pass
- It creates a new, empty tensor with the target shape
- It copies all values from the input to the output tensor
- The data values themselves stay exactly the same, just arranged differently
The layer handles each item in your batch separately, maintaining the batch structure.
Think of it like pouring water from one differently-shaped container to another - the amount of water stays the same, but it takes the shape of the new container.
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 reshaped dimensions.
Remarks
This method implements GPU-resident reshape by creating a view into the input tensor with the target shape. No data is copied - only the shape interpretation changes.
For Beginners: The GPU version of reshape 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, 28, 28, 1] and target is [784], the view will have shape [32, 784] but still points to the same GPU memory.
GetParameters()
Gets all trainable parameters of the reshape layer as a single vector.
public override Vector<T> GetParameters()
Returns
- Vector<T>
An empty vector since this layer has no trainable parameters.
Remarks
This method returns an empty vector because the ReshapeLayer has no trainable parameters. The method is required by the LayerBase class but is essentially a no-op for this layer.
For Beginners: This method returns an empty list because the layer has no learnable values.
As mentioned earlier, the reshape layer doesn't have any weights or biases that it learns during training. It just reorganizes the data structure.
This method returns an empty vector to indicate that there are no parameters to retrieve. It exists only because all layers in the network must implement it.
GetTargetShape()
Gets the target shape for the reshape operation.
public int[] GetTargetShape()
Returns
- int[]
The target shape array (excluding batch dimension).
ResetState()
Resets the internal state of the reshape layer.
public override void ResetState()
Remarks
This method resets the internal state of the reshape layer, clearing the cached input tensor from the forward pass. This is useful when starting to process a new batch of data.
For Beginners: This method clears the layer's memory to start fresh.
When resetting the state:
- The saved input from the previous forward pass is cleared
- The layer forgets any information from previous batches
This is important for:
- Processing a new, unrelated batch of data
- Preventing information from one batch affecting another
- Managing memory usage efficiently
Since this layer has no learned parameters, resetting just clears the temporarily stored input that was used for the backward pass.
UpdateParameters(T)
Updates the parameters of the reshape layer.
public override void UpdateParameters(T learningRate)
Parameters
learningRateTThe learning rate to use for the parameter updates.
Remarks
This method is required by the LayerBase class but does nothing in the ReshapeLayer because this layer has no trainable parameters to update. The ReshapeLayer only transforms the data structure without applying any learned transformations.
For Beginners: This method is empty because the layer has no internal values to update.
Unlike most layers in a neural network, the reshape layer doesn't have any weights or biases that need to be adjusted during training. It's purely a geometric transformation of the data structure.
The actual learning happens in other layers of the network that have trainable parameters like weights and biases.
This method exists only because all layers in the network must implement it.