Table of Contents

Class InputLayer<T>

Namespace
AiDotNet.NeuralNetworks.Layers
Assembly
AiDotNet.dll

Represents an input layer that passes input data through unchanged to the next layer in the neural network.

public class InputLayer<T> : LayerBase<T>, ILayer<T>, IJitCompilable<T>, IDiagnosticsProvider, IWeightLoadable<T>, IDisposable

Type Parameters

T

The numeric type used for calculations, typically float or double.

Inheritance
InputLayer<T>
Implements
Inherited Members

Remarks

The Input Layer serves as the entry point for data into a neural network. Unlike other layers, it doesn't transform the data or learn any parameters; it simply validates and passes the input through to the next layer. This layer establishes the dimensionality of the input data for the entire network.

For Beginners: This layer is like the doorway to your neural network.

Think of the InputLayer as:

  • The entrance where your data first enters the neural network
  • A way to tell the network what shape your data has
  • A pass-through that doesn't change your data

For example, if you're processing images that are 28x28 pixels, you would use an InputLayer with inputSize=784 (28×28) to tell the network about the size of each image.

Unlike other layers, the InputLayer doesn't learn or transform anything - it just passes your data into the network.

Constructors

InputLayer(int)

Initializes a new instance of the InputLayer<T> class with the specified input size.

public InputLayer(int inputSize)

Parameters

inputSize int

The size of the input vector.

Remarks

This constructor creates a new Input Layer with the specified input size. The layer uses an identity activation function, meaning it does not transform the input data in any way.

For Beginners: This creates a new doorway to your neural network with a specific size.

When creating an Input Layer, you specify:

  • inputSize: How many features your data has (like pixels in an image or properties in a dataset)

This size tells the rest of the network what dimensions to expect for input data. For example, if your data has 10 features, you would set inputSize=10.

The layer is automatically set up to pass data through without changing it.

Properties

SupportsGpuExecution

Gets whether this layer has a GPU execution implementation for inference.

protected override bool SupportsGpuExecution { get; }

Property Value

bool

Remarks

Override this to return true when the layer implements ForwardGpu(params IGpuTensor<T>[]). The actual CanExecuteOnGpu property combines this with engine availability.

For Beginners: This flag indicates if the layer has GPU code for the forward pass. Set this to true in derived classes that implement ForwardGpu.

SupportsJitCompilation

Gets whether this layer supports JIT compilation.

public override bool SupportsJitCompilation { get; }

Property Value

bool

True if the layer can be JIT compiled, false otherwise.

Remarks

This property indicates whether the layer has implemented ExportComputationGraph() and can benefit from JIT compilation. All layers MUST implement this property.

For Beginners: JIT compilation can make inference 5-10x faster by converting the layer's operations into optimized native code.

Layers should return false if they:

  • Have not yet implemented a working ExportComputationGraph()
  • Use dynamic operations that change based on input data
  • Are too simple to benefit from JIT compilation

When false, the layer will use the standard Forward() method instead.

SupportsTraining

Gets a value indicating whether this layer supports training.

public override bool SupportsTraining { get; }

Property Value

bool

false because the input layer has no trainable parameters.

Remarks

This property indicates whether the layer can be trained through backpropagation. The InputLayer always returns false because it contains no trainable parameters.

For Beginners: This property tells you if the layer can learn from data.

A value of false means:

  • The layer doesn't have any values that can be adjusted during training
  • It will behave exactly the same before and after training
  • It doesn't participate in the learning process

The input layer doesn't need to learn because its only job is to feed data into the network.

Methods

Backward(Tensor<T>)

Performs the backward pass of the input layer, simply returning the output gradient unchanged.

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

Parameters

outputGradient Tensor<T>

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

Returns

Tensor<T>

The same output gradient, unchanged.

Remarks

This method implements the backward pass of the input layer. Since the input layer does not transform the data and has no parameters to learn, it simply passes the gradient back unchanged.

For Beginners: This method passes error information backward through the network.

During the backward pass:

  • The layer receives information about how its output contributed to errors
  • Since this layer doesn't change anything, it passes this information back unchanged
  • There are no parameters to update

This method is still needed even though the layer doesn't learn, because it's part of the backpropagation process that allows the entire network to learn.

BackwardGpu(IGpuTensor<T>)

Computes the gradient of the loss with respect to the input on the GPU.

public override IGpuTensor<T> BackwardGpu(IGpuTensor<T> outputGradient)

Parameters

outputGradient IGpuTensor<T>

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

Returns

IGpuTensor<T>

The same output gradient, unchanged.

Remarks

InputLayer is an identity operation, so the gradient passes through unchanged.

ExportComputationGraph(List<ComputationNode<T>>)

Exports the layer's computation graph for JIT compilation.

public override ComputationNode<T> ExportComputationGraph(List<ComputationNode<T>> inputNodes)

Parameters

inputNodes List<ComputationNode<T>>

List to populate with input computation nodes.

Returns

ComputationNode<T>

The output computation node representing the layer's operation.

Remarks

This method constructs a computation graph representation of the layer's forward pass that can be JIT compiled for faster inference. All layers MUST implement this method to support JIT compilation.

For Beginners: JIT (Just-In-Time) compilation converts the layer's operations into optimized native code for 5-10x faster inference.

To support JIT compilation, a layer must:

  1. Implement this method to export its computation graph
  2. Set SupportsJitCompilation to true
  3. Use ComputationNode and TensorOperations to build the graph

All layers are required to implement this method, even if they set SupportsJitCompilation = false.

Forward(Tensor<T>)

Performs the forward pass of the input layer, simply returning the input unchanged.

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

Parameters

input Tensor<T>

The input tensor to process.

Returns

Tensor<T>

The same input tensor, unchanged.

Remarks

This method implements the forward pass of the input layer. Since the input layer does not transform the data, it simply returns the input tensor as is.

For Beginners: This method passes your data into the neural network without changing it.

The forward pass:

  • Takes in your data
  • Passes it through unchanged
  • Sends it to the next layer

This simple pass-through behavior is all that's needed for an input layer, as its purpose is just to feed data into the network.

ForwardGpu(params IGpuTensor<T>[])

Performs the forward pass of the layer on GPU.

public override IGpuTensor<T> ForwardGpu(params IGpuTensor<T>[] inputs)

Parameters

inputs IGpuTensor<T>[]

The GPU-resident input tensor(s).

Returns

IGpuTensor<T>

The GPU-resident output tensor.

Remarks

This method performs the layer's forward computation entirely on GPU. The input and output tensors remain in GPU memory, avoiding expensive CPU-GPU transfers.

For Beginners: This is like Forward() but runs on the graphics card.

The key difference:

  • Forward() uses CPU tensors that may be copied to/from GPU
  • ForwardGpu() keeps everything on GPU the whole time

Override this in derived classes that support GPU acceleration.

Exceptions

NotSupportedException

Thrown when the layer does not support GPU execution.

GetParameters()

Returns an empty vector since the input layer has no trainable parameters.

public override Vector<T> GetParameters()

Returns

Vector<T>

An empty vector.

Remarks

This method returns an empty vector since the InputLayer has no trainable parameters. It is implemented as required by the LayerBase interface.

For Beginners: This method returns an empty list because there are no parameters.

Since the input layer:

  • Has no weights or biases
  • Doesn't have any learnable values

This method returns an empty vector to indicate there are no parameters. Other layers would return their weights and biases here.

ResetState()

Reset state is a no-op for the input layer since it maintains no state.

public override void ResetState()

Remarks

This method is implemented as required by the LayerBase interface but does nothing for the InputLayer since it maintains no state between forward and backward passes.

For Beginners: This method exists but does nothing because there's no state to reset.

Since the input layer:

  • Doesn't store any information between processing steps
  • Doesn't keep track of previous inputs or outputs
  • Has no memory that needs to be cleared

This method is included only because all layers must have this method, but it doesn't actually do anything for the input layer.

UpdateParameters(T)

Update parameters is a no-op for the input layer since it has no trainable parameters.

public override void UpdateParameters(T learningRate)

Parameters

learningRate T

The learning rate (unused in this layer).

Remarks

This method is implemented as required by the LayerBase interface but does nothing for the InputLayer since it has no parameters to update.

For Beginners: This method exists but does nothing because there's nothing to update.

Since the input layer:

  • Has no weights or biases
  • Doesn't transform the data
  • Doesn't learn from training

This method is included only because all layers must have this method, but it doesn't actually do anything for the input layer.