Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
ModelZoo
ResNet50_tensorflow
Commits
2498f88f
Commit
2498f88f
authored
Nov 10, 2020
by
Chen Chen
Committed by
A. Unique TensorFlower
Nov 10, 2020
Browse files
Allow customized masked lm in BertPretrainerV2.
PiperOrigin-RevId: 341708068
parent
e6de0589
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
24 additions
and
7 deletions
+24
-7
official/nlp/modeling/models/bert_pretrainer.py
official/nlp/modeling/models/bert_pretrainer.py
+13
-5
official/nlp/modeling/models/bert_pretrainer_test.py
official/nlp/modeling/models/bert_pretrainer_test.py
+11
-2
No files found.
official/nlp/modeling/models/bert_pretrainer.py
View file @
2498f88f
...
@@ -177,6 +177,10 @@ class BertPretrainerV2(tf.keras.Model):
...
@@ -177,6 +177,10 @@ class BertPretrainerV2(tf.keras.Model):
to a Glorot uniform initializer.
to a Glorot uniform initializer.
classification_heads: A list of optional head layers to transform on encoder
classification_heads: A list of optional head layers to transform on encoder
sequence outputs.
sequence outputs.
customized_masked_lm: A customized masked_lm layer. If None, will create
a standard layer from `layers.MaskedLM`; if not None, will use the
specified masked_lm layer. Above arguments `mlm_activation` and
`mlm_initializer` will be ignored.
name: The name of the model.
name: The name of the model.
Inputs: Inputs defined by the encoder network, plus `masked_lm_positions` as a
Inputs: Inputs defined by the encoder network, plus `masked_lm_positions` as a
dictionary.
dictionary.
...
@@ -191,6 +195,7 @@ class BertPretrainerV2(tf.keras.Model):
...
@@ -191,6 +195,7 @@ class BertPretrainerV2(tf.keras.Model):
mlm_activation
=
None
,
mlm_activation
=
None
,
mlm_initializer
=
'glorot_uniform'
,
mlm_initializer
=
'glorot_uniform'
,
classification_heads
:
Optional
[
List
[
tf
.
keras
.
layers
.
Layer
]]
=
None
,
classification_heads
:
Optional
[
List
[
tf
.
keras
.
layers
.
Layer
]]
=
None
,
customized_masked_lm
:
Optional
[
tf
.
keras
.
layers
.
Layer
]
=
None
,
name
:
str
=
'bert'
,
name
:
str
=
'bert'
,
**
kwargs
):
**
kwargs
):
self
.
_self_setattr_tracking
=
False
self
.
_self_setattr_tracking
=
False
...
@@ -226,6 +231,9 @@ class BertPretrainerV2(tf.keras.Model):
...
@@ -226,6 +231,9 @@ class BertPretrainerV2(tf.keras.Model):
self
.
classification_heads
):
self
.
classification_heads
):
raise
ValueError
(
'Classification heads should have unique names.'
)
raise
ValueError
(
'Classification heads should have unique names.'
)
if
customized_masked_lm
is
not
None
:
self
.
masked_lm
=
customized_masked_lm
else
:
self
.
masked_lm
=
layers
.
MaskedLM
(
self
.
masked_lm
=
layers
.
MaskedLM
(
embedding_table
=
self
.
encoder_network
.
get_embedding_table
(),
embedding_table
=
self
.
encoder_network
.
get_embedding_table
(),
activation
=
mlm_activation
,
activation
=
mlm_activation
,
...
...
official/nlp/modeling/models/bert_pretrainer_test.py
View file @
2498f88f
...
@@ -19,6 +19,7 @@ from absl.testing import parameterized
...
@@ -19,6 +19,7 @@ from absl.testing import parameterized
import
tensorflow
as
tf
import
tensorflow
as
tf
from
tensorflow.python.keras
import
keras_parameterized
# pylint: disable=g-direct-tensorflow-import
from
tensorflow.python.keras
import
keras_parameterized
# pylint: disable=g-direct-tensorflow-import
from
official.nlp.modeling
import
layers
from
official.nlp.modeling
import
networks
from
official.nlp.modeling
import
networks
from
official.nlp.modeling.models
import
bert_pretrainer
from
official.nlp.modeling.models
import
bert_pretrainer
...
@@ -112,8 +113,10 @@ class BertPretrainerTest(keras_parameterized.TestCase):
...
@@ -112,8 +113,10 @@ class BertPretrainerTest(keras_parameterized.TestCase):
@
parameterized
.
parameters
(
itertools
.
product
(
@
parameterized
.
parameters
(
itertools
.
product
(
(
False
,
True
),
(
False
,
True
),
(
False
,
True
),
(
False
,
True
),
(
False
,
True
),
))
))
def
test_bert_pretrainerv2
(
self
,
dict_outputs
,
return_all_encoder_outputs
):
def
test_bert_pretrainerv2
(
self
,
dict_outputs
,
return_all_encoder_outputs
,
use_customized_masked_lm
):
"""Validate that the Keras object can be created."""
"""Validate that the Keras object can be created."""
# Build a transformer network to use within the BERT trainer.
# Build a transformer network to use within the BERT trainer.
vocab_size
=
100
vocab_size
=
100
...
@@ -129,8 +132,14 @@ class BertPretrainerTest(keras_parameterized.TestCase):
...
@@ -129,8 +132,14 @@ class BertPretrainerTest(keras_parameterized.TestCase):
dict_outputs
=
dict_outputs
)
dict_outputs
=
dict_outputs
)
# Create a BERT trainer with the created network.
# Create a BERT trainer with the created network.
if
use_customized_masked_lm
:
customized_masked_lm
=
layers
.
MaskedLM
(
embedding_table
=
test_network
.
get_embedding_table
())
else
:
customized_masked_lm
=
None
bert_trainer_model
=
bert_pretrainer
.
BertPretrainerV2
(
bert_trainer_model
=
bert_pretrainer
.
BertPretrainerV2
(
encoder_network
=
test_network
)
encoder_network
=
test_network
,
customized_masked_lm
=
customized_masked_lm
)
num_token_predictions
=
20
num_token_predictions
=
20
# Create a set of 2-dimensional inputs (the first dimension is implicit).
# Create a set of 2-dimensional inputs (the first dimension is implicit).
word_ids
=
tf
.
keras
.
Input
(
shape
=
(
sequence_length
,),
dtype
=
tf
.
int32
)
word_ids
=
tf
.
keras
.
Input
(
shape
=
(
sequence_length
,),
dtype
=
tf
.
int32
)
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment