LambdaLayer<T>

Layers · AiDotNet.NeuralNetworks.Layers

Represents a customizable layer that applies user-defined functions for both forward and backward passes.

Inheritance: LayerBase<T> → LambdaLayer<T>

Implements: IShapeContract

For Beginners

This layer lets you create your own custom operations in a neural network.

Think of the Lambda Layer as a “do-it-yourself” layer where:

  • You provide your own custom function to process the data
  • You can optionally provide a custom function for the learning process
  • It gives you flexibility to implement operations not covered by standard layers

For example, if you wanted to apply a special mathematical transformation that isn’t available in standard layers, you could define that transformation and use it in a Lambda Layer.

This is an advanced feature that gives you complete control when standard layers don’t provide what you need.

How It Works

The Lambda Layer allows for custom transformations to be incorporated into a neural network by accepting user-defined functions for both the forward and backward passes. This provides flexibility to implement custom operations that aren’t available as standard layers. The layer can optionally apply an activation function after the custom transformation.

Saving and compilation: a forward function written in engine ops (AiDotNetEngine.Current) is traced into a portable tensor program when the layer is saved, closures and captured weights included, and compiles with the rest of the model. A function that does work outside the engine can still run, but survives a save only through the expression-tree constructor.

Constructors

Constructor Summary
LambdaLayer(int[] inputShape, int[] outputShape, Expression<Func<Tensor<T>, Tensor<T>>> forwardExpression, IActivationFunction<T> activationFunction) Creates a lambda layer from an expression tree, which survives a save.
LambdaLayer(int[] inputShape, int[] outputShape, Func<Tensor<T>, Tensor<T>> forwardFunction, Func<Tensor<T>, Tensor<T>, Tensor<T>> backwardFunction, IActivationFunction<T> activationFunction) Initializes a new instance of the LambdaLayer class with the specified shapes, functions, and vector activation function.
LambdaLayer(int[] inputShape, int[] outputShape, Func<Tensor<T>, Tensor<T>> forwardFunction, Func<Tensor<T>, Tensor<T>, Tensor<T>> backwardFunction, IVectorActivationFunction<T> vectorActivationFunction) Initializes a new instance of the LambdaLayer class with the specified shapes, functions, and vector activation function.

Properties

Property Summary
bool SupportsTraining { get; } Gets a value indicating whether this layer supports training.

Methods

Method Summary
IReadOnlyList<OutputAxisContract> OutputAxesFor(int inputRank)
void ResetState() Resets the internal state of the layer.