
Seed torch's global RNG, returning its previous state to restore later
Source:R/utils.R
set_torch_seed.Rdtorch::torch_manual_seed() reseeds torch's one global generator, and
there is no way to ask it for an independent stream instead. A caller that
reseeds it and never restores the previous state leaves every later
unseeded torch call in the same session – another fit, another
de_sample() call, a c2st() call with no seed of its own –
drawing from wherever the seeded call left the generator, rather than from a
fresh one (GitHub #275, the torch analogue of #272).
Value
The RNG state captured before reseeding, suitable for
torch::torch_set_rng_state().
Details
Pair this with on.exit(torch::torch_set_rng_state(old), add = TRUE) right
after calling it. That is the same save-then-restore shape
with_fixed_seed() uses for R's own RNG, but split into a value-returning
helper plus the caller's own on.exit() rather than an expression-wrapping
one: both call sites need the seed live for the rest of a long-running
function – a network's random initialization and the epochs of shuffled
batches that follow it, not just one expression's value – so the restore
has to fire when that function returns, not when this helper does.
Requires torch (call require_torch() first; both call sites are already
past that check when they reach this).