Class UpsamplingLayer<T>
- Namespace
- AiDotNet.NeuralNetworks.Layers
- Assembly
- AiDotNet.dll
Represents an upsampling layer that increases the spatial dimensions of input tensors using nearest-neighbor interpolation.
public class UpsamplingLayer<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>UpsamplingLayer<T>
- Implements
-
ILayer<T>
- Inherited Members
Remarks
An upsampling layer increases the spatial dimensions (height and width) of input tensors by repeating values from the input to create a larger output. This implementation uses nearest-neighbor interpolation, which repeats each value in the input tensor multiple times based on the scale factor to create the upsampled output.
For Beginners: This layer makes images or feature maps larger by simply repeating pixels.
Think of it like zooming in on a digital image:
- When you zoom in on a pixelated image, each original pixel becomes a larger square
- This layer does the same thing to feature maps inside the neural network
- It's like stretching an image without adding any new information
For example, with a scale factor of 2:
- A 4×4 image becomes an 8×8 image
- Each pixel in the original image is copied to a 2×2 block in the output
- This creates a larger image that preserves the original content but with more pixels
This is useful for tasks like image generation or upscaling, where you need to increase the resolution of features that the network has processed.
Constructors
UpsamplingLayer(int[], int)
Initializes a new instance of the UpsamplingLayer<T> class.
public UpsamplingLayer(int[] inputShape, int scaleFactor)
Parameters
inputShapeint[]The shape of the input tensor.
scaleFactorintThe factor by which to increase spatial dimensions.
Remarks
This constructor creates an upsampling layer with the specified input shape and scale factor. The output shape is calculated based on the input shape and scale factor.
For Beginners: This constructor creates a new upsampling layer.
The parameters you provide determine:
- inputShape: The dimensions of the data coming into this layer
- scaleFactor: How much larger the output should be compared to the input
For example, if inputShape is [3, 32, 32] (representing 3 channels of a 32×32 image) and scaleFactor is 2, the output shape will be [3, 64, 64] - the same number of channels but twice the height and width.
Properties
SupportsGpuExecution
Gets a value indicating whether this layer supports GPU execution.
protected override bool SupportsGpuExecution { get; }
Property Value
SupportsJitCompilation
Gets whether this layer supports JIT compilation.
public override bool SupportsJitCompilation { get; }
Property Value
- bool
Always returns true as upsampling operations can be efficiently compiled.
Remarks
Upsampling layers support JIT compilation since the nearest-neighbor interpolation is a straightforward operation that can be optimized at compile time.
SupportsTraining
Gets a value indicating whether this layer supports training.
public override bool SupportsTraining { get; }
Property Value
- bool
truefor this layer, even though it has no trainable parameters, to allow gradient propagation.
Remarks
This property indicates whether the upsampling layer can be included in the training process. Although this layer does not have trainable parameters, it returns true to allow gradient propagation through the layer during backpropagation.
For Beginners: This property tells you if the layer participates in the learning process.
A value of true means:
- The layer is part of the training process
- It can pass gradients backward to previous layers
- It helps the network learn, even though it doesn't have its own parameters to adjust
This is like being a messenger that relays feedback to earlier parts of the network, even though the messenger doesn't change its own behavior.
Methods
Backward(Tensor<T>)
Performs the backward pass of the upsampling 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 upsampling layer, which is used during training to propagate error gradients back through the network. For each position in the input gradient, it sums up the corresponding gradients from the scale factor × scale factor region in the output gradient.
For Beginners: This method calculates how the layer's input should change to reduce errors.
During the backward pass:
- The layer receives information about how its output should change (outputGradient)
- For each position in the input:
- It finds all the positions in the output that came from this input position
- It sums up the gradients from all those output positions
- This sum becomes the gradient for the input position
This is like collecting feedback from all the copies made during the forward pass and combining it into a single piece of feedback for the original value.
Exceptions
- InvalidOperationException
Thrown when trying to perform a backward pass before a forward pass.
BackwardGpu(IGpuTensor<T>)
Performs the backward pass on GPU tensors.
public override IGpuTensor<T> BackwardGpu(IGpuTensor<T> gradOutput)
Parameters
gradOutputIGpuTensor<T>Gradient from the next layer.
Returns
- IGpuTensor<T>
Gradient with respect to the input.
Exceptions
- InvalidOperationException
Thrown when GPU backend is unavailable or forward pass was not called.
ExportComputationGraph(List<ComputationNode<T>>)
Exports this layer's computation as a differentiable computation graph for JIT compilation.
public override ComputationNode<T> ExportComputationGraph(List<ComputationNode<T>> inputNodes)
Parameters
inputNodesList<ComputationNode<T>>List to which input variable nodes should be added.
Returns
- ComputationNode<T>
The output computation node representing this layer's operation.
Remarks
This method builds a computation graph representation of the upsampling operation using nearest-neighbor interpolation. The operation repeats each value in the input based on the scale factor.
For Beginners: This method creates an optimized version of the upsampling operation.
For upsampling layers:
- Creates a placeholder for the input tensor
- Applies the upsampling operation (repeat values)
- Returns a computation graph for efficient execution
This allows for faster inference by pre-compiling the upsampling operation.
Exceptions
- ArgumentNullException
Thrown when inputNodes is null.
Forward(Tensor<T>)
Performs the forward pass of the upsampling layer.
public override Tensor<T> Forward(Tensor<T> input)
Parameters
inputTensor<T>The input tensor to upsample.
Returns
- Tensor<T>
The upsampled output tensor.
Remarks
This method implements the forward pass of the upsampling layer using nearest-neighbor interpolation. It repeats each value in the input tensor according to the scale factor to create a larger output tensor.
For Beginners: This method creates a larger version of the input by repeating values.
During the forward pass:
- The layer receives an input tensor (like a stack of feature maps)
- For each value in the input:
- The value is copied multiple times based on the scale factor
- These copies form a block in the output tensor
- This creates an output that is larger but contains the same information
For example, with a scale factor of 2, each pixel becomes a 2×2 block of identical pixels. This is the simplest form of upsampling, which preserves the original content but increases the spatial dimensions.
ForwardGpu(params IGpuTensor<T>[])
Performs the forward pass on GPU tensors.
public override IGpuTensor<T> ForwardGpu(params IGpuTensor<T>[] inputs)
Parameters
inputsIGpuTensor<T>[]GPU tensor inputs.
Returns
- IGpuTensor<T>
GPU tensor output after upsampling.
Exceptions
- ArgumentException
Thrown when no input tensor is provided.
- InvalidOperationException
Thrown when GPU backend is unavailable.
GetParameters()
Gets all trainable parameters of the layer as a single vector.
public override Vector<T> GetParameters()
Returns
- Vector<T>
An empty vector, as the layer has no trainable parameters.
Remarks
This method returns an empty vector as the upsampling layer does not have any trainable parameters. It is included to conform to the base class interface.
For Beginners: This method returns an empty list because this layer has no learnable values.
Since the upsampling layer:
- Has no weights or biases
- Performs a fixed operation that doesn't need to be learned
- Only transforms the input according to predefined rules
It returns an empty vector, indicating to the optimization process that there are no parameters to update for this layer.
ResetState()
Resets the internal state of the layer.
public override void ResetState()
Remarks
This method resets the internal state of the upsampling layer by clearing the cached input tensor. This is useful when starting to process a new, unrelated input.
For Beginners: This method clears the layer's memory of what it last processed.
When resetting the state:
- The layer forgets what input it recently processed
- This helps prepare it for processing new, unrelated inputs
- It's like clearing a workspace before starting a new project
This is mostly important during training, where the layer needs to maintain consistency between forward and backward passes.
UpdateParameters(T)
Updates the parameters of the layer using the calculated gradients.
public override void UpdateParameters(T learningRate)
Parameters
learningRateTThe learning rate for parameter updates.
Remarks
This method is empty as the upsampling layer does not have any trainable parameters to update. It is included to conform to the base class interface.
For Beginners: This method does nothing because this layer has no learnable values.
The upsampling layer:
- Performs a fixed, predefined operation (repeating values)
- Has no weights or biases to adjust during training
- Only passes gradients backward without changing itself
This method exists only to satisfy the requirements of the base layer class, similar to how a purely functional node in a network would need to implement this method even though it has nothing to update.