Task

[source]

Task class

keras_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.


[source]

from_preset method

Task.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:

  1. a built in preset identifier like 'bert_base_en'
  2. a Kaggle Models handle like 'kaggle://user/bert/keras/bert_base_en'
  3. a Hugging Face handle like 'hf://user/bert_base_en'
  4. a path to a local preset directory like './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

  • preset: string. A built in preset identifier, a Kaggle Models handle, a Hugging Face handle, or a path to a local directory.
  • load_weights: bool. If 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,
)

[source]

save_to_preset method

Task.save_to_preset(preset_dir)

Save task to a preset directory.

Arguments

  • preset_dir: The path to the local model preset directory.

preprocessor property

keras_nlp.models.Task.preprocessor

A keras_nlp.models.Preprocessor layer used to preprocess input.


backbone property

keras_nlp.models.Task.backbone

A keras_nlp.models.Backbone model with the core architecture.