tf.keras.random.SeedGenerator  |  TensorFlow v2.16.1 (original) (raw)

Generates variable seeds upon each call to a RNG-using function.

tf.keras.random.SeedGenerator(
    seed=None, name=None, **kwargs
)

In Keras, all RNG-using methods (such as keras.random.normal()) are stateless, meaning that if you pass an integer seed to them (such as seed=42), they will return the same values at each call. In order to get different values at each call, you must use aSeedGenerator instead as the seed argument. The SeedGeneratorobject is stateful.

Example:

seed_gen = keras.random.SeedGenerator(seed=42)
values = keras.random.normal(shape=(2, 3), seed=seed_gen)
new_values = keras.random.normal(shape=(2, 3), seed=seed_gen)

Usage in a layer:

class Dropout(keras.Layer):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        self.seed_generator = keras.random.SeedGenerator(1337)

    def call(self, x, training=False):
        if training:
            return keras.random.dropout(
                x, rate=0.5, seed=self.seed_generator
            )
        return x

Methods

from_config

View source

@classmethod from_config( config )

get_config

View source

get_config()

next

View source

next(
    ordered=True
)