Views
No views yet
pip install -U -q keras-hub
pip install -U -q keras| Preset name | Parameters | Description |
|---|---|---|
gpt2_base_en | 124.44M | 12-layer GPT-2 model where case is maintained. Trained on WebText. |
gpt2_medium_en | 354.82M | 24-layer GPT-2 model where case is maintained. Trained on WebText. |
gpt2_large_en | 774.03M | 36-layer GPT-2 model where case is maintained. Trained on WebText. |
gpt2_extra_large_en | 1.56B | 48-layer GPT-2 model where case is maintained. Trained on WebText. |
gpt2_base_en_cnn_dailymail | 124.44M | 12-layer GPT-2 model where case is maintained. Finetuned on the CNN/DailyMail summarization dataset. |
prompt = "Keras is a "1import keras
2import keras_hub
3import numpy as npgenerate() to do text generation.1gpt2_lm = keras_hub.models.GPT2CausalLM.from_preset("gpt2_base_en_cnn_dailymail")
2gpt2_lm.generate("I want to say", max_length=30)
3
4# Generate with batched prompts.
5gpt2_lm.generate(["This is a", "Where are you"], max_length=30)generate() function with a custom sampler.1gpt2_lm = keras_hub.models.GPT2CausalLM.from_preset("gpt2_base_en_cnn_dailymail")
2gpt2_lm.compile(sampler="greedy")
3gpt2_lm.generate("I want to say", max_length=30)
4
5gpt2_lm.compile(sampler=keras_hub.samplers.BeamSampler(num_beams=2))
6gpt2_lm.generate("I want to say", max_length=30)generate() without preprocessing.1# Prompt the model with `5338, 318` (the token ids for `"Who is"`).
2# Use `"padding_mask"` to indicate values that should not be overridden.
3prompt = {
4 "token_ids": np.array([[5338, 318, 0, 0, 0]] * 2),
5 "padding_mask": np.array([[1, 1, 0, 0, 0]] * 2),
6}
7
8gpt2_lm = keras_hub.models.GPT2CausalLM.from_preset(
9 "gpt2_base_en_cnn_dailymail",
10 preprocessor=None,
11)
12gpt2_lm.generate(prompt)fit() on a single batch.1features = ["The quick brown fox jumped.", "I forgot my homework."]
2gpt2_lm = keras_hub.models.GPT2CausalLM.from_preset("gpt2_base_en_cnn_dailymail")
3gpt2_lm.fit(x=features, batch_size=2)fit() without preprocessing.1x = {
2 "token_ids": np.array([[50256, 1, 2, 3, 4]] * 2),
3 "padding_mask": np.array([[1, 1, 1, 1, 1]] * 2),
4}
5y = np.array([[1, 2, 3, 4, 50256]] * 2)
6sw = np.array([[1, 1, 1, 1, 1]] * 2)
7
8gpt2_lm = keras_hub.models.GPT2CausalLM.from_preset(
9 "gpt2_base_en_cnn_dailymail",
10 preprocessor=None,
11)
12gpt2_lm.fit(x=x, y=y, sample_weight=sw, batch_size=2)1import keras
2import keras_hub
3import numpy as npgenerate() to do text generation.1gpt2_lm = keras_hub.models.GPT2CausalLM.from_preset("hf://keras/gpt2_base_en_cnn_dailymail")
2gpt2_lm.generate("I want to say", max_length=30)
3
4# Generate with batched prompts.
5gpt2_lm.generate(["This is a", "Where are you"], max_length=30)generate() function with a custom sampler.1gpt2_lm = keras_hub.models.GPT2CausalLM.from_preset("hf://keras/gpt2_base_en_cnn_dailymail")
2gpt2_lm.compile(sampler="greedy")
3gpt2_lm.generate("I want to say", max_length=30)
4
5gpt2_lm.compile(sampler=keras_hub.samplers.BeamSampler(num_beams=2))
6gpt2_lm.generate("I want to say", max_length=30)generate() without preprocessing.1# Prompt the model with `5338, 318` (the token ids for `"Who is"`).
2# Use `"padding_mask"` to indicate values that should not be overridden.
3prompt = {
4 "token_ids": np.array([[5338, 318, 0, 0, 0]] * 2),
5 "padding_mask": np.array([[1, 1, 0, 0, 0]] * 2),
6}
7
8gpt2_lm = keras_hub.models.GPT2CausalLM.from_preset(
9 "hf://keras/gpt2_base_en_cnn_dailymail",
10 preprocessor=None,
11)
12gpt2_lm.generate(prompt)fit() on a single batch.1features = ["The quick brown fox jumped.", "I forgot my homework."]
2gpt2_lm = keras_hub.models.GPT2CausalLM.from_preset("hf://keras/gpt2_base_en_cnn_dailymail")
3gpt2_lm.fit(x=features, batch_size=2)fit() without preprocessing.1x = {
2 "token_ids": np.array([[50256, 1, 2, 3, 4]] * 2),
3 "padding_mask": np.array([[1, 1, 1, 1, 1]] * 2),
4}
5y = np.array([[1, 2, 3, 4, 50256]] * 2)
6sw = np.array([[1, 1, 1, 1, 1]] * 2)
7
8gpt2_lm = keras_hub.models.GPT2CausalLM.from_preset(
9 "hf://keras/gpt2_base_en_cnn_dailymail",
10 preprocessor=None,
11)
12gpt2_lm.fit(x=x, y=y, sample_weight=sw, batch_size=2)