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
3008753b
Commit
3008753b
authored
Sep 26, 2020
by
Chen Chen
Committed by
A. Unique TensorFlower
Sep 26, 2020
Browse files
Internal change
PiperOrigin-RevId: 333979065
parent
9269550c
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
34 additions
and
8 deletions
+34
-8
official/nlp/tasks/utils.py
official/nlp/tasks/utils.py
+34
-8
No files found.
official/nlp/tasks/utils.py
View file @
3008753b
...
@@ -16,25 +16,51 @@
...
@@ -16,25 +16,51 @@
"""Common utils for tasks."""
"""Common utils for tasks."""
from
typing
import
Any
,
Callable
from
typing
import
Any
,
Callable
from
absl
import
logging
import
orbit
import
orbit
import
tensorflow
as
tf
import
tensorflow
as
tf
import
tensorflow_hub
as
hub
import
tensorflow_hub
as
hub
def
get_encoder_from_hub
(
hub_module
:
str
)
->
tf
.
keras
.
Model
:
def
get_encoder_from_hub
(
hub_model
)
->
tf
.
keras
.
Model
:
"""Gets an encoder from hub."""
"""Gets an encoder from hub.
Args:
hub_model: A tfhub model loaded by `hub.load(...)`.
Returns:
A tf.keras.Model.
"""
input_word_ids
=
tf
.
keras
.
layers
.
Input
(
input_word_ids
=
tf
.
keras
.
layers
.
Input
(
shape
=
(
None
,),
dtype
=
tf
.
int32
,
name
=
'input_word_ids'
)
shape
=
(
None
,),
dtype
=
tf
.
int32
,
name
=
'input_word_ids'
)
input_mask
=
tf
.
keras
.
layers
.
Input
(
input_mask
=
tf
.
keras
.
layers
.
Input
(
shape
=
(
None
,),
dtype
=
tf
.
int32
,
name
=
'input_mask'
)
shape
=
(
None
,),
dtype
=
tf
.
int32
,
name
=
'input_mask'
)
input_type_ids
=
tf
.
keras
.
layers
.
Input
(
input_type_ids
=
tf
.
keras
.
layers
.
Input
(
shape
=
(
None
,),
dtype
=
tf
.
int32
,
name
=
'input_type_ids'
)
shape
=
(
None
,),
dtype
=
tf
.
int32
,
name
=
'input_type_ids'
)
hub_layer
=
hub
.
KerasLayer
(
hub_module
,
trainable
=
True
)
hub_layer
=
hub
.
KerasLayer
(
hub_model
,
trainable
=
True
)
pooled_output
,
sequence_output
=
hub_layer
(
output_dict
=
{}
[
input_word_ids
,
input_mask
,
input_type_ids
])
dict_input
=
dict
(
return
tf
.
keras
.
Model
(
input_word_ids
=
input_word_ids
,
inputs
=
[
input_word_ids
,
input_mask
,
input_type_ids
],
input_mask
=
input_mask
,
outputs
=
[
sequence_output
,
pooled_output
])
input_type_ids
=
input_type_ids
)
# The legacy hub model takes a list as input and returns a Tuple of
# `pooled_output` and `sequence_output`, while the new hub model takes dict
# as input and returns a dict.
# TODO(chendouble): Remove the support of legacy hub model when the new ones
# are released.
hub_output_signature
=
hub_model
.
signatures
[
'serving_default'
].
outputs
if
len
(
hub_output_signature
)
==
2
:
logging
.
info
(
'Use the legacy hub module with list as input/output.'
)
pooled_output
,
sequence_output
=
hub_layer
(
[
input_word_ids
,
input_mask
,
input_type_ids
])
output_dict
[
'pooled_output'
]
=
pooled_output
output_dict
[
'sequence_output'
]
=
sequence_output
else
:
logging
.
info
(
'Use the new hub module with dict as input/output.'
)
output_dict
=
hub_layer
(
dict_input
)
return
tf
.
keras
.
Model
(
inputs
=
dict_input
,
outputs
=
output_dict
)
def
predict
(
predict_step_fn
:
Callable
[[
Any
],
Any
],
def
predict
(
predict_step_fn
:
Callable
[[
Any
],
Any
],
...
...
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