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
0206efb4
Unverified
Commit
0206efb4
authored
Jul 28, 2020
by
Sylvain Gugger
Committed by
GitHub
Jul 28, 2020
Browse files
Make all data collators accept dict (#6065)
* Make all data collators accept dict * Style
parent
31a5486e
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
7 additions
and
3 deletions
+7
-3
src/transformers/data/data_collator.py
src/transformers/data/data_collator.py
+7
-3
No files found.
src/transformers/data/data_collator.py
View file @
0206efb4
from
dataclasses
import
dataclass
from
dataclasses
import
dataclass
from
typing
import
Any
,
Callable
,
Dict
,
List
,
NewType
,
Tuple
from
typing
import
Any
,
Callable
,
Dict
,
List
,
NewType
,
Tuple
,
Union
import
torch
import
torch
from
torch.nn.utils.rnn
import
pad_sequence
from
torch.nn.utils.rnn
import
pad_sequence
...
@@ -77,7 +77,9 @@ class DataCollatorForLanguageModeling:
...
@@ -77,7 +77,9 @@ class DataCollatorForLanguageModeling:
mlm
:
bool
=
True
mlm
:
bool
=
True
mlm_probability
:
float
=
0.15
mlm_probability
:
float
=
0.15
def
__call__
(
self
,
examples
:
List
[
torch
.
Tensor
])
->
Dict
[
str
,
torch
.
Tensor
]:
def
__call__
(
self
,
examples
:
List
[
Union
[
torch
.
Tensor
,
Dict
[
str
,
torch
.
Tensor
]]])
->
Dict
[
str
,
torch
.
Tensor
]:
if
isinstance
(
examples
[
0
],
dict
):
examples
=
[
e
[
"input_ids"
]
for
e
in
examples
]
batch
=
self
.
_tensorize_batch
(
examples
)
batch
=
self
.
_tensorize_batch
(
examples
)
if
self
.
mlm
:
if
self
.
mlm
:
inputs
,
labels
=
self
.
mask_tokens
(
batch
)
inputs
,
labels
=
self
.
mask_tokens
(
batch
)
...
@@ -148,7 +150,9 @@ class DataCollatorForPermutationLanguageModeling:
...
@@ -148,7 +150,9 @@ class DataCollatorForPermutationLanguageModeling:
plm_probability
:
float
=
1
/
6
plm_probability
:
float
=
1
/
6
max_span_length
:
int
=
5
# maximum length of a span of masked tokens
max_span_length
:
int
=
5
# maximum length of a span of masked tokens
def
__call__
(
self
,
examples
:
List
[
torch
.
Tensor
])
->
Dict
[
str
,
torch
.
Tensor
]:
def
__call__
(
self
,
examples
:
List
[
Union
[
torch
.
Tensor
,
Dict
[
str
,
torch
.
Tensor
]]])
->
Dict
[
str
,
torch
.
Tensor
]:
if
isinstance
(
examples
[
0
],
dict
):
examples
=
[
e
[
"input_ids"
]
for
e
in
examples
]
batch
=
self
.
_tensorize_batch
(
examples
)
batch
=
self
.
_tensorize_batch
(
examples
)
inputs
,
perm_mask
,
target_mapping
,
labels
=
self
.
mask_tokens
(
batch
)
inputs
,
perm_mask
,
target_mapping
,
labels
=
self
.
mask_tokens
(
batch
)
return
{
"input_ids"
:
inputs
,
"perm_mask"
:
perm_mask
,
"target_mapping"
:
target_mapping
,
"labels"
:
labels
}
return
{
"input_ids"
:
inputs
,
"perm_mask"
:
perm_mask
,
"target_mapping"
:
target_mapping
,
"labels"
:
labels
}
...
...
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