fn

uniform_

Tensor
uniform_(tensor: Tensor, a: float = 0.0, b: float = 1.0)
source

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.

Returns

Tensor

tensor (mutated) for chaining.

Notes

The resulting distribution has mean and variance

E[W]=a+b2,Var(W)=(ba)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.

Examples

>>> import lucid
>>> from lucid.nn.init import uniform_
>>> w = lucid.empty(4, 4)
>>> uniform_(w, -0.1, 0.1)