nn
175 memberslucid.nnlucid.nn — neural-network layers, containers and parameter state.
The stateful half of the network API. Module owns parameters, buffers,
submodules and the hook machinery; Parameter marks a tensor as trainable
state that Module.parameters() collects and an optimizer steps. 176 names
are exported here — layers, containers, losses and the subpackages below.
Layers are deliberately thin: each holds its Parameters and delegates the
computation to the matching lucid.nn.functional entry point, so a layer
and its functional form are one implementation and one autograd path rather
than two that can drift apart.
Subpackages: functional (stateless ops), init (initialisation
schemes), utils (gradient clipping, spectral norm, pruning, packed
sequences), modules (the layer implementations re-exported here), and
quantized / qat / intrinsic for the quantization stack.
Namespace discipline: Module, Parameter and every layer live here and
never leak into the lucid top level — lucid.Linear does not exist by
design, so there is exactly one path to each name.
Sub-packages
Functional
130 memberslucid.nn.functionallucid.nn.functional — the stateless ops the layers are built from.
Fused Modules
4 memberslucid.nn.intrinsic`lucid.nn.intrinsic` — fused (intrinsic) float modules.
Init
26 memberslucid.nn.initnn.init: parameter initialization functions. All functions operate in-place and return the tensor.
NN Utils
21 memberslucid.nn.utilslucid.nn.utils: utility functions for neural networks.
QAT
9 memberslucid.nn.qat`lucid.nn.qat` — quantization-aware training modules.
Quantized
27 memberslucid.nn.quantized`lucid.nn.quantized` — quantized inference modules.
Classes
Module48 methodsBase class for all neural network modules.
UninitializedParameterWarningA lazy layer's parameters were read before they existed.
Parameter2 methodsA Tensor subclass that Module recognises as a learnable parameter.
UninitializedParameter1 methodsA parameter that exists before its shape does.
RemovableHandle4 methodsLightweight handle returned by every register_*_hook call.
Linear4 methodsApply a learnable affine transformation to incoming data.
NoisyLinear6 methodsA linear layer whose weights carry learnable exploration noise.
Identity2 methodsPass-through layer that returns its input unchanged.
Bilinear4 methodsApply a bilinear transformation to a pair of input tensors.
LazyLinear3 methodsLinear layer whose input dimension is inferred on the first forward call.
FusedLinear3 methodsLinear layer with a kernel-fused non-linear activation.
Conv1d1 methodsApplies a 1D convolution over a sequence of input signals.
Conv2d1 methodsApplies a 2D convolution over a batch of images or feature maps.
Conv3d1 methodsApplies a 3D convolution over volumetric data (e.g. video or medical scans).
ConvTranspose1d2 methodsApplies a 1D transposed convolution (fractionally-strided convolution).
ConvTranspose2d2 methodsApplies a 2D transposed convolution (fractionally-strided convolution).
ConvTranspose3d2 methodsApplies a 3D transposed convolution (fractionally-strided convolution).
LazyConv1d2 methodsA Conv1d that infers in_channels from the first input.
LazyConv2d2 methodsA Conv2d that infers in_channels from the first input.
LazyConv3d2 methodsA Conv3d that infers in_channels from the first input.
LazyConvTranspose1d2 methodsA ConvTranspose1d that infers in_channels from the first input.
LazyConvTranspose2d2 methodsA ConvTranspose2d that infers in_channels from the first input.
LazyConvTranspose3d2 methodsA ConvTranspose3d that infers in_channels from the first input.
ReLU3 methodsRectified Linear Unit activation function.
LeakyReLU3 methodsLeaky Rectified Linear Unit activation function.
ELU3 methodsExponential Linear Unit activation function.
CELU3 methodsContinuously Differentiable Exponential Linear Unit activation function.
SELU2 methodsScaled Exponential Linear Unit activation function.
GELU3 methodsGaussian Error Linear Unit activation function.
SiLU2 methodsSigmoid Linear Unit (Swish) activation function.
Mish1 methodsMish activation function.
Softplus3 methodsSoftplus activation function.
Hardswish1 methodsHard Swish activation function.
Hardsigmoid1 methodsHard Sigmoid activation function.
Sigmoid1 methodsSigmoid (logistic) activation function.
Tanh1 methodsHyperbolic tangent activation function.
Softmax3 methodsSoftmax activation function.
Softmax2d1 methodsSoftmax activation applied over the channel dimension of a 4-D tensor.
LogSoftmax3 methodsLog-Softmax activation function.
RReLU3 methodsRandomized Leaky ReLU activation function.
CosineSimilarity3 methodsCosine similarity between two tensors along a specified dimension.
PairwiseDistance3 methodsPairwise distance between corresponding rows of two tensors.
ReLU62 methodsReLU6 activation function.
PReLU3 methodsParametric Rectified Linear Unit activation function.
Threshold3 methodsThreshold activation function.
Hardtanh3 methodsHardtanh activation function.
LogSigmoid1 methodsLog-Sigmoid activation function.
Softsign1 methodsSoftsign activation function.
Softmin3 methodsSoftmin activation function.
GLU3 methodsGated Linear Unit activation function.
Hardshrink3 methodsHard Shrinkage activation function.
Tanhshrink1 methodsTanhshrink activation function.
Softshrink3 methodsSoft Shrinkage activation function.
LayerNorm3 methodsLayer normalization over the trailing dimensions of the input.
RMSNorm3 methodsRoot Mean Square Layer Normalization.
GroupNorm3 methodsGroup normalization over the channel dimension.
BatchNorm1dBatch normalization over a 2-D or 3-D input (N, C) or (N, C, L).
BatchNorm2dBatch normalization over a 4-D input (N, C, H, W).
BatchNorm3dBatch normalization over a 5-D input (N, C, D, H, W).
InstanceNorm1dInstance normalization for 3-D input (N, C, L).
InstanceNorm2dInstance normalization for 4-D input (N, C, H, W).
InstanceNorm3dInstance normalization for 5-D input (N, C, D, H, W).
LocalResponseNorm3 methodsLocal Response Normalization (LRN) across adjacent channels.
LazyBatchNorm1dBatchNorm1d with lazy num_features inference.
LazyBatchNorm2dBatchNorm2d with lazy num_features inference.
LazyBatchNorm3dBatchNorm3d with lazy num_features inference.
LazyInstanceNorm1dInstanceNorm1d with lazy num_features inference.
LazyInstanceNorm2dInstanceNorm2d with lazy num_features inference.
LazyInstanceNorm3dInstanceNorm3d with lazy num_features inference.
MaxPool1d3 methodsApplies 1-D max pooling over a sequence.
MaxPool2d3 methodsApplies 2-D max pooling over a spatial feature map.
MaxPool3d3 methodsApplies 3-D max pooling over a volumetric feature map.
AvgPool1d3 methodsApplies 1-D average pooling over a sequence.
AvgPool2d3 methodsApplies 2-D average pooling over a spatial feature map.
AvgPool3d3 methodsApplies 3-D average pooling over a volumetric feature map.
AdaptiveAvgPool1d3 methodsApplies adaptive 1-D average pooling to produce a fixed output length.
AdaptiveAvgPool2d3 methodsApplies adaptive 2-D average pooling to produce a fixed output size.
AdaptiveAvgPool3d3 methodsApplies adaptive 3-D average pooling to produce a fixed output volume.
AdaptiveMaxPool2d3 methodsApplies adaptive 2-D max pooling to produce a fixed output size.
AdaptiveMaxPool1d3 methodsApplies adaptive 1-D max pooling to produce a fixed output length.
AdaptiveMaxPool3d3 methodsApplies adaptive 3-D max pooling to produce a fixed output volume.
LPPool1d3 methodsApplies 1-D power-average (Lp-norm) pooling over a sequence.
LPPool2d3 methodsApplies 2-D power-average (Lp-norm) pooling over a spatial feature map.
LPPool3d3 methodsApplies 3-D power-average (Lp-norm) pooling over a volumetric input.
MaxUnpool1d1 methodsComputes a partial inverse of MaxPool1d.
MaxUnpool2d1 methodsComputes a partial inverse of MaxPool2d.
MaxUnpool3d1 methodsComputes a partial inverse of MaxPool3d.
FractionalMaxPool2d2 methodsApplies fractional max-pooling over a 2-D spatial feature map.
FractionalMaxPool3d2 methodsApplies fractional max-pooling over a 3-D volumetric feature map.
Dropout3 methodsRandomly zero individual tensor elements during training (inverted dropout).
Dropout1d3 methodsRandomly zero entire channels during training for 3-D inputs.
Dropout2d3 methodsRandomly zero entire feature-map channels during training for 4-D inputs.
Dropout3d3 methodsRandomly zero entire volumetric feature-map channels during training.
AlphaDropout3 methodsAlpha dropout — element-wise dropout that preserves SELU self-normalisation.
FeatureAlphaDropout3 methodsChannel-wise alpha dropout that preserves SELU self-normalisation.
Embedding3 methodsLearnable dense lookup table that maps integer token indices to vectors.
EmbeddingBag3 methodsEmbedding lookup table with per-bag reduction.
VectorQuantizer6 methodsNearest-neighbour codebook lookup with a straight-through gradient.
VectorQuantizerOutputWhat VectorQuantizer returns.
RotaryEmbedding2 methodsPrecomputed cos / sin tables for rotary positional embedding.
SinusoidalEmbedding2 methodsFixed 1-D sinusoidal positional encoding (Vaswani et al., 2017).
SinusoidalEmbedding2D2 methodsFixed 2-D sinusoidal positional encoding (DETR §A.4 / Carion 2020).
TimestepEmbedding2 methodsSinusoidal-frequency embedding of integer timesteps + 2-layer MLP.
GroupedQueryAttention1 methodsGrouped-query attention — multi-head attention with fewer key/value heads.
MultiheadAttention3 methodsMulti-head scaled dot-product attention.
MultiQueryAttention1 methodsMulti-query attention — multi-head attention with a single key/value head.
LSTM4 methodsLong Short-Term Memory (LSTM) recurrent layer.
GRU4 methodsMulti-layer Gated Recurrent Unit (GRU) recurrent layer.
RNN4 methodsMulti-layer Elman recurrent neural network (RNN).
LSTMCell3 methodsSingle time-step Long Short-Term Memory (LSTM) cell.
GRUCell3 methodsSingle time-step Gated Recurrent Unit (GRU) cell.
RNNCell3 methodsSingle time-step Elman RNN cell.
MSELoss3 methodsMean squared error (MSE) loss between each element of the prediction and the target.
L1Loss3 methodsMean absolute error (MAE) loss between each element of the prediction and the target.
CrossEntropyLoss3 methodsCross-entropy loss for multi-class classification.
NLLLoss3 methodsNegative log-likelihood loss.
BCELoss3 methodsBinary cross-entropy loss.
BCEWithLogitsLoss3 methodsBinary cross-entropy loss that accepts raw logits.
HuberLoss3 methodsHuber loss — a smooth interpolation between MSE and MAE.
SmoothL1Loss3 methodsSmooth L1 loss — a -parameterised Huber loss.
KLDivLoss3 methodsKullback–Leibler divergence loss.
TripletMarginLoss3 methodsTriplet margin loss for metric learning.
TripletMarginWithDistanceLoss3 methodsTriplet margin loss with a user-supplied distance function.
CosineEmbeddingLoss3 methodsCosine embedding loss for learning similarity/dissimilarity.
MarginRankingLoss3 methodsMargin ranking loss for pairwise ranking problems.
HingeEmbeddingLoss3 methodsHinge embedding loss for binary similarity learning.
PoissonNLLLoss3 methodsPoisson negative log-likelihood loss for count data.
GaussianNLLLoss3 methodsGaussian negative log-likelihood loss for heteroscedastic regression.
CTCLoss3 methodsConnectionist Temporal Classification (CTC) loss.
MultiMarginLoss3 methodsMulti-class hinge (SVM-style) margin loss.
MultilabelMarginLoss3 methodsMulti-label ranking margin loss.
MultiLabelMarginLossCamelCase alias for MultilabelMarginLoss.
SoftMarginLoss3 methodsSoft-margin binary hinge loss.
MultiLabelSoftMarginLoss3 methodsMulti-label soft-margin loss (BCE with logits averaged over classes).
Sequential9 methodsAn ordered container of modules applied one after another in sequence.
ModuleList9 methodsA list-like container that registers all child modules with the parent.
ModuleDict13 methodsA dict-like container that registers all child modules under string keys.
ParameterList8 methodsA list-like container that registers Parameter objects with the module.
ParameterDict13 methodsA dict-like container that registers Parameter objects under string keys.
Flatten3 methodsFlatten a contiguous range of dimensions into a single dimension.
Unflatten3 methodsExpand one dimension of a tensor into multiple dimensions.
Unfold3 methodsExtract sliding local blocks (patches) from a batched 4-D input tensor.
Fold3 methodsCombine an array of sliding local blocks back into a batched tensor.
ConstantPad1d1 methodsPad a 3-D tensor (N, C, L) along the last dimension with a constant.
ConstantPad2d1 methodsPad a 4-D tensor (N, C, H, W) on all four spatial sides with a constant.
ConstantPad3d1 methodsPad a 5-D tensor (N, C, D, H, W) on all six faces with a constant.
ZeroPad1d2 methodsPad a 3-D tensor (N, C, L) with zeros along the sequence dimension.
ZeroPad2d2 methodsPad a 4-D tensor (N, C, H, W) with zeros on all four spatial sides.
ZeroPad3d2 methodsPad a 5-D tensor (N, C, D, H, W) with zeros on all six faces.
ReflectionPad1d3 methodsPad a 3-D tensor (N, C, L) by reflecting at the sequence boundaries.
ReflectionPad2d3 methodsPad a 4-D tensor (N, C, H, W) by reflecting at all four spatial edges.
ReflectionPad3d3 methodsPad a 5-D tensor (N, C, D, H, W) by reflecting at the volume boundaries.
ReplicationPad1d3 methodsPad a 3-D tensor (N, C, L) by replicating the edge values.
ReplicationPad2d3 methodsPad a 4-D tensor (N, C, H, W) by replicating the edge pixels.
ReplicationPad3d3 methodsPad a 5-D tensor (N, C, D, H, W) by replicating the boundary voxels.
CircularPad1d3 methodsPad a 3-D tensor (N, C, L) by wrapping the sequence around itself.
CircularPad2d3 methodsPad a 4-D tensor (N, C, H, W) with wrap-around in both spatial axes.
CircularPad3d3 methodsPad a 5-D tensor (N, C, D, H, W) with wrap-around in all three axes.
Upsample3 methodsUpsample an input tensor to a given spatial size or scale factor.
UpsamplingNearest2d1 methodsNearest-neighbour upsampling for 4-D tensors (N, C, H, W).
UpsamplingBilinear2d1 methodsBilinear upsampling for 4-D tensors (N, C, H, W) with aligned corners.
PixelShuffle3 methodsRearrange channel-stacked sub-pixel planes into a high-resolution output.
PixelUnshuffle3 methodsReverse the sub-pixel rearrangement performed by PixelShuffle.
ChannelShuffle3 methodsShuffle channels across groups to enable cross-group information flow.
TransformerEncoderLayer3 methodsSingle transformer encoder layer: self-attention followed by a position-wise feed-forward network, with residual connections and layer normalisation.
TransformerEncoder3 methodsA stack of identical TransformerEncoderLayer modules.
TransformerDecoderLayer3 methodsSingle transformer decoder layer: masked self-attention, cross-attention over encoder memory, and a position-wise FFN — each with residual connections and layer normalisation.
TransformerDecoder3 methodsA stack of identical TransformerDecoderLayer modules.
Transformer4 methodsFull encoder-decoder Transformer architecture.
Functions
register_module_full_backward_hook→ RemovableHandleRegister a global post-hook fired after a module's backward pass.
register_module_full_backward_pre_hook→ RemovableHandleRegister a global pre-hook fired before any module's backward pass.
register_module_forward_hook→ RemovableHandleRegister a global post-forward hook fired after every Module.__call__.
register_module_forward_pre_hook→ RemovableHandleRegister a global pre-forward hook fired on every Module.__call__.
register_module_load_state_dict_pre_hook→ RemovableHandleRegister a global pre-hook fired for every Module during load_state_dict.
register_module_load_state_dict_post_hook→ RemovableHandleRegister a global post-hook fired after every Module's load_state_dict.