Skip to content

Commit 4b7a417

Browse files
authored
Rename max_length to sequence_length (#149)
In the >90% use case (dense inputs) the value provided here will be the sequence_length itself. This rename will improve the readability of our hello world examples.
1 parent 1f39ecc commit 4b7a417

File tree

5 files changed

+35
-35
lines changed

5 files changed

+35
-35
lines changed

examples/bert/bert_model.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -488,7 +488,7 @@ def __init__(
488488

489489
self._position_embedding_layer = keras_nlp.layers.PositionEmbedding(
490490
initializer=initializer,
491-
max_length=max_sequence_length,
491+
sequence_length=max_sequence_length,
492492
name="position_embedding",
493493
)
494494

keras_nlp/layers/position_embedding.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ class PositionEmbedding(keras.layers.Layer):
3232
corresponds to the sequence, that is, the penultimate dimension.
3333
3434
Args:
35-
max_length: The maximum length of the dynamic sequence.
35+
sequence_length: The maximum length of the dynamic sequence.
3636
initializer: The initializer to use for the embedding weights. Defaults
3737
to "glorot_uniform".
3838
seq_axis: The axis of the input tensor where we add the embeddings.
@@ -43,7 +43,7 @@ class PositionEmbedding(keras.layers.Layer):
4343
input_dim=vocab_size, output_dim=embed_dim
4444
)
4545
position_embeddings = keras_nlp.layers.PositionEmbedding(
46-
max_length=max_length
46+
sequence_length=sequence_length
4747
)
4848
4949
embedded_tokens = token_embeddings(inputs)
@@ -58,23 +58,23 @@ class PositionEmbedding(keras.layers.Layer):
5858

5959
def __init__(
6060
self,
61-
max_length,
61+
sequence_length,
6262
initializer="glorot_uniform",
6363
**kwargs,
6464
):
6565
super().__init__(**kwargs)
66-
if max_length is None:
66+
if sequence_length is None:
6767
raise ValueError(
68-
"`max_length` must be an Integer, received `None`."
68+
"`sequence_length` must be an Integer, received `None`."
6969
)
70-
self.max_length = int(max_length)
70+
self.sequence_length = int(sequence_length)
7171
self.initializer = keras.initializers.get(initializer)
7272

7373
def get_config(self):
7474
config = super().get_config()
7575
config.update(
7676
{
77-
"max_length": self.max_length,
77+
"sequence_length": self.sequence_length,
7878
"initializer": keras.initializers.serialize(self.initializer),
7979
}
8080
)
@@ -84,7 +84,7 @@ def build(self, input_shape):
8484
feature_size = input_shape[-1]
8585
self.position_embeddings = self.add_weight(
8686
"embeddings",
87-
shape=[self.max_length, feature_size],
87+
shape=[self.sequence_length, feature_size],
8888
initializer=self.initializer,
8989
trainable=True,
9090
)

keras_nlp/layers/position_embedding_test.py

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ def test_static_layer_output_shape(self):
3434
sequence_length = 21
3535
feature_size = 30
3636
test_layer = position_embedding.PositionEmbedding(
37-
max_length=sequence_length
37+
sequence_length=sequence_length
3838
)
3939
input_tensor = tf.keras.Input(shape=(sequence_length, feature_size))
4040
output_tensor = test_layer(input_tensor)
@@ -51,7 +51,7 @@ def test_more_than_3_dimensions_static(self):
5151
sequence_length = 21
5252
feature_size = 30
5353
test_layer = position_embedding.PositionEmbedding(
54-
max_length=sequence_length
54+
sequence_length=sequence_length
5555
)
5656
input_tensor = tf.keras.Input(
5757
shape=(feature_size, sequence_length, feature_size)
@@ -75,7 +75,7 @@ def test_float16_dtype(self):
7575
sequence_length = 21
7676
feature_size = 30
7777
test_layer = position_embedding.PositionEmbedding(
78-
max_length=sequence_length, dtype="float16"
78+
sequence_length=sequence_length, dtype="float16"
7979
)
8080
input_tensor = tf.keras.Input(shape=(sequence_length, feature_size))
8181
output_tensor = test_layer(input_tensor)
@@ -91,7 +91,7 @@ def test_dynamic_layer_output_shape(self):
9191
max_sequence_length = 40
9292
feature_size = 30
9393
test_layer = position_embedding.PositionEmbedding(
94-
max_length=max_sequence_length
94+
sequence_length=max_sequence_length
9595
)
9696
# Create a 3-dimensional input (the first dimension is implicit).
9797
input_tensor = tf.keras.Input(shape=(None, feature_size))
@@ -107,7 +107,7 @@ def test_more_than_3_dimensions_dynamic(self):
107107
max_sequence_length = 60
108108
feature_size = 30
109109
test_layer = position_embedding.PositionEmbedding(
110-
max_length=max_sequence_length
110+
sequence_length=max_sequence_length
111111
)
112112
# Create a 4-dimensional input (the first dimension is implicit).
113113
input_tensor = tf.keras.Input(shape=(None, None, feature_size))
@@ -123,7 +123,7 @@ def test_dynamic_layer_slicing(self):
123123
max_sequence_length = 40
124124
feature_size = 30
125125
test_layer = position_embedding.PositionEmbedding(
126-
max_length=max_sequence_length
126+
sequence_length=max_sequence_length
127127
)
128128
# Create a 3-dimensional input (the first dimension is implicit).
129129
input_tensor = tf.keras.Input(shape=(None, feature_size))
@@ -148,7 +148,7 @@ def test_callable_initializer(self):
148148
max_sequence_length = 4
149149
feature_size = 3
150150
test_layer = position_embedding.PositionEmbedding(
151-
max_length=max_sequence_length,
151+
sequence_length=max_sequence_length,
152152
initializer=custom_init,
153153
)
154154
inputs = tf.keras.Input(shape=(max_sequence_length, feature_size))
@@ -172,7 +172,7 @@ def test_ragged_tensor_with_3_dimensions(self):
172172
max_sequence_length = 4
173173
feature_size = 2
174174
test_layer = position_embedding.PositionEmbedding(
175-
max_length=max_sequence_length,
175+
sequence_length=max_sequence_length,
176176
initializer=custom_init,
177177
)
178178
# Create a 3-dimensional ragged input (the first dimension is implicit).
@@ -209,7 +209,7 @@ def test_ragged_tensor_with_4_dimensions(self):
209209
max_sequence_length = 4
210210
feature_size = 2
211211
test_layer = position_embedding.PositionEmbedding(
212-
max_length=max_sequence_length,
212+
sequence_length=max_sequence_length,
213213
initializer=custom_init,
214214
)
215215
# Create a 4-dimensional ragged input (the first dimension is implicit).
@@ -254,7 +254,7 @@ def test_one_training_step(self):
254254
max_sequence_length = 4
255255
feature_size = 3
256256
test_layer = position_embedding.PositionEmbedding(
257-
max_length=max_sequence_length
257+
sequence_length=max_sequence_length
258258
)
259259
inputs = tf.keras.Input(shape=(max_sequence_length, feature_size))
260260
outputs = test_layer(inputs)
@@ -283,11 +283,11 @@ def test_one_training_step(self):
283283
def test_get_config_and_from_config(self):
284284
max_sequence_length = 40
285285
test_layer = position_embedding.PositionEmbedding(
286-
max_length=max_sequence_length
286+
sequence_length=max_sequence_length
287287
)
288288
config = test_layer.get_config()
289289
expected_config_subset = {
290-
"max_length": max_sequence_length,
290+
"sequence_length": max_sequence_length,
291291
"initializer": {
292292
"class_name": "GlorotUniform",
293293
"config": {"seed": None},
@@ -306,7 +306,7 @@ def test_save_model(self):
306306
max_sequence_length = 4
307307
feature_size = 6
308308
test_layer = position_embedding.PositionEmbedding(
309-
max_length=max_sequence_length
309+
sequence_length=max_sequence_length
310310
)
311311
inputs = tf.keras.Input(shape=(max_sequence_length, feature_size))
312312
outputs = test_layer(inputs)

keras_nlp/layers/token_and_position_embedding.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ class TokenAndPositionEmbedding(keras.layers.Layer):
2828
Args:
2929
vocabulary_size: The size of the vocabulary (should be no larger
3030
than 999)
31-
max_length: The maximum length of input sequence
31+
sequence_length: The maximum length of input sequence
3232
embedding_dim: The output dimension of the embedding layer
3333
embeddings_initializer: The initializer to use for the Embedding
3434
Layers
@@ -49,7 +49,7 @@ class TokenAndPositionEmbedding(keras.layers.Layer):
4949
inputs = tf.keras.Input(shape=(seq_length,))
5050
embedding_layer = keras_nlp.layers.TokenAndPositionEmbedding(
5151
vocabulary_size=vocab_size,
52-
max_length=max_length,
52+
sequence_length=seq_length,
5353
embedding_dim=embed_dim,
5454
)
5555
outputs = embedding_layer(inputs)
@@ -59,7 +59,7 @@ class TokenAndPositionEmbedding(keras.layers.Layer):
5959
def __init__(
6060
self,
6161
vocabulary_size,
62-
max_length,
62+
sequence_length,
6363
embedding_dim,
6464
embeddings_initializer="glorot_uniform",
6565
mask_zero=False,
@@ -70,16 +70,16 @@ def __init__(
7070
raise ValueError(
7171
"`vocabulary_size` must be an Integer, received `None`."
7272
)
73-
if max_length is None:
73+
if sequence_length is None:
7474
raise ValueError(
75-
"`max_length` must be an Integer, received `None`."
75+
"`sequence_length` must be an Integer, received `None`."
7676
)
7777
if embedding_dim is None:
7878
raise ValueError(
7979
"`embedding_dim` must be an Integer, received `None`."
8080
)
8181
self.vocabulary_size = int(vocabulary_size)
82-
self.max_length = int(max_length)
82+
self.sequence_length = int(sequence_length)
8383
self.embedding_dim = int(embedding_dim)
8484
self.token_embedding = keras.layers.Embedding(
8585
vocabulary_size,
@@ -88,7 +88,7 @@ def __init__(
8888
mask_zero=mask_zero,
8989
)
9090
self.position_embedding = keras_nlp.layers.PositionEmbedding(
91-
max_length=max_length,
91+
sequence_length=sequence_length,
9292
initializer=embeddings_initializer,
9393
)
9494
self.supports_masking = self.token_embedding.supports_masking
@@ -98,7 +98,7 @@ def get_config(self):
9898
config.update(
9999
{
100100
"vocabulary_size": self.vocabulary_size,
101-
"max_length": self.max_length,
101+
"sequence_length": self.sequence_length,
102102
"embedding_dim": self.embedding_dim,
103103
"embeddings_initializer": keras.initializers.serialize(
104104
self.token_embedding.embeddings_initializer

keras_nlp/layers/token_and_position_embedding_test.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ class TokenAndPositionEmbeddingTest(tf.test.TestCase):
3232
def test_get_config_and_from_config(self):
3333
token_and_position_embed = TokenAndPositionEmbedding(
3434
vocabulary_size=5,
35-
max_length=10,
35+
sequence_length=10,
3636
embedding_dim=32,
3737
)
3838

@@ -62,7 +62,7 @@ def test_ragged_tensor(self):
6262
embedding_dim = 3
6363
test_layer = TokenAndPositionEmbedding(
6464
vocabulary_size=vocabulary_size,
65-
max_length=sequence_length,
65+
sequence_length=sequence_length,
6666
embedding_dim=embedding_dim,
6767
embeddings_initializer=custom_embed_init,
6868
)
@@ -106,7 +106,7 @@ def test_dense_tensor(self):
106106
embedding_dim = 3
107107
test_layer = TokenAndPositionEmbedding(
108108
vocabulary_size=vocabulary_size,
109-
max_length=sequence_length,
109+
sequence_length=sequence_length,
110110
embedding_dim=embedding_dim,
111111
embeddings_initializer=custom_embed_init,
112112
)
@@ -124,7 +124,7 @@ def test_dense_tensor(self):
124124
def test_mask_propagation(self):
125125
test_layer = TokenAndPositionEmbedding(
126126
vocabulary_size=5,
127-
max_length=4,
127+
sequence_length=4,
128128
embedding_dim=3,
129129
mask_zero=True,
130130
)
@@ -139,7 +139,7 @@ def test_save_model(self):
139139
embedding_dim = 3
140140
test_layer = TokenAndPositionEmbedding(
141141
vocabulary_size=vocabulary_size,
142-
max_length=sequence_length,
142+
sequence_length=sequence_length,
143143
embedding_dim=embedding_dim,
144144
)
145145
inputs = keras.Input(shape=(sequence_length,))

0 commit comments

Comments
 (0)