Skip to content

rng

Random Number Generator API

This module provides utility functions to set random seed and create random id generators.

Functions:

Functions

set_random_seed

set_random_seed(seed: int | None = None) -> int

Set random seed across libraries: TF, Numpy, Python

Parameters:

  • seed (int | None, default: None ) –

    Random seed state to use. Defaults to None.

Returns:

  • int ( int ) –

    Random seed

Source code in neuralspot_edge/utils/rng.py
def set_random_seed(seed: int | None = None) -> int:
    """Set random seed across libraries: TF, Numpy, Python

    Args:
        seed (int | None, optional): Random seed state to use. Defaults to None.

    Returns:
        int: Random seed
    """

    seed = seed or random.randint(0, 2**16)
    random.seed(seed)
    # keras will set all backends including numpy
    keras.utils.set_random_seed(seed)
    return seed

uniform_id_generator

uniform_id_generator(ids: Iterable[T], repeat: bool = True, shuffle: bool = True) -> Generator[T, None, None]

Simple generator that yields ids in a uniform manner.

Parameters:

  • ids (Iterable[T]) –

    List of ids.

  • repeat (bool, default: True ) –

    Whether to repeat generator. Defaults to True.

  • shuffle (bool, default: True ) –

    Whether to shuffle ids.. Defaults to True.

Note

If repeat is False, generator will stop after yielding all ids once. If shuffle, ids parameter will be modified in place.

Returns:

  • Generator[T, None, None]

    Generator[T, None, None]: Generator

Yields: T: Id

Source code in neuralspot_edge/utils/rng.py
def uniform_id_generator(
    ids: Iterable[T],
    repeat: bool = True,
    shuffle: bool = True,
) -> Generator[T, None, None]:
    """Simple generator that yields ids in a uniform manner.

    Args:
        ids (Iterable[T]): List of ids.
        repeat (bool, optional): Whether to repeat generator. Defaults to True.
        shuffle (bool, optional): Whether to shuffle ids.. Defaults to True.

    Note:
        If repeat is False, generator will stop after yielding all ids once.
        If shuffle, ids parameter will be modified in place.

    Returns:
        Generator[T, None, None]: Generator
    Yields:
        T: Id
    """
    while True:
        if shuffle:
            random.shuffle(ids)
        yield from ids
        if not repeat:
            break

random_id_generator

random_id_generator(ids: Iterable[T], weights: list[int] | None = None) -> Generator[T, None, None]

Simple generator that yields ids in a random manner.

Parameters:

  • ids (Iterable[T]) –

    List of ids

  • weights (list[int], default: None ) –

    Weights for each id. Defaults to None.

Returns:

  • Generator[T, None, None]

    Generator[T, None, None]: Generator

Yields:

  • T ( T ) –

    Id

Source code in neuralspot_edge/utils/rng.py
def random_id_generator(
    ids: Iterable[T],
    weights: list[int] | None = None,
) -> Generator[T, None, None]:
    """Simple generator that yields ids in a random manner.

    Args:
        ids (Iterable[T]): List of ids
        weights (list[int], optional): Weights for each id. Defaults to None.

    Returns:
        Generator[T, None, None]: Generator

    Yields:
        T: Id
    """
    while True:
        yield random.choice(ids)