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
71f71ddb
Commit
71f71ddb
authored
Oct 29, 2019
by
VictorSanh
Committed by
Lysandre Debut
Nov 27, 2019
Browse files
run_xnli + utils_xnli
parent
b5d884d2
Changes
2
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
627 additions
and
0 deletions
+627
-0
examples/run_xnli.py
examples/run_xnli.py
+534
-0
examples/utils_xnli.py
examples/utils_xnli.py
+93
-0
No files found.
examples/run_xnli.py
0 → 100644
View file @
71f71ddb
This diff is collapsed.
Click to expand it.
examples/utils_xnli.py
0 → 100644
View file @
71f71ddb
# coding=utf-8
# Copyright 2018 The Google AI Language Team Authors and The HuggingFace Inc. team.
# Copyright (c) 2018, NVIDIA CORPORATION. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
""" XNLI utils (dataset loading and evaluation) """
from
__future__
import
absolute_import
,
division
,
print_function
import
logging
import
os
from
transformers.data.processors
import
DataProcessor
,
InputExample
from
transformers.data.metrics
import
simple_accuracy
logger
=
logging
.
getLogger
(
__name__
)
class
XnliProcessor
(
DataProcessor
):
"""Processor for the XNLI dataset.
Adapted from https://github.com/google-research/bert/blob/f39e881b169b9d53bea03d2d341b31707a6c052b/run_classifier.py#L207"""
def
__init__
(
self
,
language
,
train_language
=
None
):
self
.
language
=
language
self
.
train_language
=
train_language
def
get_train_examples
(
self
,
data_dir
):
"""See base class."""
lg
=
self
.
language
if
self
.
train_language
is
None
else
self
.
train_language
lines
=
self
.
_read_tsv
(
os
.
path
.
join
(
data_dir
,
f
"XNLI-MT-1.0/multinli/multinli.train.
{
lg
}
.tsv"
))
examples
=
[]
for
(
i
,
line
)
in
enumerate
(
lines
):
if
i
==
0
:
continue
guid
=
"%s-%s"
%
(
'train'
,
i
)
text_a
=
line
[
0
]
text_b
=
line
[
1
]
label
=
"contradiction"
if
line
[
2
]
==
"contradictory"
else
line
[
2
]
assert
isinstance
(
text_a
,
str
)
and
isinstance
(
text_b
,
str
)
and
isinstance
(
label
,
str
)
examples
.
append
(
InputExample
(
guid
=
guid
,
text_a
=
text_a
,
text_b
=
text_b
,
label
=
label
))
return
examples
def
get_dev_examples
(
self
,
data_dir
):
"""See base class."""
lines
=
self
.
_read_tsv
(
os
.
path
.
join
(
data_dir
,
"XNLI-1.0/xnli.dev.tsv"
))
examples
=
[]
for
(
i
,
line
)
in
enumerate
(
lines
):
if
i
==
0
:
continue
language
=
line
[
0
]
if
language
!=
self
.
language
:
continue
guid
=
"%s-%s"
%
(
'dev'
,
i
)
text_a
=
line
[
6
]
text_b
=
line
[
7
]
label
=
line
[
1
]
assert
isinstance
(
text_a
,
str
)
and
isinstance
(
text_b
,
str
)
and
isinstance
(
label
,
str
)
examples
.
append
(
InputExample
(
guid
=
guid
,
text_a
=
text_a
,
text_b
=
text_b
,
label
=
label
))
return
examples
def
get_labels
(
self
):
"""See base class."""
return
[
"contradiction"
,
"entailment"
,
"neutral"
]
def
xnli_compute_metrics
(
task_name
,
preds
,
labels
):
assert
len
(
preds
)
==
len
(
labels
)
if
task_name
==
"xnli"
:
return
{
"acc"
:
simple_accuracy
(
preds
,
labels
)}
else
:
raise
ValueError
(
f
'
{
task_name
}
is not a supported task.'
)
xnli_processors
=
{
"xnli"
:
XnliProcessor
,
}
xnli_output_modes
=
{
"xnli"
:
"classification"
,
}
xnli_tasks_num_labels
=
{
"xnli"
:
3
,
}
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