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 askey.Supports batched keys: if
keyhas shape(*batch, K), the leading dimensions ofshapemust be broadcastable with*batchand each key independently generates its slice of the output.Gradients flow to tensor
low/highas for a reparameterized sample (liketorch.distributions.Distribution.rsample()).- Parameters:
key (Tensor) – A PRNG key returned by
key(),split(), orfold_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
shapeand 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
shapeand 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:
Example:
>>> key = torch.func._random.key(42, device="cuda") >>> torch.func._random.uniform(key, (1000,))