Views
No views yet
from_preset()
constructor.pip install -U -q keras-Hub
pip install -U -q keras| Preset name | Parameters | Description |
|---|---|---|
| roberta_base_en | 124.05M | 12-layer RoBERTa model where case is maintained.Trained on English Wikipedia, BooksCorpus, CommonCraw, and OpenWebText. |
| roberta_large_en | 354.31M | 24-layer RoBERTa model where case is maintained.Trained on English Wikipedia, BooksCorpus, CommonCraw, and OpenWebText. |
max_sequence_length default value. This determines the variable
shape for positional embeddings.1import keras
2import keras_hub
3import numpy as np1features = ["The quick brown fox jumped.", "I forgot my homework."]
2labels = [0, 3]
3
4# Pretrained classifier.
5classifier = keras_hub.models.RobertaClassifier.from_preset(
6 "roberta_base_en",
7 num_classes=4,
8)
9classifier.fit(x=features, y=labels, batch_size=2)
10classifier.predict(x=features, batch_size=2)
11
12# Re-compile (e.g., with a new learning rate).
13classifier.compile(
14 loss=keras.losses.SparseCategoricalCrossentropy(from_logits=True),
15 optimizer=keras.optimizers.Adam(5e-5),
16 jit_compile=True,
17)
18# Access backbone programmatically (e.g., to change `trainable`).
19classifier.backbone.trainable = False
20# Fit again.
21classifier.fit(x=features, y=labels, batch_size=2)1features = {
2 "token_ids": np.ones(shape=(2, 12), dtype="int32"),
3 "padding_mask": np.array([[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0]] * 2),
4}
5labels = [0, 3]
6
7# Pretrained classifier without preprocessing.
8classifier = keras_hub.models.RobertaClassifier.from_preset(
9 "roberta_base_en",
10 num_classes=4,
11 preprocessor=None,
12)
13classifier.fit(x=features, y=labels, batch_size=2)1import keras
2import keras_hub
3import numpy as np1features = ["The quick brown fox jumped.", "I forgot my homework."]
2labels = [0, 3]
3
4# Pretrained classifier.
5classifier = keras_hub.models.RobertaClassifier.from_preset(
6 "hf://keras/roberta_base_en",
7 num_classes=4,
8)
9classifier.fit(x=features, y=labels, batch_size=2)
10classifier.predict(x=features, batch_size=2)
11
12# Re-compile (e.g., with a new learning rate).
13classifier.compile(
14 loss=keras.losses.SparseCategoricalCrossentropy(from_logits=True),
15 optimizer=keras.optimizers.Adam(5e-5),
16 jit_compile=True,
17)
18# Access backbone programmatically (e.g., to change `trainable`).
19classifier.backbone.trainable = False
20# Fit again.
21classifier.fit(x=features, y=labels, batch_size=2)1features = {
2 "token_ids": np.ones(shape=(2, 12), dtype="int32"),
3 "padding_mask": np.array([[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0]] * 2),
4}
5labels = [0, 3]
6
7# Pretrained classifier without preprocessing.
8classifier = keras_hub.models.RobertaClassifier.from_preset(
9 "hf://keras/roberta_base_en",
10 num_classes=4,
11 preprocessor=None,
12)
13classifier.fit(x=features, y=labels, batch_size=2)