Backbone
- 원본 링크 : https://keras.io/api/keras_hub/base_classes/backbone/
- 최종 확인 : 2024-11-26
Backbone class
keras_hub.models.Backbone(*args, dtype=None, **kwargs)Base class for all Backbone models.
A Backbone is the basic architecture for a given NLP model. Unlike a
keras_hub.models.Task, a Backbone is not tailored to any specific loss
function and training setup. A Backbone generally outputs the last hidden
states of an architecture before any output predictions.
A Backbone can be used in one of two ways:
- Through a
Taskclass, which will wrap and extend aBackboneso it can be used with high level Keras functions likefit(),predict()orevaluate().Taskclasses are built with a particular training objective in mind (e.g. classification or language modeling). - Directly, by extending underlying functional model with additional outputs and training setup. This is the most flexible approach, and can allow for any outputs, loss, or custom training loop.
All backbones include a from_preset() constructor which can be used to
load a pre-trained config and weights.
Example
# Load a BERT backbone with pre-trained weights.
backbone = keras_hub.models.Backbone.from_preset(
"bert_base_en",
)
# Load a GPT2 backbone with pre-trained weights at bfloat16 precision.
backbone = keras_hub.models.Backbone.from_preset(
"gpt2_base_en",
dtype="bfloat16",
trainable=False,
)from_preset method
Backbone.from_preset(preset, load_weights=True, **kwargs)Instantiate a keras_hub.models.Backbone 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:
- a built-in preset identifier like
'bert_base_en' - a Kaggle Models handle like
'kaggle://user/bert/keras/bert_base_en' - a Hugging Face handle like
'hf://user/bert_base_en' - a path to a local preset directory like
'./bert_base_en'
This constructor can be called in one of two ways. Either from the base
class like keras_hub.models.Backbone.from_preset(), or from
a model class like keras_hub.models.GemmaBackbone.from_preset().
If calling from the base class, the subclass of the returning object
will be inferred from the config in the preset directory.
For any Backbone subclass, you can run cls.presets.keys() to list
all built-in presets available on the class.
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. IfFalse, the weights will be randomly initialized.
Examples
# Load a Gemma backbone with pre-trained weights.
model = keras_hub.models.Backbone.from_preset(
"gemma_2b_en",
)
# Load a Bert backbone with a pre-trained config and random weights.
model = keras_hub.models.Backbone.from_preset(
"bert_base_en",
load_weights=False,
)token_embedding property
keras_hub.models.Backbone.token_embeddingA keras.layers.Embedding instance for embedding token ids.
This layer embeds integer token ids to the hidden dim of the model.
enable_lora method
Backbone.enable_lora(rank)Enable Lora on the backbone.
Calling this method will freeze all weights on the backbone,
while enabling Lora on the query & value EinsumDense layers
of the attention layers.
save_lora_weights method
Backbone.save_lora_weights(filepath)load_lora_weights method
Backbone.load_lora_weights(filepath)save_to_preset method
Backbone.save_to_preset(preset_dir)Save backbone to a preset directory.
Arguments
- preset_dir: The path to the local model preset directory.