Task
classkeras_nlp.models.Task(*args, compile=True, **kwargs)
Base class for all Task models.
A Task
wraps a keras_hub.models.Backbone
and
a keras_hub.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_hub.models.Backbone
and
keras_hub.models.Preprocessor
.
Arguments
True
. If True
will compile the model
with default parameters on construction. Model can still be
recompiled with a new loss, optimizer and metrics before training.from_preset
methodTask.from_preset(preset, load_weights=True, **kwargs)
Instantiate a keras_hub.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
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_hub.models.CausalLM.from_preset()
, or
from a model class like keras_hub.models.BertTextClassifier.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
, saved weights will be loaded into
the model architecture. If False
, all weights will be
randomly initialized.Examples
# Load a Gemma generative task.
causal_lm = keras_hub.models.CausalLM.from_preset(
"gemma_2b_en",
)
# Load a Bert classification task.
model = keras_hub.models.TextClassifier.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_hub.models.Preprocessor
layer used to preprocess input.
backbone
propertykeras_nlp.models.Task.backbone
A keras_hub.models.Backbone
model with the core architecture.