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
chenpangpang
transformers
Commits
0094565f
Unverified
Commit
0094565f
authored
Jun 28, 2022
by
Matt
Committed by
GitHub
Jun 28, 2022
Browse files
Fix loss computation in TFBertForPreTraining (#17898)
parent
1dfa03f1
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
13 additions
and
22 deletions
+13
-22
src/transformers/models/bert/modeling_tf_bert.py
src/transformers/models/bert/modeling_tf_bert.py
+13
-22
No files found.
src/transformers/models/bert/modeling_tf_bert.py
View file @
0094565f
...
@@ -124,29 +124,20 @@ class TFBertPreTrainingLoss:
...
@@ -124,29 +124,20 @@ class TFBertPreTrainingLoss:
loss_fn
=
tf
.
keras
.
losses
.
SparseCategoricalCrossentropy
(
loss_fn
=
tf
.
keras
.
losses
.
SparseCategoricalCrossentropy
(
from_logits
=
True
,
reduction
=
tf
.
keras
.
losses
.
Reduction
.
NONE
from_logits
=
True
,
reduction
=
tf
.
keras
.
losses
.
Reduction
.
NONE
)
)
unmasked_lm_losses
=
loss_fn
(
y_true
=
labels
[
"labels"
],
y_pred
=
logits
[
0
])
# make sure only labels that are not equal to -100
# make sure only labels that are not equal to -100
# are taken into account as loss
# are taken into account for the loss computation
masked_lm_active_loss
=
tf
.
not_equal
(
tf
.
reshape
(
tensor
=
labels
[
"labels"
],
shape
=
(
-
1
,)),
-
100
)
lm_loss_mask
=
tf
.
cast
(
labels
[
"labels"
]
!=
-
100
,
dtype
=
unmasked_lm_losses
.
dtype
)
masked_lm_reduced_logits
=
tf
.
boolean_mask
(
lm_loss_denominator
=
tf
.
reduce_sum
(
lm_loss_mask
,
axis
=
1
)
tensor
=
tf
.
reshape
(
tensor
=
logits
[
0
],
shape
=
(
-
1
,
shape_list
(
logits
[
0
])[
2
])),
masked_lm_losses
=
tf
.
math
.
multiply_no_nan
(
unmasked_lm_losses
,
lm_loss_mask
)
mask
=
masked_lm_active_loss
,
reduced_masked_lm_loss
=
tf
.
reduce_sum
(
masked_lm_losses
,
axis
=
1
)
/
lm_loss_denominator
)
masked_lm_labels
=
tf
.
boolean_mask
(
unmasked_ns_loss
=
loss_fn
(
y_true
=
labels
[
"next_sentence_label"
],
y_pred
=
logits
[
1
])
tensor
=
tf
.
reshape
(
tensor
=
labels
[
"labels"
],
shape
=
(
-
1
,)),
mask
=
masked_lm_active_loss
ns_loss_mask
=
tf
.
cast
(
labels
[
"next_sentence_label"
]
!=
-
100
,
dtype
=
unmasked_ns_loss
.
dtype
)
)
# Just zero out samples where label is -100, no reduction
next_sentence_active_loss
=
tf
.
not_equal
(
tf
.
reshape
(
tensor
=
labels
[
"next_sentence_label"
],
shape
=
(
-
1
,)),
-
100
)
masked_ns_loss
=
tf
.
math
.
multiply_no_nan
(
unmasked_ns_loss
,
ns_loss_mask
)
next_sentence_reduced_logits
=
tf
.
boolean_mask
(
tensor
=
tf
.
reshape
(
tensor
=
logits
[
1
],
shape
=
(
-
1
,
2
)),
mask
=
next_sentence_active_loss
return
reduced_masked_lm_loss
+
masked_ns_loss
)
next_sentence_label
=
tf
.
boolean_mask
(
tensor
=
tf
.
reshape
(
tensor
=
labels
[
"next_sentence_label"
],
shape
=
(
-
1
,)),
mask
=
next_sentence_active_loss
)
masked_lm_loss
=
loss_fn
(
y_true
=
masked_lm_labels
,
y_pred
=
masked_lm_reduced_logits
)
next_sentence_loss
=
loss_fn
(
y_true
=
next_sentence_label
,
y_pred
=
next_sentence_reduced_logits
)
masked_lm_loss
=
tf
.
reshape
(
tensor
=
masked_lm_loss
,
shape
=
(
-
1
,
shape_list
(
next_sentence_loss
)[
0
]))
masked_lm_loss
=
tf
.
reduce_mean
(
input_tensor
=
masked_lm_loss
,
axis
=
0
)
return
masked_lm_loss
+
next_sentence_loss
class
TFBertEmbeddings
(
tf
.
keras
.
layers
.
Layer
):
class
TFBertEmbeddings
(
tf
.
keras
.
layers
.
Layer
):
...
...
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