Task
classkeras_nlp.models.Task()
Base class for all Task models.
A Task
wraps a keras_nlp.models.Backbone
and
a keras_nlp.models.Preprocessor
to create a model that can be directly
used for training, fine-tuning, and prediction for a given text problem.
All Task
models have backbone
and preprocessor
properties. By
default fit()
, predict()
and evaluate()
will preprocess all inputs
automatically. To preprocess inputs separately or with a custom function,
you can set task.preprocessor = None
, which disable any automatic
preprocessing on inputs.
All Task
classes include a from_preset()
constructor which can be used
to load a pre-trained config and weights. Calling from_preset()
on a task
will automatically instantiate a keras_nlp.models.Backbone
and
keras_nlp.models.Preprocessor
.
from_preset
methodTask.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,
)
save_to_preset
methodTask.save_to_preset(preset_dir)
Save task to a preset directory.
Arguments
preprocessor
propertykeras_nlp.models.Task.preprocessor
A keras_nlp.models.Preprocessor
layer used to preprocess input.
backbone
propertykeras_nlp.models.Task.backbone
A keras_nlp.models.Backbone
model with the core architecture.