fn

rand_like

Tensor
rand_like(t: Tensor, dtype: DTypeLike = None, device: DeviceLike = None, requires_grad: _bool = False)
source

Return a U[0,1)U[0,1) random tensor with the same shape, dtype, and device as t.

Equivalent to lucid.rand(t.shape, dtype=t.dtype, device=t.device), but infers metadata from an existing tensor so the caller need not repeat shape and type information.

Each element is drawn independently from

XiU[0,1)X_i \sim U[0, 1)

Parameters

tTensor
Reference tensor. Its shape, dtype, and device are inherited by the output unless overridden.
dtypelucid.dtype
Override the data type. Defaults to t.dtype.
devicestr or lucid.device
Override the device. Defaults to t.device.
requires_gradbool
Enable autograd tracking. Default: False.

Returns

Tensor

Uniform random tensor shaped like t.

Notes

The returned tensor mirrors t's shape, dtype, and device exactly unless overridden via the corresponding keyword arguments. Sampling follows the same dtype-default rule as lucid.rand: only floating-point dtypes are supported. Calling this on an integer tensor without overriding dtype raises — use lucid.randint_like for integer noise.

Examples

>>> import lucid
>>> x = lucid.zeros(3, 4, device="metal")
>>> noise = lucid.rand_like(x)
>>> noise.shape, noise.device
((3, 4), lucid.device('metal'))