Skip to main content

Python function

gaussian

gaussian()

max.experimental.random.gaussian(shape=(), mean=0.0, std=1.0, *, dtype=None, device=None)

source

Creates a tensor filled with random values from a Gaussian (normal) distribution.

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

tensor = random.gaussian((2, 3), dtype=DType.float32, device=CPU())

Parameters:

  • shape (Iterable[int | str | Dim | integer[Any] | TypedAttr]) – 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 to None.
  • device (Device | None) – The device where the tensor will be allocated. If None, uses the default device (accelerator if available, otherwise CPU). Defaults to None.

Returns:

A Tensor with random values sampled from the Gaussian distribution.

Raises:

ValueError – If std <= 0.