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
7d5c47aa
Commit
7d5c47aa
authored
Sep 10, 2020
by
Hongkun Yu
Committed by
A. Unique TensorFlower
Sep 10, 2020
Browse files
Internal change
PiperOrigin-RevId: 330982547
parent
b792005c
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
25 additions
and
38 deletions
+25
-38
official/nlp/modeling/networks/mobile_bert_encoder.py
official/nlp/modeling/networks/mobile_bert_encoder.py
+5
-10
official/nlp/modeling/networks/mobile_bert_encoder_test.py
official/nlp/modeling/networks/mobile_bert_encoder_test.py
+20
-28
No files found.
official/nlp/modeling/networks/mobile_bert_encoder.py
View file @
7d5c47aa
...
...
@@ -406,8 +406,6 @@ class MobileBERTEncoder(tf.keras.Model):
num_feedforward_networks
=
4
,
normalization_type
=
'no_norm'
,
classifier_activation
=
False
,
return_all_layers
=
False
,
return_attention_score
=
False
,
**
kwargs
):
"""Class initialization.
...
...
@@ -438,8 +436,6 @@ class MobileBERTEncoder(tf.keras.Model):
MobileBERT paper. 'layer_norm' is used for the teacher model.
classifier_activation: If using the tanh activation for the final
representation of the [CLS] token in fine-tuning.
return_all_layers: If return all layer outputs.
return_attention_score: If return attention scores for each layer.
**kwargs: Other keyworded and arguments.
"""
self
.
_self_setattr_tracking
=
False
...
...
@@ -513,12 +509,11 @@ class MobileBERTEncoder(tf.keras.Model):
else
:
self
.
_pooler_layer
=
None
if
return_all_layers
:
outputs
=
[
all_layer_outputs
,
first_token
]
else
:
outputs
=
[
prev_output
,
first_token
]
if
return_attention_score
:
outputs
.
append
(
all_attention_scores
)
outputs
=
dict
(
sequence_output
=
prev_output
,
pooled_output
=
first_token
,
encoder_outputs
=
all_layer_outputs
,
attention_scores
=
all_attention_scores
)
super
(
MobileBERTEncoder
,
self
).
__init__
(
inputs
=
self
.
inputs
,
outputs
=
outputs
,
**
kwargs
)
...
...
official/nlp/modeling/networks/mobile_bert_encoder_test.py
View file @
7d5c47aa
...
...
@@ -32,7 +32,7 @@ def generate_fake_input(batch_size=1, seq_len=5, vocab_size=10000, seed=0):
return
fake_input
class
Mo
deling
Test
(
parameterized
.
TestCase
,
tf
.
test
.
TestCase
):
class
Mo
bileBertEncoder
Test
(
parameterized
.
TestCase
,
tf
.
test
.
TestCase
):
def
test_embedding_layer_with_token_type
(
self
):
layer
=
mobile_bert_encoder
.
MobileBertEmbedding
(
10
,
8
,
2
,
16
)
...
...
@@ -116,7 +116,9 @@ class ModelingTest(parameterized.TestCase, tf.test.TestCase):
word_ids
=
tf
.
keras
.
Input
(
shape
=
(
sequence_length
,),
dtype
=
tf
.
int32
)
mask
=
tf
.
keras
.
Input
(
shape
=
(
sequence_length
,),
dtype
=
tf
.
int32
)
type_ids
=
tf
.
keras
.
Input
(
shape
=
(
sequence_length
,),
dtype
=
tf
.
int32
)
layer_output
,
pooler_output
=
test_network
([
word_ids
,
mask
,
type_ids
])
outputs
=
test_network
([
word_ids
,
mask
,
type_ids
])
layer_output
,
pooler_output
=
outputs
[
'sequence_output'
],
outputs
[
'pooled_output'
]
self
.
assertIsInstance
(
test_network
.
transformer_layers
,
list
)
self
.
assertLen
(
test_network
.
transformer_layers
,
num_blocks
)
...
...
@@ -134,13 +136,13 @@ class ModelingTest(parameterized.TestCase, tf.test.TestCase):
test_network
=
mobile_bert_encoder
.
MobileBERTEncoder
(
word_vocab_size
=
100
,
hidden_size
=
hidden_size
,
num_blocks
=
num_blocks
,
return_all_layers
=
True
)
num_blocks
=
num_blocks
)
word_ids
=
tf
.
keras
.
Input
(
shape
=
(
sequence_length
,),
dtype
=
tf
.
int32
)
mask
=
tf
.
keras
.
Input
(
shape
=
(
sequence_length
,),
dtype
=
tf
.
int32
)
type_ids
=
tf
.
keras
.
Input
(
shape
=
(
sequence_length
,),
dtype
=
tf
.
int32
)
all_layer_output
,
_
=
test_network
([
word_ids
,
mask
,
type_ids
])
outputs
=
test_network
([
word_ids
,
mask
,
type_ids
])
all_layer_output
=
outputs
[
'encoder_outputs'
]
self
.
assertIsInstance
(
all_layer_output
,
list
)
self
.
assertLen
(
all_layer_output
,
num_blocks
+
1
)
...
...
@@ -153,16 +155,13 @@ class ModelingTest(parameterized.TestCase, tf.test.TestCase):
test_network
=
mobile_bert_encoder
.
MobileBERTEncoder
(
word_vocab_size
=
vocab_size
,
hidden_size
=
hidden_size
,
num_blocks
=
num_blocks
,
return_all_layers
=
False
)
num_blocks
=
num_blocks
)
word_ids
=
tf
.
keras
.
Input
(
shape
=
(
sequence_length
,),
dtype
=
tf
.
int32
)
mask
=
tf
.
keras
.
Input
(
shape
=
(
sequence_length
,),
dtype
=
tf
.
int32
)
type_ids
=
tf
.
keras
.
Input
(
shape
=
(
sequence_length
,),
dtype
=
tf
.
int32
)
layer_out_tensor
,
pooler_out_tensor
=
test_network
(
[
word_ids
,
mask
,
type_ids
])
model
=
tf
.
keras
.
Model
([
word_ids
,
mask
,
type_ids
],
[
layer_out_tensor
,
pooler_out_tensor
])
outputs
=
test_network
([
word_ids
,
mask
,
type_ids
])
model
=
tf
.
keras
.
Model
([
word_ids
,
mask
,
type_ids
],
outputs
)
input_seq
=
generate_fake_input
(
batch_size
=
1
,
seq_len
=
sequence_length
,
vocab_size
=
vocab_size
)
...
...
@@ -170,13 +169,12 @@ class ModelingTest(parameterized.TestCase, tf.test.TestCase):
batch_size
=
1
,
seq_len
=
sequence_length
,
vocab_size
=
2
)
token_type
=
generate_fake_input
(
batch_size
=
1
,
seq_len
=
sequence_length
,
vocab_size
=
2
)
layer_output
,
pooler_output
=
model
.
predict
(
[
input_seq
,
input_mask
,
token_type
])
outputs
=
model
.
predict
([
input_seq
,
input_mask
,
token_type
])
layer
_output_shape
=
[
1
,
sequence_length
,
hidden_size
]
self
.
assertAllEqual
(
layer
_output
.
shape
,
layer
_output_shape
)
poole
r
_output_shape
=
[
1
,
hidden_size
]
self
.
assertAllEqual
(
poole
r
_output
.
shape
,
poole
r
_output_shape
)
sequence
_output_shape
=
[
1
,
sequence_length
,
hidden_size
]
self
.
assertAllEqual
(
outputs
[
'sequence
_output
'
]
.
shape
,
sequence
_output_shape
)
poole
d
_output_shape
=
[
1
,
hidden_size
]
self
.
assertAllEqual
(
outputs
[
'
poole
d
_output
'
]
.
shape
,
poole
d
_output_shape
)
def
test_mobilebert_encoder_invocation_with_attention_score
(
self
):
vocab_size
=
100
...
...
@@ -186,18 +184,13 @@ class ModelingTest(parameterized.TestCase, tf.test.TestCase):
test_network
=
mobile_bert_encoder
.
MobileBERTEncoder
(
word_vocab_size
=
vocab_size
,
hidden_size
=
hidden_size
,
num_blocks
=
num_blocks
,
return_all_layers
=
False
,
return_attention_score
=
True
)
num_blocks
=
num_blocks
)
word_ids
=
tf
.
keras
.
Input
(
shape
=
(
sequence_length
,),
dtype
=
tf
.
int32
)
mask
=
tf
.
keras
.
Input
(
shape
=
(
sequence_length
,),
dtype
=
tf
.
int32
)
type_ids
=
tf
.
keras
.
Input
(
shape
=
(
sequence_length
,),
dtype
=
tf
.
int32
)
layer_out_tensor
,
pooler_out_tensor
,
attention_out_tensor
=
test_network
(
[
word_ids
,
mask
,
type_ids
])
model
=
tf
.
keras
.
Model
(
[
word_ids
,
mask
,
type_ids
],
[
layer_out_tensor
,
pooler_out_tensor
,
attention_out_tensor
])
outputs
=
test_network
([
word_ids
,
mask
,
type_ids
])
model
=
tf
.
keras
.
Model
([
word_ids
,
mask
,
type_ids
],
outputs
)
input_seq
=
generate_fake_input
(
batch_size
=
1
,
seq_len
=
sequence_length
,
vocab_size
=
vocab_size
)
...
...
@@ -205,9 +198,8 @@ class ModelingTest(parameterized.TestCase, tf.test.TestCase):
batch_size
=
1
,
seq_len
=
sequence_length
,
vocab_size
=
2
)
token_type
=
generate_fake_input
(
batch_size
=
1
,
seq_len
=
sequence_length
,
vocab_size
=
2
)
_
,
_
,
attention_score_output
=
model
.
predict
(
[
input_seq
,
input_mask
,
token_type
])
self
.
assertLen
(
attention_score_output
,
num_blocks
)
outputs
=
model
.
predict
([
input_seq
,
input_mask
,
token_type
])
self
.
assertLen
(
outputs
[
'attention_scores'
],
num_blocks
)
@
parameterized
.
named_parameters
(
(
'sequence_classification'
,
models
.
BertClassifier
,
[
None
,
5
]),
...
...
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