Classifier classkeras_nlp.models.Classifier()
Base class for all classification tasks.
Classifier tasks wrap a keras_nlp.models.Backbone and
a keras_nlp.models.Preprocessor to create a model that can be used for
sequence classification. Classifier tasks take an additional
num_classes argument, controlling the number of predicted output classes.
To fine-tune with fit(), pass a dataset containing tuples of (x, y)
labels where x is a string and y is a integer from [0, num_classes).
All Classifier tasks include a from_preset() constructor which can be
used to load a pre-trained config and weights.
Example
# Load a BERT classifier with pre-trained weights.
classifier = keras_nlp.models.Classifier.from_preset(
"bert_base_en",
num_classes=2,
)
# Fine-tune on IMDb movie reviews (or any dataset).
imdb_train, imdb_test = tfds.load(
"imdb_reviews",
split=["train", "test"],
as_supervised=True,
batch_size=16,
)
classifier.fit(imdb_train, validation_data=imdb_test)
# Predict two new examples.
classifier.predict(["What an amazing movie!", "A total waste of my time."])
from_preset methodClassifier.from_preset(preset, load_weights=True, **kwargs)
Instantiate a keras_nlp.models.Task from a model preset.
A preset is a directory of configs, weights and other file assets used
to save and load a pre-trained model. The preset can be passed as a
one of:
'bert_base_en''kaggle://user/bert/keras/bert_base_en''hf://user/bert_base_en''./bert_base_en'For any Task subclass, you can run cls.presets.keys() to list all
built-in presets available on the class.
This constructor can be called in one of two ways. Either from a task
specific base class like keras_nlp.models.CausalLM.from_preset(), or
from a model class like keras_nlp.models.BertClassifier.from_preset().
If calling from the a base class, the subclass of the returning object
will be inferred from the config in the preset directory.
Arguments
True, the weights will be loaded into the
model architecture. If False, the weights will be randomly
initialized.Examples
# Load a Gemma generative task.
causal_lm = keras_nlp.models.CausalLM.from_preset(
"gemma_2b_en",
)
# Load a Bert classification task.
model = keras_nlp.models.Classifier.from_preset(
"bert_base_en",
num_classes=2,
)
compile methodClassifier.compile(optimizer="auto", loss="auto", metrics="auto", **kwargs)
Configures the Classifier task for training.
The Classifier task extends the default compilation signature of
keras.Model.compile with defaults for optimizer, loss, and
metrics. To override these defaults, pass any value
to these arguments during compilation.
Arguments
"auto", an optimizer name, or a keras.Optimizer
instance. Defaults to "auto", which uses the default optimizer
for the given model and task. See keras.Model.compile and
keras.optimizers for more info on possible optimizer values."auto", a loss name, or a keras.losses.Loss instance.
Defaults to "auto", where a
keras.losses.SparseCategoricalCrossentropy loss will be
applied for the classification task. See
keras.Model.compile and keras.losses for more info on
possible loss values."auto", or a list of metrics to be evaluated by
the model during training and testing. Defaults to "auto",
where a keras.metrics.SparseCategoricalAccuracy will be
applied to track the accuracy of the model during training.
See keras.Model.compile and keras.metrics for
more info on possible metrics values.keras.Model.compile for a full list of arguments
supported by the compile method.save_to_preset methodClassifier.save_to_preset(preset_dir)
Save task to a preset directory.
Arguments
preprocessor propertykeras_nlp.models.Classifier.preprocessor
A keras_nlp.models.Preprocessor layer used to preprocess input.
backbone propertykeras_nlp.models.Classifier.backbone
A keras_nlp.models.Backbone model with the core architecture.