The initial values assigned to a network’s weights have a profound effect on whether training converges, how quickly it does so, and how deep the network can be. Poorly initialised weights lead to either vanishing gradients (weights shrink toward zero) or exploding gradients (signals amplify out of control). TheDocumentation Index
Fetch the complete documentation index at: https://mintlify.com/antlobach/clorch/llms.txt
Use this file to discover all available pages before exploring further.
clorch.nn.init namespace provides a set of in-place initializers that modify a tensor’s values directly — wrapping the corresponding LibTorch C++ routines.
All
init/ functions modify the tensor in place and return the same tensor. They run inside a no-grad scope so the initialization operation itself is not recorded in the autograd graph.Basic Initializers
These functions are useful for quick experiments, zeroing bias tensors, or setting a known baseline before applying a more sophisticated scheme.Xavier (Glorot) Initialization
Xavier initialization scales the random weights based on the number of input and output connections (fan-in and fan-out) so that the variance of activations and gradients stays roughly constant across layers. It is designed for layers followed by symmetric activations such astanh or sigmoid.
Xavier Uniform
Draws weights from a uniform distribution bounded by± sqrt(6 / (fan_in + fan_out)) scaled by an optional gain.
Xavier Normal
Draws weights from a normal distribution with standard deviationsqrt(2 / (fan_in + fan_out)) scaled by gain.
Kaiming (He) Initialization
Kaiming initialization accounts for the fact that ReLU-like activations zero out half their inputs. It scales weights so that the variance is preserved through the forward pass (:fan-in mode) or the backward pass (:fan-out mode).
Kaiming Uniform
Kaiming Normal
Kaiming Options
| Option | Default | Description |
|---|---|---|
:non-linearity | :leaky-relu | The activation following this layer. Supported: :relu, :leaky-relu, :tanh, :sigmoid, :linear |
:a | 0 | Negative slope for :leaky-relu. Ignored for other non-linearities |
:mode | :fan-in | :fan-in preserves variance in the forward pass; :fan-out preserves variance in the backward pass |
The default
:non-linearity in clorch.nn.init is :leaky-relu (matching PyTorch’s default). Always pass :non-linearity :relu explicitly when initializing layers before a standard ReLU activation.Usage Pattern Inside defmodel
The recommended place to initialize custom weights is immediately after constructing the model, operating directly on the tensor fields exposed by the record.
Choosing the Right Initializer
| Scenario | Recommended Initializer |
|---|---|
Layer followed by tanh or sigmoid | xavier-uniform! or xavier-normal! |
Layer followed by relu | kaiming-uniform! with :non-linearity :relu |
Layer followed by leaky-relu | kaiming-normal! with :non-linearity :leaky-relu, :a slope |
| Embedding or output layer | normal! with a small std (e.g. 0.02) |
| Bias terms | zeros! |
| Debug / ablation baseline | constant! or ones! |