Views
No views yet
from_preset constructor.| Preset name | Parameters | Description |
|---|---|---|
| bart_base_en | 139.42M | 6-layer BART model where case is maintained. Trained on BookCorpus, English Wikipedia and CommonCrawl |
| bart_large_en | 406.29M | 12-layer BART model where case is maintained. Trained on BookCorpus, English Wikipedia and CommonCrawl. |
| bart_large_en_cnn | 406.29M | The bart_large_en backbone model fine-tuned on the CNN+DM summarization dataset. |
max_sequence_length uses the value from
sequence length. This determines the variable shape for positional
embeddings.1import keras
2import keras_hub
3import numpy as npgenerate() to do text generation, given an input context.1bart_lm = keras_hub.models.BartSeq2SeqLM.from_preset("bart_large_en_cnn")
2bart_lm.generate("The quick brown fox", max_length=30)
3
4# Generate with batched inputs.
5bart_lm.generate(["The quick brown fox", "The whale"], max_length=30)generate() function with a custom sampler.1bart_lm = keras_hub.models.BartSeq2SeqLM.from_preset("bart_large_en_cnn")
2bart_lm.compile(sampler="greedy")
3bart_lm.generate("The quick brown fox", max_length=30)generate() with encoder inputs and an incomplete decoder input (prompt).1bart_lm = keras_hub.models.BartSeq2SeqLM.from_preset("bart_large_en_cnn")
2bart_lm.generate(
3 {
4 "encoder_text": "The quick brown fox",
5 "decoder_text": "The fast"
6 }
7)generate() without preprocessing.1# Preprocessed inputs, with encoder inputs corresponding to
2# "The quick brown fox", and the decoder inputs to "The fast". Use
3# `"padding_mask"` to indicate values that should not be overridden.
4prompt = {
5 "encoder_token_ids": np.array([[0, 133, 2119, 6219, 23602, 2, 1, 1]]),
6 "encoder_padding_mask": np.array(
7 [[True, True, True, True, True, True, False, False]]
8 ),
9 "decoder_token_ids": np.array([[2, 0, 133, 1769, 2, 1, 1]]),
10 "decoder_padding_mask": np.array([[True, True, True, True, False, False]])
11}
12
13bart_lm = keras_hub.models.BartSeq2SeqLM.from_preset(
14 "bart_large_en_cnn",
15 preprocessor=None,
16)
17bart_lm.generate(prompt)fit() on a single batch.1features = {
2 "encoder_text": ["The quick brown fox jumped.", "I forgot my homework."],
3 "decoder_text": ["The fast hazel fox leapt.", "I forgot my assignment."]
4}
5bart_lm = keras_hub.models.BartSeq2SeqLM.from_preset("bart_large_en_cnn")
6bart_lm.fit(x=features, batch_size=2)fit() without preprocessing.1x = {
2 "encoder_token_ids": np.array([[0, 133, 2119, 2, 1]] * 2),
3 "encoder_padding_mask": np.array([[1, 1, 1, 1, 0]] * 2),
4 "decoder_token_ids": np.array([[2, 0, 133, 1769, 2]] * 2),
5 "decoder_padding_mask": np.array([[1, 1, 1, 1, 1]] * 2),
6}
7y = np.array([[0, 133, 1769, 2, 1]] * 2)
8sw = np.array([[1, 1, 1, 1, 0]] * 2)
9
10bart_lm = keras_hub.models.BartSeq2SeqLM.from_preset(
11 "bart_large_en_cnn",
12 preprocessor=None,
13)
14bart_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, given an input context.1bart_lm = keras_hub.models.BartSeq2SeqLM.from_preset("hf://keras/bart_large_en_cnn")
2bart_lm.generate("The quick brown fox", max_length=30)
3
4# Generate with batched inputs.
5bart_lm.generate(["The quick brown fox", "The whale"], max_length=30)generate() function with a custom sampler.1bart_lm = keras_hub.models.BartSeq2SeqLM.from_preset("hf://keras/bart_large_en_cnn")
2bart_lm.compile(sampler="greedy")
3bart_lm.generate("The quick brown fox", max_length=30)generate() with encoder inputs and an incomplete decoder input (prompt).1bart_lm = keras_hub.models.BartSeq2SeqLM.from_preset("hf://keras/bart_large_en_cnn")
2bart_lm.generate(
3 {
4 "encoder_text": "The quick brown fox",
5 "decoder_text": "The fast"
6 }
7)generate() without preprocessing.1# Preprocessed inputs, with encoder inputs corresponding to
2# "The quick brown fox", and the decoder inputs to "The fast". Use
3# `"padding_mask"` to indicate values that should not be overridden.
4prompt = {
5 "encoder_token_ids": np.array([[0, 133, 2119, 6219, 23602, 2, 1, 1]]),
6 "encoder_padding_mask": np.array(
7 [[True, True, True, True, True, True, False, False]]
8 ),
9 "decoder_token_ids": np.array([[2, 0, 133, 1769, 2, 1, 1]]),
10 "decoder_padding_mask": np.array([[True, True, True, True, False, False]])
11}
12
13bart_lm = keras_hub.models.BartSeq2SeqLM.from_preset(
14 "hf://keras/bart_large_en_cnn",
15 preprocessor=None,
16)
17bart_lm.generate(prompt)fit() on a single batch.1features = {
2 "encoder_text": ["The quick brown fox jumped.", "I forgot my homework."],
3 "decoder_text": ["The fast hazel fox leapt.", "I forgot my assignment."]
4}
5bart_lm = keras_hub.models.BartSeq2SeqLM.from_preset("hf://keras/bart_large_en_cnn")
6bart_lm.fit(x=features, batch_size=2)fit() without preprocessing.1x = {
2 "encoder_token_ids": np.array([[0, 133, 2119, 2, 1]] * 2),
3 "encoder_padding_mask": np.array([[1, 1, 1, 1, 0]] * 2),
4 "decoder_token_ids": np.array([[2, 0, 133, 1769, 2]] * 2),
5 "decoder_padding_mask": np.array([[1, 1, 1, 1, 1]] * 2),
6}
7y = np.array([[0, 133, 1769, 2, 1]] * 2)
8sw = np.array([[1, 1, 1, 1, 0]] * 2)
9
10bart_lm = keras_hub.models.BartSeq2SeqLM.from_preset(
11 "hf://keras/bart_large_en_cnn",
12 preprocessor=None,
13)
14bart_lm.fit(x=x, y=y, sample_weight=sw, batch_size=2)