-
Notifications
You must be signed in to change notification settings - Fork 251
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Create `Backbone` base class * Format * Throw exception if no presets. * Address comments 1 * Address comments 2 * Extend to RoBERTa * Format * Attach `from_preset` to subclass and extend to all models * format * Fix typos in docstring * More docstring typos
- Loading branch information
Showing
7 changed files
with
196 additions
and
379 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
# Copyright 2022 The KerasNLP Authors | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# https://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
"""Base class for Backbone models.""" | ||
|
||
import os | ||
|
||
from tensorflow import keras | ||
|
||
from keras_nlp.utils.python_utils import classproperty | ||
|
||
|
||
@keras.utils.register_keras_serializable(package="keras_nlp") | ||
class Backbone(keras.Model): | ||
def __init__(self, *args, **kwargs): | ||
super().__init__(*args, **kwargs) | ||
|
||
@classmethod | ||
def from_config(cls, config): | ||
return cls(**config) | ||
|
||
@classproperty | ||
def presets(cls): | ||
return {} | ||
|
||
@classmethod | ||
def from_preset( | ||
cls, | ||
preset, | ||
load_weights=True, | ||
**kwargs, | ||
): | ||
"""Instantiate {{model_name}} model from preset architecture and weights. | ||
Args: | ||
preset: string. Must be one of "{{preset_names}}". | ||
load_weights: Whether to load pre-trained weights into model. | ||
Defaults to `True`. | ||
Examples: | ||
```python | ||
# Load architecture and weights from preset | ||
model = {{model_name}}.from_preset("{{example_preset_name}}") | ||
# Load randomly initialized model from preset architecture | ||
model = {{model_name}}.from_preset( | ||
"{{example_preset_name}}", | ||
load_weights=False | ||
) | ||
``` | ||
""" | ||
|
||
if not cls.presets: | ||
raise NotImplementedError( | ||
"No presets have been created for this class." | ||
) | ||
|
||
if preset not in cls.presets: | ||
raise ValueError( | ||
"`preset` must be one of " | ||
f"""{", ".join(cls.presets)}. Received: {preset}.""" | ||
) | ||
metadata = cls.presets[preset] | ||
config = metadata["config"] | ||
model = cls.from_config({**config, **kwargs}) | ||
|
||
if not load_weights: | ||
return model | ||
|
||
weights = keras.utils.get_file( | ||
"model.h5", | ||
metadata["weights_url"], | ||
cache_subdir=os.path.join("models", preset), | ||
file_hash=metadata["weights_hash"], | ||
) | ||
|
||
model.load_weights(weights) | ||
return model |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.