TensorPlay
latest (dev)
Copy
View Markdown

Latest development documentation · Updated 2026-10-08

MixtureSameFamily

class tensorplay.distributions.MixtureSameFamily(mixture_distribution: Categorical, component_distribution: Distribution, validate_args: bool | None = None)[source]

The MixtureSameFamily distribution implements a (batch of) mixture distribution where all components are from different parameterizations of the same distribution type. It is parameterized by a Categorical “selecting distribution” (over k components) and a component distribution, i.e., a Distribution with a rightmost batch shape (equal to [k]) which indexes each (batch of) component.

Examples:

>>> # xdoctest: +SKIP("undefined vars")
>>> # Construct Gaussian Mixture Model in 1D consisting of 5 equally
>>> # weighted normal distributions
>>> mix = D.Categorical(tensorplay.ones(5,))
>>> comp = D.Normal(tensorplay.randn(5,), tensorplay.rand(5,))
>>> gmm = MixtureSameFamily(mix, comp)

>>> # Construct Gaussian Mixture Model in 2D consisting of 5 equally
>>> # weighted bivariate normal distributions
>>> mix = D.Categorical(tensorplay.ones(5,))
>>> comp = D.Independent(D.Normal(
...          tensorplay.randn(5,2), tensorplay.rand(5,2)), 1)
>>> gmm = MixtureSameFamily(mix, comp)

>>> # Construct a batch of 3 Gaussian Mixture Models in 2D each
>>> # consisting of 5 random weighted bivariate normal distributions
>>> mix = D.Categorical(tensorplay.rand(3,5))
>>> comp = D.Independent(D.Normal(
...         tensorplay.randn(3,5,2), tensorplay.rand(3,5,2)), 1)
>>> gmm = MixtureSameFamily(mix, comp)
Parameters:
  • mixture_distribution – tensorplay.distributions.Categorical-like instance. Manages the probability of selecting components. The number of categories must match the rightmost batch dimension of the component_distribution. Must have either scalar batch_shape or batch_shape matching component_distribution.batch_shape[:-1]

  • component_distribution – tensorplay.distributions.Distribution-like instance. Right-most batch dimension indexes component.

property batch_shape: Size

Returns the shape over which parameters are batched.

entropy() → Tensor

Returns entropy of distribution, batched over batch_shape.

Returns:

Tensor of shape batch_shape.

enumerate_support(expand: bool = True) → Tensor

Returns tensor containing all values supported by a discrete distribution. The result will enumerate over dimension 0, so the shape of the result will be (cardinality,) + batch_shape + event_shape (where event_shape = () for univariate distributions).

Note that this enumerates over all batched tensors in lock-step [[0, 0], [1, 1], …]. With expand=False, enumeration happens along dim 0, but with the remaining batch dimensions being singleton dimensions, [[0], [1], ...

To iterate over the full Cartesian product use itertools.product(m.enumerate_support()).

Parameters:

expand (bool) – whether to expand the support over the batch dims to match the distribution’s batch_shape.

Returns:

Tensor iterating over dimension 0.

property event_shape: Size

Returns the shape of a single sample (without batching).

icdf(value: Tensor) → Tensor

Returns the inverse cumulative density/mass function evaluated at value.

Parameters:

value (Tensor)

property mode: Tensor

Returns the mode of the distribution.

perplexity() → Tensor

Returns perplexity of distribution, batched over batch_shape.

Returns:

Tensor of shape batch_shape.

rsample(sample_shape: Size | Sequence[int] = ()) → Tensor

Generates a sample_shape shaped reparameterized sample or sample_shape shaped batch of reparameterized samples if the distribution parameters are batched.

sample_n(n: int) → Tensor

Generates n samples or n batches of samples if the distribution parameters are batched.

static set_default_validate_args(value: bool) → None

Sets whether validation is enabled or disabled.

The default behavior mimics Python’s assert statement: validation is on by default, but is disabled if Python is run in optimized mode (via python -O). Validation may be expensive, so you may want to disable it once a model is working.

Parameters:

value (bool) – Whether to enable validation.

property stddev: Tensor

Returns the standard deviation of the distribution.

On this page

Ask DeepWiki