Rate this Page
★ ★ ★ ★ ★

torch.func._random.uniform#

torch.func._random.uniform(key, *shape, low=0.0, high=1.0, dtype=None)[source]#

Generate uniformly distributed random values from a PRNG key.

Produces a tensor of the given shape filled with values drawn uniformly from the interval [low, high). The output is fully determined by the key, so calling with the same key always returns the same result. The output is placed on the same device as key.

Supports batched keys: if key has shape (*batch, K), the leading dimensions of shape must be broadcastable with *batch and each key independently generates its slice of the output.

Gradients flow to tensor low / high as for a reparameterized sample (like torch.distributions.Distribution.rsample()).

Parameters:
  • key (Tensor) – A PRNG key returned by key(), split(), or fold_in().

  • *shape (int) – The desired output shape.

  • low (float or Tensor) – Lower bound (inclusive) of the uniform distribution. A floating-point tensor must broadcast to shape and gives a bound per element. Default: 0.0.

  • high (float or Tensor) – Upper bound (exclusive) of the uniform distribution. A floating-point tensor must broadcast to shape and gives a bound per element. Default: 1.0.

  • dtype (torch.dtype, optional) – The desired dtype. Default: torch.float32.

Returns:

A tensor of the given shape filled with uniform random values.

Return type:

Tensor

Example:

>>> key = torch.func._random.key(42, device="cuda")  
>>> torch.func._random.uniform(key, (1000,))