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
66404fbb
Commit
66404fbb
authored
Apr 05, 2021
by
A. Unique TensorFlower
Browse files
Internal change
PiperOrigin-RevId: 366819679
parent
90e17531
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
20 additions
and
2 deletions
+20
-2
official/core/base_task.py
official/core/base_task.py
+8
-2
official/core/base_trainer_test.py
official/core/base_trainer_test.py
+12
-0
No files found.
official/core/base_task.py
View file @
66404fbb
...
@@ -217,8 +217,14 @@ class Task(tf.Module, metaclass=abc.ABCMeta):
...
@@ -217,8 +217,14 @@ class Task(tf.Module, metaclass=abc.ABCMeta):
with
tf
.
GradientTape
()
as
tape
:
with
tf
.
GradientTape
()
as
tape
:
outputs
=
model
(
features
,
training
=
True
)
outputs
=
model
(
features
,
training
=
True
)
# Computes per-replica loss.
# Computes per-replica loss.
loss
=
self
.
build_losses
(
if
model
.
compiled_loss
:
labels
=
labels
,
model_outputs
=
outputs
,
aux_losses
=
model
.
losses
)
loss
=
model
.
compiled_loss
(
labels
,
outputs
,
regularization_losses
=
model
.
losses
)
loss
+=
self
.
build_losses
(
labels
=
labels
,
model_outputs
=
outputs
,
aux_losses
=
None
)
else
:
loss
=
self
.
build_losses
(
labels
=
labels
,
model_outputs
=
outputs
,
aux_losses
=
model
.
losses
)
# Scales loss as the default gradients allreduce performs sum inside the
# Scales loss as the default gradients allreduce performs sum inside the
# optimizer.
# optimizer.
scaled_loss
=
loss
/
tf
.
distribute
.
get_strategy
().
num_replicas_in_sync
scaled_loss
=
loss
/
tf
.
distribute
.
get_strategy
().
num_replicas_in_sync
...
...
official/core/base_trainer_test.py
View file @
66404fbb
...
@@ -368,6 +368,18 @@ class TrainerTest(tf.test.TestCase, parameterized.TestCase):
...
@@ -368,6 +368,18 @@ class TrainerTest(tf.test.TestCase, parameterized.TestCase):
with
self
.
assertRaises
(
RuntimeError
):
with
self
.
assertRaises
(
RuntimeError
):
_
=
trainer
.
train
(
tf
.
convert_to_tensor
(
2
,
dtype
=
tf
.
int32
))
_
=
trainer
.
train
(
tf
.
convert_to_tensor
(
2
,
dtype
=
tf
.
int32
))
def
test_model_with_compiled_loss
(
self
):
task
=
mock_task
.
MockTask
()
model
=
task
.
build_model
()
model
.
compile
(
loss
=
tf
.
keras
.
losses
.
CategoricalCrossentropy
())
trainer
=
trainer_lib
.
Trainer
(
self
.
_config
,
task
,
model
=
model
,
optimizer
=
task
.
create_optimizer
(
self
.
_config
.
trainer
.
optimizer_config
))
logs
=
trainer
.
train
(
tf
.
convert_to_tensor
(
5
,
dtype
=
tf
.
int32
))
self
.
assertIn
(
'training_loss'
,
logs
)
if
__name__
==
'__main__'
:
if
__name__
==
'__main__'
:
tf
.
test
.
main
()
tf
.
test
.
main
()
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