-
Notifications
You must be signed in to change notification settings - Fork 301
Add a learned positional embedding layer #47
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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,113 @@ | ||
# 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. | ||
|
||
"""Position embedding implementation based on `keras.layers.Layer`.""" | ||
|
||
import tensorflow as tf | ||
from tensorflow import keras | ||
|
||
SEQUENCE_AXIS = -2 | ||
|
||
|
||
class PositionEmbedding(keras.layers.Layer): | ||
"""Creates a layer which learns a position embedding for inputs sequences. | ||
|
||
This class assumes that in the input tensor, the last dimension corresponds | ||
to the features, and the dimension before the last corresponds to the | ||
sequence. | ||
|
||
This class accepts `RaggedTensor`s as inputs to process batches of sequences | ||
of different lengths. The one ragged dimension must be the dimension that | ||
corresponds to the sequence, that is, the penultimate dimension. | ||
|
||
Args: | ||
max_length: The maximum length of the dynamic sequence. | ||
initializer: The initializer to use for the embedding weights. Defaults | ||
to "glorot_uniform". | ||
seq_axis: The axis of the input tensor where we add the embeddings. | ||
|
||
Example: | ||
```python | ||
token_embeddings = layers.Embedding( | ||
input_dim=vocab_size, output_dim=embed_dim | ||
) | ||
position_embeddings = keras_nlp.layers.PositionEmbedding( | ||
max_length=max_length | ||
) | ||
|
||
embedded_tokens = self.token_embeddings(inputs) | ||
embedded_positions = self.position_embeddings(embedded_tokens) | ||
outputs = embedded_tokens + embedded_positions | ||
``` | ||
|
||
Reference: | ||
[BERT: Pre-training of Deep Bidirectional Transformers for Language | ||
Understanding](https://arxiv.org/abs/1810.04805). | ||
""" | ||
|
||
def __init__( | ||
self, | ||
max_length, | ||
initializer="glorot_uniform", | ||
**kwargs, | ||
): | ||
super().__init__(**kwargs) | ||
if max_length is None: | ||
raise ValueError("`max_length` must be an Integer, not `None`.") | ||
self.max_length = int(max_length) | ||
self.initializer = keras.initializers.get(initializer) | ||
|
||
def get_config(self): | ||
config = super().get_config() | ||
config.update( | ||
{ | ||
"max_length": self.max_length, | ||
"initializer": keras.initializers.serialize(self.initializer), | ||
} | ||
) | ||
return config | ||
|
||
def build(self, input_shape): | ||
feature_size = input_shape[-1] | ||
self.position_embeddings = self.add_weight( | ||
"embeddings", | ||
shape=[self.max_length, feature_size], | ||
initializer=self.initializer, | ||
trainable=True, | ||
) | ||
|
||
super().build(input_shape) | ||
|
||
def call(self, inputs): | ||
if isinstance(inputs, tf.RaggedTensor): | ||
bounding_shape = inputs.bounding_shape() | ||
position_embeddings = self._trim_and_broadcast_position_embeddings( | ||
bounding_shape, | ||
) | ||
# then apply row lengths to recreate the same ragged shape as inputs | ||
return tf.RaggedTensor.from_tensor( | ||
position_embeddings, | ||
inputs.nested_row_lengths(), | ||
) | ||
else: | ||
return self._trim_and_broadcast_position_embeddings( | ||
tf.shape(inputs), | ||
) | ||
|
||
def _trim_and_broadcast_position_embeddings(self, shape): | ||
sequence_length = shape[SEQUENCE_AXIS] | ||
# trim to match the length of the sequence | ||
position_embeddings = self.position_embeddings[:sequence_length, :] | ||
# then broadcast to add the missing dimensions to match "shape" | ||
return tf.broadcast_to(position_embeddings, shape) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.