Seeding¶
gympn.seeding ¶
Reproducible seeding for gympn.
A single entry point, :func:seed_everything, seeds every stochastic source a
gympn run touches -- Python's random, NumPy, and PyTorch (CPU and CUDA), plus
the cuDNN / deterministic-algorithm switches -- so that a run is reproducible
given the same seed, code, and hardware.
Environment stochasticity (task arrivals, service delays) is produced by the
Petri-net behaviour functions through the global random / numpy modules,
so seeding those globals makes the environment reproducible as well, provided
execution is single-threaded within a run (the default for a training worker).
Usage¶
import gympn gympn.seed_everything(0) 0
or seed a specific problem / environment / run directly::
pn.set_seed(0) # GymProblem
env.set_seed(0) # AEPN_Env
pn.training_run(length=20, args_dict={"agent_seed": 0, ...}) # seeds internally
pn.testing_run(solver, length=20, seed=0) # reproducible evaluation
seed_everything ¶
seed_everything(seed: int, *, deterministic: bool = True, set_hash_seed: bool = True) -> int
Seed all stochastic sources used by gympn.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
seed
|
int
|
Seed applied to Python |
required |
deterministic
|
bool
|
If True, also request deterministic PyTorch kernels: cuDNN determinism
and |
True
|
set_hash_seed
|
bool
|
If True, set |
True
|
Returns:
| Type | Description |
|---|---|
int
|
The seed, for convenience and logging. |
seed_network_init ¶
seed_network_init(seed: int) -> int
Pin the torch RNG to a dedicated sub-stream for NETWORK INITIALISATION.
seed_everything fixes the stream, but not the position in it at which
the policy's weights are drawn: how many draws are consumed beforehand
depends on the run (env construction, token id generation) and on the
method (lva builds a critic aux head, lrq3/lqi build extra Q
heads, rudder an LSTM). Two arms of the same experiment therefore start
from different initial policies, which silently turns a matched-seed
comparison into an unmatched one.
Measured before this existed (s1, seed 0, epoch-1 return under the initial
policy): ppo_clip/mc_q/lrq2/lrq all gave 8.40, while
lcv/rudder gave 7.95 and lva 8.55 -- and the same method
(lrq2, seed 0, same config) gave 8.40 in one run and 7.95 in another.
Calling this immediately before the networks' parameters are first
materialised makes the initial policy a deterministic function of seed
alone. The sub-stream is offset from the environment stream so the two
cannot alias.
Note that gympn's networks use lazy modules, so parameters are created at the first FORWARD pass, not at construction -- this must therefore be called at the start of training, not right after the networks are built.