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

tf.keras.random.SeedGenerator

Stay organized with collections Save and categorize content based on your preferences.

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
)

Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates. Some content is licensed under the numpy license.

Last updated 2024-06-07 UTC.