GenericMixture and the Component protocol¶
GenericMixture(components=[...]) is a mixture of user-supplied components. A component is either an object that
implements the Component protocol (log_probability / summarize / from_summaries, the pomegranate 1.x API, so
batch_size works for free) or a torch.nn.Module with learnable parameters and a log_prob(X) method, whose M-step is
a generalised EM step (L-BFGS on the expected complete-data log-likelihood, accepted only on improvement).
import torch
from tgmm import GenericMixture
class Exponential: # a protocol component: three methods, no base class
def __init__(self, rate): self.rate, self.sw, self.sx = torch.tensor(rate), 0.0, 0.0
def log_probability(self, X): return torch.log(self.rate) - self.rate * X[:, 0]
def summarize(self, X, w): self.sw += w.sum(); self.sx += (w * X[:, 0]).sum()
def from_summaries(self):
self.rate = self.sw / self.sx; self.sw = self.sx = 0.0
def sample(self, n): return torch.distributions.Exponential(self.rate).sample((n, 1))
def n_parameters(self): return 1
mixture = GenericMixture([Exponential(0.5), Exponential(4.0)]).fit(X)
mixture.components_[0].rate, mixture.weights_
See the design
note
for the contract, the decisions (no data-driven initialisation, n_init=1, which algorithms are supported) and the
limits (no SAEM or online EM: they blend additive statistics, which opaque summarize state is not).
GenericMixture
¶
Bases: BaseMixture
Mixture :math:p(x)=\sum_k \pi_k f_k(x) of user-supplied components.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
components
|
sequence of components
|
The
The constructor keeps deep copies; every |
required |
init_weights
|
uniform
|
Initial mixing weights. (default: 'uniform') |
'uniform'
|
n_init
|
int
|
Must be 1 (restarts would all begin from the same components). (default: 1) |
1
|
algorithm
|
(em, cem, sem)
|
|
'em'
|
noise
|
see `BaseMixture`
|
|
False
|
noise_volume
|
see `BaseMixture`
|
|
False
|
noise_prior
|
see `BaseMixture`
|
|
False
|
noise_init
|
see `BaseMixture`
|
|
False
|
frozen_components
|
sequence of int
|
Components that are never updated (their mixing weights still are). A component whose
|
()
|
max_iter
|
int
|
|
1000
|
tol
|
int
|
|
1000
|
random_state
|
int
|
|
1000
|
warm_start
|
int
|
|
1000
|
batch_size
|
int
|
|
1000
|
temperature
|
int
|
|
1000
|
convergence
|
int
|
|
1000
|
sem_burn_in
|
int
|
|
1000
|
components_per_class
|
as in |
1
|
|
unlabeled_weight
|
as in |
1
|
|
label_confidence
|
as in |
1
|
|
verbose
|
as in |
1
|
|
verbose_interval
|
as in |
1
|
|
device
|
as in |
1
|
Attributes:
| Name | Type | Description |
|---|---|---|
components_ |
list
|
The fitted components (for autograd components: adapters, with the module in |
weights_ |
(Tensor, shape(n_components))
|
|
Source code in tgmm/mixtures/generic.py
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 | |
Methods:¶
save(filepath)
¶
load(filepath, device=None)
classmethod
¶
Load a model saved with save. The component classes must be importable.
Source code in tgmm/mixtures/generic.py
protocol
¶
The component protocol behind tgmm.GenericMixture.
A mixture component is, from the point of view of EM, three operations: evaluate its log-density,
summarise a weighted chunk of data, and update its parameters from the summaries. This module
defines that interface as a typing.Protocol so that any object -- yours, or one adapted from another
library -- can be a component without inheriting from anything in tgmm.
The three-method shape (log_probability / summarize / from_summaries) follows the API of
pomegranate 1.x (https://pomegranate.readthedocs.io, MIT licence), whose distributions are designed
exactly for out-of-core EM; the design is credited to that project. tgmm does not import pomegranate.
See docs/superpowers/specs/2026-10-15-v05-component-protocol-design.md.
Classes¶
Component
¶
Bases: Protocol
What GenericMixture needs from a mixture component.
Required methods
log_probability(X) -> Tensor of shape (n,)
:math:\log f_k(x_i) for every row of X.
summarize(X, sample_weight) -> None
Accumulate the weighted sufficient statistics of one chunk: sample_weight has shape (n,)
and holds the (row-weighted) responsibility of this component for each row. It is called once
per row chunk (batch_size splits an M-step into several chunks), so it must add to what
it already holds.
from_summaries() -> None
Update the parameters from everything summarised since the last call, then reset the
accumulators. A component that received (almost) no weight should keep its parameters.
Optional members (looked up with getattr; not part of the runtime check)
sample(n) -> Tensor of shape (n, d)
Needed by GenericMixture.sample.
n_parameters() -> int
Number of free scalar parameters, used by bic/aic/icl. Without it, the number of scalar
entries of the tensors held in the object's public attributes is used (see
component_n_parameters).
frozen : bool
If truthy, the component's parameters are never updated.
reseed(x) -> None
Called during SEM with the worst-fit data point when the component's sampled count drops
below one.
Functions:¶
component_is_frozen(component)
¶
Whether component declares itself frozen (component.frozen is truthy).
component_n_parameters(component)
¶
Number of free parameters of a component: component.n_parameters() if it exists, else the
number of scalar entries of the tensors held in its public attributes (names that do not start
with _; for an nn.Module attribute, its parameters that require grad). Implement
n_parameters() whenever the object stores anything else as a public tensor.