sparse_(tensor: Tensor, sparsity: float, std: float = 0.01, generator: _C_engine.Generator | None = None)Initialise a 2-D tensor in-place with a sparse random matrix.
For each column, a random subset of ceil(sparsity * rows) rows
is set to zero; the remaining entries are drawn from
. Sparse initialisation, due to
Martens (2010), encourages each output unit to depend on only a
small number of inputs at the start of training and helps in
ill-conditioned regimes — particularly for very wide layers where a
dense Gaussian would have an unfavourable condition number.
Parameters
tensorTensorTwo-dimensional tensor to fill in place.
sparsityfloatFraction of entries in each column that are zeroed. Must lie in
[0, 1].stdfloat= 0.01Standard deviation of the Gaussian used for the non-zero
entries. Default
0.01.PRNG used for both the Gaussian fill and the column zero-mask.
Default
None.Returns
Tensortensor populated with a column-sparse Gaussian matrix.
Raises
ValueErrorIf
tensor.ndim != 2 or sparsity is outside [0, 1].Notes
The expected per-entry variance is
Examples
>>> import lucid
>>> from lucid.nn.init import sparse_
>>> w = lucid.empty(256, 256)
>>> _ = sparse_(w, sparsity=0.9, std=0.01)
>>> int((w == 0).sum().item()) # ceil(0.9 * 256) = 231 zeros in each column
59136