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
a785a49a
Commit
a785a49a
authored
Aug 17, 2020
by
A. Unique TensorFlower
Browse files
Merge pull request #9070 from NivekNey:patch-1
PiperOrigin-RevId: 327072144
parents
c60168f0
fbf3470f
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
14 additions
and
4 deletions
+14
-4
official/nlp/bert/run_classifier.py
official/nlp/bert/run_classifier.py
+14
-4
No files found.
official/nlp/bert/run_classifier.py
View file @
a785a49a
...
@@ -263,6 +263,7 @@ def run_keras_compile_fit(model_dir,
...
@@ -263,6 +263,7 @@ def run_keras_compile_fit(model_dir,
def
get_predictions_and_labels
(
strategy
,
def
get_predictions_and_labels
(
strategy
,
trained_model
,
trained_model
,
eval_input_fn
,
eval_input_fn
,
is_regression
=
False
,
return_probs
=
False
):
return_probs
=
False
):
"""Obtains predictions of trained model on evaluation data.
"""Obtains predictions of trained model on evaluation data.
...
@@ -273,6 +274,7 @@ def get_predictions_and_labels(strategy,
...
@@ -273,6 +274,7 @@ def get_predictions_and_labels(strategy,
strategy: Distribution strategy.
strategy: Distribution strategy.
trained_model: Trained model with preloaded weights.
trained_model: Trained model with preloaded weights.
eval_input_fn: Input function for evaluation data.
eval_input_fn: Input function for evaluation data.
is_regression: Whether it is a regression task.
return_probs: Whether to return probabilities of classes.
return_probs: Whether to return probabilities of classes.
Returns:
Returns:
...
@@ -288,8 +290,11 @@ def get_predictions_and_labels(strategy,
...
@@ -288,8 +290,11 @@ def get_predictions_and_labels(strategy,
"""Replicated predictions."""
"""Replicated predictions."""
inputs
,
labels
=
inputs
inputs
,
labels
=
inputs
logits
=
trained_model
(
inputs
,
training
=
False
)
logits
=
trained_model
(
inputs
,
training
=
False
)
probabilities
=
tf
.
nn
.
softmax
(
logits
)
if
not
is_regression
:
return
probabilities
,
labels
probabilities
=
tf
.
nn
.
softmax
(
logits
)
return
probabilities
,
labels
else
:
return
logits
,
labels
outputs
,
labels
=
strategy
.
run
(
_test_step_fn
,
args
=
(
next
(
iterator
),))
outputs
,
labels
=
strategy
.
run
(
_test_step_fn
,
args
=
(
next
(
iterator
),))
# outputs: current batch logits as a tuple of shard logits
# outputs: current batch logits as a tuple of shard logits
...
@@ -447,9 +452,10 @@ def custom_main(custom_callbacks=None, custom_metrics=None):
...
@@ -447,9 +452,10 @@ def custom_main(custom_callbacks=None, custom_metrics=None):
include_sample_weights
=
include_sample_weights
)
include_sample_weights
=
include_sample_weights
)
if
FLAGS
.
mode
==
'predict'
:
if
FLAGS
.
mode
==
'predict'
:
num_labels
=
input_meta_data
.
get
(
'num_labels'
,
1
)
with
strategy
.
scope
():
with
strategy
.
scope
():
classifier_model
=
bert_models
.
classifier_model
(
classifier_model
=
bert_models
.
classifier_model
(
bert_config
,
input_meta_data
[
'
num_labels
'
]
)[
0
]
bert_config
,
num_labels
)[
0
]
checkpoint
=
tf
.
train
.
Checkpoint
(
model
=
classifier_model
)
checkpoint
=
tf
.
train
.
Checkpoint
(
model
=
classifier_model
)
latest_checkpoint_file
=
(
latest_checkpoint_file
=
(
FLAGS
.
predict_checkpoint_path
or
FLAGS
.
predict_checkpoint_path
or
...
@@ -460,7 +466,11 @@ def custom_main(custom_callbacks=None, custom_metrics=None):
...
@@ -460,7 +466,11 @@ def custom_main(custom_callbacks=None, custom_metrics=None):
checkpoint
.
restore
(
checkpoint
.
restore
(
latest_checkpoint_file
).
assert_existing_objects_matched
()
latest_checkpoint_file
).
assert_existing_objects_matched
()
preds
,
_
=
get_predictions_and_labels
(
preds
,
_
=
get_predictions_and_labels
(
strategy
,
classifier_model
,
eval_input_fn
,
return_probs
=
True
)
strategy
,
classifier_model
,
eval_input_fn
,
is_regression
=
(
num_labels
==
1
),
return_probs
=
True
)
output_predict_file
=
os
.
path
.
join
(
FLAGS
.
model_dir
,
'test_results.tsv'
)
output_predict_file
=
os
.
path
.
join
(
FLAGS
.
model_dir
,
'test_results.tsv'
)
with
tf
.
io
.
gfile
.
GFile
(
output_predict_file
,
'w'
)
as
writer
:
with
tf
.
io
.
gfile
.
GFile
(
output_predict_file
,
'w'
)
as
writer
:
logging
.
info
(
'***** Predict results *****'
)
logging
.
info
(
'***** Predict results *****'
)
...
...
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