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
3ebf1a13
"python/vscode:/vscode.git/clone" did not exist on "f8260f25391fcd68e2fe82324d5b4b970aa76b60"
Commit
3ebf1a13
authored
Nov 02, 2018
by
VictorSanh
Browse files
Fix loss computation for indexes bigger than max_seq_length.
parent
629bd006
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
16 additions
and
3 deletions
+16
-3
modeling_pytorch.py
modeling_pytorch.py
+16
-3
No files found.
modeling_pytorch.py
View file @
3ebf1a13
...
@@ -485,9 +485,22 @@ class BertForQuestionAnswering(nn.Module):
...
@@ -485,9 +485,22 @@ class BertForQuestionAnswering(nn.Module):
start_logits
,
end_logits
=
logits
.
split
(
1
,
dim
=-
1
)
start_logits
,
end_logits
=
logits
.
split
(
1
,
dim
=-
1
)
if
start_positions
is
not
None
and
end_positions
is
not
None
:
if
start_positions
is
not
None
and
end_positions
is
not
None
:
loss_fct
=
CrossEntropyLoss
()
#loss_fct = CrossEntropyLoss()
start_loss
=
loss_fct
(
start_logits
,
start_positions
)
#start_loss = loss_fct(start_logits, start_positions)
end_loss
=
loss_fct
(
end_logits
,
end_positions
)
#end_loss = loss_fct(end_logits, end_positions)
batch_size
,
seq_length
=
input_ids
.
size
()
def
compute_loss
(
logits
,
positions
):
max_position
=
positions
.
max
().
item
()
one_hot
=
torch
.
FloatTensor
(
batch_size
,
max
(
max_position
,
seq_length
)
+
1
).
zero_
()
one_hot
=
one_hot
.
scatter
(
1
,
positions
,
1
)
one_hot
=
one_hot
[:,
:
seq_length
]
log_probs
=
nn
.
functional
.
log_softmax
(
logits
,
dim
=
-
1
).
view
(
batch_size
,
seq_length
)
loss
=
-
torch
.
mean
(
torch
.
sum
(
one_hot
*
log_probs
),
dim
=
-
1
)
return
loss
start_loss
=
compute_loss
(
start_logits
,
start_positions
)
end_loss
=
compute_loss
(
end_logits
,
end_positions
)
total_loss
=
(
start_loss
+
end_loss
)
/
2
total_loss
=
(
start_loss
+
end_loss
)
/
2
return
total_loss
,
(
start_logits
,
end_logits
)
return
total_loss
,
(
start_logits
,
end_logits
)
else
:
else
:
...
...
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