Skip to content
fn

uniform

→Tensor
uniform_(tensor: Tensor, a: float = 0.0, b: float = 1.0, generator: _C_engine.Generator | None = None)

Implementing kernel

C++ engine symbols that back this Python API.

Initialise tensor in-place with samples from a uniform distribution.

Each entry is drawn independently from U(a,b)\mathcal{U}(a, b). The function mutates tensor in place and returns the same object so calls can be chained or used as a one-liner inside a model constructor.

Parameters

tensorTensor
Tensor to fill in place; any shape is accepted.
afloat= 0.0
Lower bound of the uniform interval. Default 0.0.
bfloat= 1.0
Upper bound of the uniform interval. Default 1.0.
generatorGenerator= None
PRNG for uniform draws.

Returns

Tensor

tensor after uniform fill.

Notes

The resulting distribution has mean and variance

E[W]=a+b2,Var(W)=(b−a)212.\mathbb{E}[W] = \frac{a + b}{2}, \qquad \mathrm{Var}(W) = \frac{(b - a)^2}{12}.

Higher-level schemes such as xavier_uniform_ and kaiming_uniform_ are thin wrappers over this function with bounds computed from the fan factors of tensor.

The draw is Tensor.uniform_'s under lucid.no_grad, as the reference's is, so the two cannot disagree: the same seed gives the same values, a == b moves the stream on as any other bounds do, and a complex tensor draws both parts from the interval.

Examples

>>> import lucid
>>> from lucid.nn.init import uniform_
>>> w = lucid.empty(4, 4)
>>> _ = uniform_(w, -0.1, 0.1)
>>> bool(((w >= -0.1) & (w <= 0.1)).all().item())
True