Random
coola.random ¶
Contain functionalities to manage randomness.
coola.random.BaseRandomManager ¶
Bases: ABC
Implement the base class to manage randomness.
Each child class must implement the methods:
- get_rng_state
.
- manual_seed
.
- set_rng_state
.
Example usage:
>>> from coola.random import TorchRandomManager
>>> manager = TorchRandomManager()
>>> manager.manual_seed(42)
coola.random.BaseRandomManager.get_rng_state
abstractmethod
¶
get_rng_state() -> Any
Get the current RNG state.
Returns:
Type | Description |
---|---|
Any
|
The current RNG state. |
Example usage:
>>> from coola.random import TorchRandomManager
>>> manager = TorchRandomManager()
>>> state = manager.get_rng_state()
>>> state
{'torch': tensor([...], dtype=torch.uint8), 'torch.cuda': ...}
coola.random.BaseRandomManager.manual_seed
abstractmethod
¶
manual_seed(seed: int) -> None
Set the seed for generating random numbers.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
seed
|
int
|
The desired seed. |
required |
Example usage:
>>> from coola.random import TorchRandomManager
>>> manager = TorchRandomManager()
>>> manager.manual_seed(42)
coola.random.BaseRandomManager.set_rng_state
abstractmethod
¶
set_rng_state(state: Any) -> None
Set the RNG state.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
state
|
Any
|
The new RNG state. |
required |
Example usage:
>>> import torch
>>> from coola.random import TorchRandomManager
>>> manager = TorchRandomManager()
>>> state = manager.get_rng_state()
>>> manager.set_rng_state(state)
coola.random.NumpyRandomManager ¶
Bases: BaseRandomManager
Implement a random manager for the library numpy
.
The seed must be between 0
and 2**32 - 1
, so a modulo
operator to convert an integer to an integer between 0
and
2**32 - 1
.
Example usage:
>>> from coola.random import NumpyRandomManager
>>> manager = NumpyRandomManager()
>>> manager.manual_seed(42)
coola.random.RandomManager ¶
Bases: BaseRandomManager
Implement the default random manager.
By default, it is initialized with the following random managers:
'random'
:RandomRandomManager
'numpy'
:NumpyRandomManager
ifnumpy
is available'torch'
:TorchRandomManager
iftorch
is available
Example usage:
>>> from coola.random import RandomManager
>>> manager = RandomManager()
>>> manager.manual_seed(42)
coola.random.RandomManager.add_manager
classmethod
¶
add_manager(
name: str,
manager: BaseRandomManager,
exist_ok: bool = False,
) -> None
Add a random manager for a given name.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
name
|
str
|
The name for the random manager to add. |
required |
manager
|
BaseRandomManager
|
The random manager to add. |
required |
exist_ok
|
bool
|
If |
False
|
Raises:
Type | Description |
---|---|
RuntimeError
|
if a random manager is already registered
for the name and |
Example usage:
>>> from coola.random import BaseRandomManager, RandomManager
>>> class OtherRandomManager(BaseRandomManager):
... def get_rng_state(self) -> dict:
... return {}
... def manual_seed(self, seed: int) -> None:
... pass
... def set_rng_state(self, state: dict) -> dict:
... pass
...
>>> RandomManager.add_manager("other", OtherRandomManager())
>>> # To overwrite an existing random manager
>>> RandomManager.add_manager("other", OtherRandomManager(), exist_ok=True)
coola.random.RandomManager.has_manager
classmethod
¶
has_manager(name: str) -> bool
Indicate if a random manager is registered for the given name.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
name
|
str
|
The name to check. |
required |
Returns:
Type | Description |
---|---|
bool
|
|
Example usage:
>>> from coola.random import RandomManager
>>> RandomManager.has_manager("random")
True
>>> RandomManager.has_manager("missing")
False
coola.random.RandomRandomManager ¶
Bases: BaseRandomManager
Implement a random manager for the python standard library
random
.
Example usage:
>>> from coola.random import RandomRandomManager
>>> manager = RandomRandomManager()
>>> manager.manual_seed(42)
coola.random.TorchRandomManager ¶
Bases: BaseRandomManager
Implements a random number generator for the library torch
.
Example usage:
>>> from coola.random import TorchRandomManager
>>> manager = TorchRandomManager()
>>> manager.manual_seed(42)
coola.random.get_random_managers ¶
get_random_managers() -> dict[str, BaseRandomManager]
Get the random managers and their default name.
Returns:
Type | Description |
---|---|
dict[str, BaseRandomManager]
|
The mapping between the name and random managers. |
Example usage:
>>> from coola.random import get_random_managers
>>> get_random_managers()
{'random': RandomRandomManager()...}
coola.random.get_rng_state ¶
get_rng_state() -> dict[str, Any]
Get the current RNG state.
Returns:
Type | Description |
---|---|
dict[str, Any]
|
The current RNG state. |
Example usage:
>>> from coola.random import get_rng_state
>>> state = get_rng_state()
>>> state
{'random': ...}
coola.random.manual_seed ¶
manual_seed(seed: int) -> None
Set the seed for generating random numbers.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
seed
|
int
|
The desired random seed. |
required |
Example usage:
>>> import torch
>>> from coola.random import manual_seed
>>> manual_seed(42)
>>> torch.randn(3)
tensor([...])
>>> torch.randn(3)
tensor([...])
>>> manual_seed(42)
>>> torch.randn(3)
tensor([...])
coola.random.numpy_seed ¶
numpy_seed(seed: int) -> Generator[None]
Implement a context manager to manage the NumPy random seed and random number generator (RNG) state.
The context manager sets the specified random seed and restores the original RNG state afterward.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
seed
|
int
|
The random number generator seed to use while using this context manager. |
required |
Example usage:
>>> import numpy
>>> from coola.random import numpy_seed
>>> with numpy_seed(42):
... print(numpy.random.randn(2, 4))
...
[[...]]
>>> with numpy_seed(42):
... print(numpy.random.randn(2, 4))
...
[[...]]
coola.random.random_seed ¶
random_seed(seed: int) -> Generator[None]
Implement a context manager to manage the random seed and random number generator (RNG) state.
The context manager sets the specified random seed and restores the original RNG state afterward.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
seed
|
int
|
The random number generator seed to use while using this context manager. |
required |
Example usage:
>>> import numpy
>>> from coola.random import random_seed
>>> with random_seed(42):
... print(numpy.random.randn(2, 4))
...
[[...]]
>>> with random_seed(42):
... print(numpy.random.randn(2, 4))
...
[[...]]
coola.random.register_random_managers ¶
register_random_managers() -> None
Register randomness managers to RandomManager
.
Example usage:
>>> from coola.random import RandomManager
>>> from coola.random.default import register_random_managers
>>> register_random_managers()
>>> manager = RandomManager()
>>> manager
RandomManager(
(random): RandomRandomManager()
...
)
coola.random.set_rng_state ¶
set_rng_state(state: dict[str, Any]) -> None
Set the RNG state.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
state
|
dict[str, Any]
|
The new RNG state. |
required |
Example usage:
>>> import torch
>>> from coola.random import get_rng_state, set_rng_state
>>> st = get_rng_state()
>>> set_rng_state(st)
coola.random.torch_seed ¶
torch_seed(seed: int) -> Generator[None]
Implement a context manager to manage the PyTorch random seed and random number generator (RNG) state.
The context manager sets the specified random seed and restores the original RNG state afterward.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
seed
|
int
|
The random number generator seed to use while using this context manager. |
required |
Example usage:
>>> import torch
>>> from coola.random import torch_seed
>>> with torch_seed(42):
... print(torch.randn(2, 4))
...
tensor([[...]])
>>> with torch_seed(42):
... print(torch.randn(2, 4))
...
tensor([[...]])