Generates variable seeds upon each call to a function generating random numbers.
Source:R/random.R
random_seed_generator.Rd
In Keras, all random number generators (such as
random_normal()
) are stateless, meaning that if you pass an
integer seed to them (such as seed=42
), they will return the same
values for repeated calls. To get different values for each
call, a SeedGenerator
providing the state of the random generator
has to be used.
Note that all the random number generators have a default seed of NULL
,
which implies that an internal global SeedGenerator
is used.
If you need to decouple the RNG from the global state you can provide
a local StateGenerator
with either a deterministic or random initial
state.
Remark concerning the JAX backen: Note that the use of a local
StateGenerator
as seed argument is required for JIT compilation of
RNG with the JAX backend, because the use of global state is not
supported.
Value
A SeedGenerator
instance, which can be passed as the seed =
argument to other random tensor generators.
Examples
seed_gen <- random_seed_generator(seed = 42)
values <- random_normal(shape = c(2, 3), seed = seed_gen)
new_values <- random_normal(shape = c(2, 3), seed = seed_gen)
Usage in a layer:
layer_dropout2 <- new_layer_class(
"dropout2",
initialize = function(...) {
super$initialize(...)
self$seed_generator <- random_seed_generator(seed = 1337)
},
call = function(x, training = FALSE) {
if (training) {
return(random_dropout(x, rate = 0.5, seed = self$seed_generator))
}
return(x)
}
)
out <- layer_dropout(rate = 0.8)
out(op_ones(10), training = TRUE)
See also
Other random: random_beta()
random_binomial()
random_categorical()
random_dropout()
random_gamma()
random_integer()
random_normal()
random_shuffle()
random_truncated_normal()
random_uniform()