espnet2.iterators.multiple_iter_factory.MultipleIterFactory
espnet2.iterators.multiple_iter_factory.MultipleIterFactory
class espnet2.iterators.multiple_iter_factory.MultipleIterFactory(build_funcs: Collection[Callable[[], AbsIterFactory]], seed: int = 0, shuffle: bool = False)
Bases: AbsIterFactory
A factory for creating multiple iterator instances based on provided builder
functions. This class allows for the optional shuffling of the iterator construction order and can be seeded for reproducibility.
build_funcs
A list of callable functions that return instances of AbsIterFactory.
- Type: list
seed
An integer seed for random number generation to control shuffling.
- Type: int
shuffle
A flag indicating whether to shuffle the order of iterator factories.
Type: bool
Parameters:
- build_funcs (Collection *[*Callable [ [ ] , AbsIterFactory ] ]) – A collection of functions that will be called to create iterator factories.
- seed (int , optional) – The seed for the random number generator. Defaults to 0.
- shuffle (bool , optional) – Whether to shuffle the order of iterators. Defaults to False.
Yields:Iterator – An iterator that yields items from the constructed iterators.
####### Examples
>>> def build_func_a():
... return SomeIterFactoryA()
...
>>> def build_func_b():
... return SomeIterFactoryB()
...
>>> factory = MultipleIterFactory([build_func_a, build_func_b], seed=42,
... shuffle=True)
>>> for item in factory.build_iter(epoch=1):
... print(item)
NOTE
The order of the iterators can change between different epochs if shuffling is enabled.
build_iter(epoch: int, shuffle: bool | None = None) → Iterator
Generates an iterator by building iterators from a collection of provided
functions.
This method allows for the creation of multiple iterators, which can be shuffled based on the epoch and a specified seed. The iterator is built by calling each of the provided build functions in sequence.
- Parameters:
- epoch (int) – The current epoch number, used to seed the random number generator for shuffling.
- shuffle (bool , optional) – If True, the build functions will be shuffled before building iterators. If None, defaults to the instance’s shuffle attribute.
- Yields:Iterator – An iterator that yields elements from the built iterators.
####### Examples
>>> factory = MultipleIterFactory([build_func1, build_func2], seed=42)
>>> for item in factory.build_iter(epoch=1, shuffle=True):
... print(item)
NOTE
This method is intended for use within the MultipleIterFactory context and assumes that the build functions return instances of AbsIterFactory.
- Raises:AssertionError – If the object returned by a build function is not an instance of AbsIterFactory.