Keras 3 API documentation / KerasNLP / Samplers / RandomSampler

RandomSampler

[source]

RandomSampler class

keras_nlp.samplers.RandomSampler(seed=None, **kwargs)

Random Sampler class.

This sampler implements random sampling. Briefly, random sampler randomly selects a token from the entire distribution of the tokens, with selection chance determined by the probability of each token.

Arguments

  • seed: int. The random seed. Defaults to None.

Call arguments

{{call_args}}

Examples

causal_lm = keras_nlp.models.GPT2CausalLM.from_preset("gpt2_base_en")

# Pass by name to compile.
causal_lm.compile(sampler="random")
causal_lm.generate(["Keras is a"])

# Pass by object to compile.
sampler = keras_nlp.samplers.RandomSampler(temperature=0.7)
causal_lm.compile(sampler=sampler)
causal_lm.generate(["Keras is a"])