Python module
random
Provides experimental random tensor generation utilities.
WARNING
This module contains experimental APIs that are subject to change or removal in future versions. Use with caution in production environments.
This module provides functions for generating random tensors with various distributions. All functions support specifying data type and device, with sensible defaults based on the target device.
You can generate random tensors using different distributions:
from max.experimental import random
from max.dtype import DType
from max.driver import CPU
tensor1 = random.uniform((2, 3), dtype=DType.float32, device=CPU())
tensor2 = random.uniform((4, 4), range=(0, 1), dtype=DType.float32, device=CPU())
gaussian()
max.experimental.random.gaussian(shape=(), mean=0.0, std=1.0, *, dtype=None, device=None)
Creates a tensor filled with random values from a Gaussian (normal) distribution.
WARNING
This is an experimental API that may change in future versions.
Generates a tensor with values sampled from a normal (Gaussian) distribution with the specified mean and standard deviation. This is commonly used for weight initialization using techniques like Xavier/Glorot or He initialization.
Create tensors with random values from a Gaussian distribution:
from max.experimental import random
from max.driver import CPU
from max.dtype import DType
# Standard normal distribution
tensor = random.gaussian((2, 3), dtype=DType.float32, device=CPU())
-
Parameters:
-
- shape (Iterable[int | str | Dim | integer[Any]]) – The shape of the output tensor. Defaults to scalar (empty tuple).
- mean (float) – The mean (center) of the Gaussian distribution. This determines
where the distribution is centered. Defaults to
0.0
. - std (float) – The standard deviation (spread) of the Gaussian distribution.
Must be positive. Larger values create more spread in the distribution.
Defaults to
1.0
. - dtype (DType | None) – The data type of the output tensor. If
None
, uses the default dtype for the specified device (float32 for CPU, bfloat16 for accelerators). Defaults toNone
. - device (Device | None) – The device where the tensor will be allocated. If
None
, uses the default device (accelerator if available, otherwise CPU). Defaults toNone
.
-
Returns:
-
A
tensor
with random values sampled from the Gaussian distribution. -
Raises:
-
ValueError – If std <= 0.
gaussian_like()
max.experimental.random.gaussian_like(like, mean=0.0, std=1.0)
Generates random values from a Gaussian (normal) distribution for tensors of a given type.
See max.graph.ops.random.gaussian()
for details.
-
Parameters:
-
- like (TensorType)
- mean (Value[TensorType] | TensorValue | Shape | Dim | HasTensorValue | int | float | integer[Any] | floating[Any] | ndarray[Any, dtype[number[Any]]])
- std (Value[TensorType] | TensorValue | Shape | Dim | HasTensorValue | int | float | integer[Any] | floating[Any] | ndarray[Any, dtype[number[Any]]])
-
Return type:
normal()
max.experimental.random.normal(shape=(), mean=0.0, std=1.0, *, dtype=None, device=None)
Alias for gaussian()
.
Creates a tensor with values from a normal (Gaussian) distribution.
normal_like()
max.experimental.random.normal_like(like, mean=0.0, std=1.0)
Alias for gaussian_like()
.
-
Parameters:
-
- like (TensorType)
- mean (Value[TensorType] | TensorValue | Shape | Dim | HasTensorValue | int | float | integer[Any] | floating[Any] | ndarray[Any, dtype[number[Any]]])
- std (Value[TensorType] | TensorValue | Shape | Dim | HasTensorValue | int | float | integer[Any] | floating[Any] | ndarray[Any, dtype[number[Any]]])
-
Return type:
uniform()
max.experimental.random.uniform(shape=(), range=(0, 1), *, dtype=None, device=None)
Creates a tensor filled with random values from a uniform distribution.
WARNING
This is an experimental API that may change in future versions.
Generates a tensor with values uniformly distributed between the specified minimum and maximum bounds. This is useful for initializing weights, generating random inputs, or creating noise.
Create tensors with uniform random values:
from max.experimental import random
from max.dtype import DType
from max.driver import CPU
# Generate 2x3 tensor with values between 0 and 1
tensor1 = random.uniform((2, 3), dtype=DType.float32, device=CPU())
tensor2 = random.uniform((4, 4), range=(0, 1), dtype=DType.float32, device=CPU())
-
Parameters:
-
- shape (Iterable[int | str | Dim | integer[Any]]) – The shape of the output tensor. Defaults to scalar (empty tuple).
- range (tuple[float, float]) – A tuple specifying the (min, max) bounds of the uniform
distribution. The minimum value is inclusive, the maximum value
is exclusive. Defaults to
(0, 1)
. - dtype (DType | None) – The data type of the output tensor. If
None
, uses the default dtype for the specified device (float32 for CPU, bfloat16 for accelerators). Defaults toNone
. - device (Device | None) – The device where the tensor will be allocated. If
None
, uses the default device (accelerator if available, otherwise CPU). Defaults toNone
.
-
Returns:
-
A
tensor
with random values sampled from the uniform distribution. -
Raises:
-
ValueError – If the range tuple does not contain exactly two values or if min >= max.
uniform_like()
max.experimental.random.uniform_like(like, range=(0, 1))
Generates random values from a uniform distribution for tensors of a given type.
See max.graph.ops.random.uniform()
for details.
-
Parameters:
-
- like (TensorType)
- range (tuple[Value[TensorType] | TensorValue | Shape | Dim | HasTensorValue | int | float | integer[Any] | floating[Any] | ndarray[Any, dtype[number[Any]]], Value[TensorType] | TensorValue | Shape | Dim | HasTensorValue | int | float | integer[Any] | floating[Any] | ndarray[Any, dtype[number[Any]]]])
-
Return type:
Was this page helpful?
Thank you! We'll create more content like this.
Thank you for helping us improve!