Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
chenpangpang
transformers
Commits
63e36007
Commit
63e36007
authored
Dec 10, 2019
by
Morgan Funtowicz
Browse files
Make sure padding, cls and another non-context tokens cannot appear in the answer.
parent
40a39ab6
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
8 additions
and
4 deletions
+8
-4
transformers/pipelines.py
transformers/pipelines.py
+8
-4
No files found.
transformers/pipelines.py
View file @
63e36007
...
@@ -188,14 +188,18 @@ class QuestionAnsweringPipeline(Pipeline):
...
@@ -188,14 +188,18 @@ class QuestionAnsweringPipeline(Pipeline):
start
,
end
=
start
.
cpu
().
numpy
(),
end
.
cpu
().
numpy
()
start
,
end
=
start
.
cpu
().
numpy
(),
end
.
cpu
().
numpy
()
answers
=
[]
answers
=
[]
for
i
,
(
example
,
feature
,
start_
,
end_
)
in
enumerate
(
zip
(
texts
,
features
,
start
,
end
)):
for
(
example
,
feature
,
start_
,
end_
)
in
zip
(
texts
,
features
,
start
,
end
):
start_
,
end_
=
start_
*
np
.
abs
(
np
.
array
(
feature
.
p_mask
)
-
1
),
end_
*
np
.
abs
(
np
.
array
(
feature
.
p_mask
)
-
1
)
# Normalize logits and spans to retrieve the answer
# Normalize logits and spans to retrieve the answer
start_
=
np
.
exp
(
start_
)
/
np
.
sum
(
np
.
exp
(
start_
))
start_
=
np
.
exp
(
start_
)
/
np
.
sum
(
np
.
exp
(
start_
))
end_
=
np
.
exp
(
end_
)
/
np
.
sum
(
np
.
exp
(
end_
))
end_
=
np
.
exp
(
end_
)
/
np
.
sum
(
np
.
exp
(
end_
))
starts
,
ends
,
scores
=
self
.
decode
(
start_
,
end_
,
kwargs
[
'topk'
],
kwargs
[
'max_answer_len'
])
# Mask padding and question
start_
,
end_
=
start_
*
np
.
abs
(
np
.
array
(
feature
.
p_mask
)
-
1
),
end_
*
np
.
abs
(
np
.
array
(
feature
.
p_mask
)
-
1
)
# Mask CLS
start_
[
0
]
=
end_
[
0
]
=
0
starts
,
ends
,
scores
=
self
.
decode
(
start_
,
end_
,
kwargs
[
'topk'
],
kwargs
[
'max_answer_len'
])
char_to_word
=
np
.
array
(
example
.
char_to_word_offset
)
char_to_word
=
np
.
array
(
example
.
char_to_word_offset
)
# Convert the answer (tokens) back to the original text
# Convert the answer (tokens) back to the original text
...
...
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