random_seed_generator | R Documentation |
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.
random_seed_generator(seed = NULL, name = NULL, ...)
seed |
Initial seed for the random number generator |
name |
String, name for the object |
... |
For forward/backward compatability. |
A SeedGenerator
instance, which can be passed as the seed =
argument to other random tensor generators.
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)
## tf.Tensor([0. 5. 5. 0. 0. 0. 0. 0. 0. 0.], shape=(10), dtype=float32)
Other random:
random_beta()
random_binomial()
random_categorical()
random_dropout()
random_gamma()
random_integer()
random_normal()
random_shuffle()
random_truncated_normal()
random_uniform()
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.