Layer class abstract
The abstract base class for all neural network layers.
A Layer is the fundamental, callable building block of a neural network. It
encapsulates both a state (its trainable parameters, like weights and biases)
and a transformation from input tensors to output tensors (the forward pass).
Layers are designed to be chained together in a model, such as an SNetwork,
where the output of one layer becomes the input to the next, forming the
complete network architecture.
Lifecycle:
- Instantiation: A layer is created (e.g.,
DenseLayer(64)). Its weights are not yet created. - Build: The first time the layer is called with an input, the
buildmethod runs automatically. It uses the input's shape to initialize the weights and biases with the correct dimensions. This is called deferred initialization. - Forward Pass: On every call, the
forwardmethod is executed to perform the layer's core mathematical operations.
Example
// Define a layer (weights are not created yet).
Layer dense = DenseLayer(32, activation: ReLU());
// Create an input tensor.
Tensor<Vector> input = Tensor<Vector>([1.0, 2.0, 3.0]);
// Call the layer. This will first build it, then run the forward pass.
Tensor<Vector> output = dense.call(input) as Tensor<Vector>;
- Implementers
- AveragePooling2DLayer
- BatchNorm1D
- BatchNorm2D
- Conv2DLayer
- ConvLSTMLayer
- DenseLayer
- DenseLayerMatrix
- DropoutLayer
- DropoutLayerMatrix
- DualLSTMLayer
- EmbeddingLayer
- EmbeddingLayerMatrix
- FlattenLayer
- GeneralizedChainedScaleLayer
- GlobalAveragePooling1D
- GlobalAveragePoolingLayer
- LayerNormalization
- LayerNormalizationVector
- LSTMLayer
- MaxPooling1DLayer
- MaxPooling2DLayer
- MultiHeadAttention
- MultiTierLSTMLayer
- PositionalEncoding
- ReLULayer
- ReLULayerMatrix
- ReshapeVectorToMatrixLayer
- RNN
- SingleHeadAttention
- SNetwork
- TransformerEncoderBlock
Constructors
- Layer()
Properties
- hashCode → int
-
The hash code for this object.
no setterinherited
- name → String
-
A user-friendly name for the layer (e.g., 'dense', 'lstm').
no setter
-
parameters
→ List<
Tensor> -
A list of all trainable tensors (weights and biases) in the layer.
no setter
- runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
Methods
-
build(
Tensor input) → void - Initializes the layer's parameters based on the shape of the first input.
-
call(
Tensor input) → Tensor - The public, callable interface for the layer.
-
forward(
Tensor input) → Tensor - The core logic of the layer's transformation.
-
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
toString(
) → String -
A string representation of this object.
inherited
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited