# GeneratorMixture

### *class* deepinv.physics.generator.GeneratorMixture(generators, probs, use_batch_sampling=True, device='cpu', rng=None, verbose=False)

Bases: [`PhysicsGenerator`](https://deepinv.org/api/stubs/deepinv.physics.generator.PhysicsGenerator.html.md#deepinv.physics.generator.PhysicsGenerator)

Base class for mixing multiple [`physics generators`](https://deepinv.org/api/stubs/deepinv.physics.generator.PhysicsGenerator.html.md#deepinv.physics.generator.PhysicsGenerator).

The mixture randomly selects a subset of batch elements
to be generated by each generator according to the probabilities given in the constructor.

* **Parameters:**
  * **generators** ([*list*](https://docs.python.org/3.12/library/stdtypes.html#list) *[*[*PhysicsGenerator*](https://deepinv.org/api/stubs/deepinv.physics.generator.PhysicsGenerator.html.md#deepinv.physics.generator.PhysicsGenerator) *]*) – the generators instantiated from [`deepinv.physics.generator.PhysicsGenerator`](https://deepinv.org/api/stubs/deepinv.physics.generator.PhysicsGenerator.html.md#deepinv.physics.generator.PhysicsGenerator).
  * **probs** ([*list*](https://docs.python.org/3.12/library/stdtypes.html#list) *[*[*float*](https://docs.python.org/3.12/library/functions.html#float) *]*) – the probability of each generator to be used at each step
  * **device** ([*str*](https://docs.python.org/3.12/library/stdtypes.html#str)) – device on which the generator is located, defaults to “cpu”
  * **rng** ([*torch.Generator*](https://docs.pytorch.org/docs/stable/generated/torch.Generator.html#torch.Generator)) – a pseudorandom random number generator for the parameter generation. If `None`, a generator will be created on the specified device with a random seed.
  * **verbose** ([*bool*](https://docs.python.org/3.12/library/functions.html#bool)) – whether to print warnings about the batch-compatibility of the generators, defaults to False.
* **Param:**
  bool use_batch_sampling: whether to sample a different generator for each element in the batch. This is only possible if all generators in the mixture produce parameters with the same keys and shapes. If not, a single generator will be sampled per batch. Defaults to `True`.

<hr />

* **Examples:**
  Mixing two types of blur
  ```pycon
  >>> from deepinv.physics.generator import MotionBlurGenerator, DiffractionBlurGenerator
  >>> from deepinv.physics.generator import GeneratorMixture
  >>> _ = torch.manual_seed(0)
  >>> g1 = MotionBlurGenerator(psf_size = (3, 3), num_channels = 1)
  >>> g2 = DiffractionBlurGenerator(psf_size = (3, 3), num_channels = 1)
  >>> generator = GeneratorMixture([g1, g2], [0.5, 0.5])
  >>> params_dict = generator.step(batch_size=1)
  >>> print(params_dict.keys())
  dict_keys(['filter'])
  ```

#### step(batch_size=1, seed=None, \*\*kwargs)

Returns a new set of physics’ parameters,
according to the probabilities given in the constructor.

* **Parameters:**
  * **batch_size** ([*int*](https://docs.python.org/3.12/library/functions.html#int)) – the number of samples to generate.
  * **seed** ([*int*](https://docs.python.org/3.12/library/functions.html#int)) – the seed for the random number generator.
* **Returns:**
  A dictionary with the new parameters, ie `{param_name: param_value}`.

<a id="sphx-glr-backref-deepinv-physics-generator-generatormixture"></a>

## Examples using `GeneratorMixture`:

<div class="sphx-glr-thumbnails">
<!-- thumbnail-parent-div-open --><div class="sphx-glr-thumbcontainer" tooltip="This example provides a tour of 2D blur operators in DeepInverse. In particular, we show how to use DiffractionBlurs (Fresnel diffraction), motion blurs and space varying blurs.">![](auto_examples/physics/images/thumb/sphx_glr_demo_blur_tour_thumb.png)

[Tour of blur operators](https://deepinv.org/auto_examples/physics/demo_blur_tour.html.md)

  <div class="sphx-glr-thumbnail-title">Tour of blur operators</div>
</div>
<!-- thumbnail-parent-div-close --></div>
