LION.utils.rng¶
Random numbers that can be reproduced.
seed_from_parts(*parts): a seed from strings (a stable hash).seeded(seed, *parts, device=...): run code with torch’s random numbers seeded fromseedandparts, e.g. the noise of one data sample, so that sample is the same in every epoch, run, order and DataLoader worker. The global random state is restored afterwards.seed_all(seed): seed torch, numpy and Python’srandom, as the solvers do when training starts;random_state()/set_random_state()keep and restore their state (checkpoints).
One seed gives the same numbers on the same device type and torch version. torch’s CPU and CUDA generators are different algorithms, so CPU and GPU give different (statistically equivalent) numbers.
Functions
The state of every generator |
|
|
Seed every random generator training draws from: torch's (CPU and all CUDA devices), numpy's and Python's |
|
A 32-bit seed from |
|
Run the code in the |
|
Restore the generators from |
- LION.utils.rng.random_state()[source]¶
The state of every generator
seed_allseeds, as tensors, lists and numbers (so a checkpoint holding it loads withweights_only=True).
- LION.utils.rng.seed_all(seed)[source]¶
Seed every random generator training draws from: torch’s (CPU and all CUDA devices), numpy’s and Python’s
random. The solvers call it when training starts (theirseed); call it at the top of a script to also make the model’s initial weights repeatable.
- LION.utils.rng.seed_from_parts(*parts)[source]¶
A 32-bit seed from
parts(anything, as strings), the same on every machine and run: a stable hash (BLAKE2b), not Python’shash.
- LION.utils.rng.seeded(seed, *parts, device=None)[source]¶
Run the code in the
withblock with torch’s random numbers seeded fromseedandparts(seed_from_parts), on the CPU and, ifdeviceis a CUDA device, on it. The random state of those devices is restored afterwards, so the rest of the program draws the numbers it would have drawn anyway.seed=Nonedoes nothing: the code draws from the global random state.- with seeded(0, “noise”, patient_id, slice_index, device=sinogram.device):
noisy = sinogram_add_noise(sinogram)