You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Commit b430d76
Browse filesBrowse the repository at this point in the historyBrowse files
Copy file name to clipboardExpand all lines: textattack/datasets/dataset.py
+33-16Lines changed: 33 additions & 16 deletions
Original file line number
Diff line number
Diff line change
@@ -9,19 +9,28 @@ class Dataset(torch.utils.data.Dataset):
9
9
data via :meth:`__getitem__` and :meth:`__len__` methods.
10
10
11
11
Args:
12
-
dataset (list_like): A list-like iterable of ``(input, output)`` pairs. Here, `output` can either be an integer representing labels for classification
13
-
or a string for seq2seq tasks. If input consists of multiple sequences (e.g. SNLI), iterable should be of the form ``([input_1, input_2, ...], output)`` and ``input_columns`` parameter must be set.
14
-
input_columns (:obj:`list[str]`, `optional`, defaults to :obj:`["text"]`):
12
+
dataset (:obj:`list[tuple]`):
13
+
A list of :obj:`(input, output)` pairs.
14
+
If :obj:`input` consists of multiple fields (e.g. "premise" and "hypothesis" for SNLI),
15
+
:obj:`input` must be of the form :obj:`(input_1, input_2, ...)` and :obj:`input_columns` parameter must be set.
16
+
:obj:`output` can either be an integer representing labels for classification or a string for seq2seq tasks.
17
+
input_columns (:obj:`list[str]`, `optional`, defaults to :obj:`["text"]`):
15
18
List of column names of inputs in order.
16
19
label_map (:obj:`dict[int, int]`, `optional`, defaults to :obj:`None`):
17
-
Mapping if output labels of the dataset should be re-mapped. Useful if model was trained with a different label arrangement than
18
-
provided in the ``datasets`` version of the dataset. For example, if dataset's arrangement is 0 for negative and 1 for positive, but model's label
19
-
arrangement is 1 for negative and 0 for positive, pass ``{0: 1, 1: 0}``. Could also be used to remap literal labels to numerical labels,
20
-
(e.g. ``{"positive": 1, "negative": 0}``)
21
-
label_names (list[str], optional): List of label names in corresponding order (e.g. ``["World", "Sports", "Business", "Sci/Tech"] for AG-News dataset).
22
-
If not set, labels will printed as is (e.g. "0", "1", ...). This should be set to ``None`` for non-classification datasets.
23
-
output_scale_factor (float): Factor to divide ground-truth outputs by. Generally, TextAttack goal functions require model outputs between 0 and 1. Some datasets are regression tasks, in which case this is necessary.
24
-
shuffle (bool): Whether to shuffle the dataset on load.
20
+
Mapping if output labels of the dataset should be re-mapped. Useful if model was trained with a different label arrangement.
21
+
For example, if dataset's arrangement is 0 for `Negative` and 1 for `Positive`, but model's label
22
+
arrangement is 1 for `Negative` and 0 for `Positive`, passing :obj:`{0: 1, 1: 0}` will remap the dataset's label to match with model's arrangements.
23
+
Could also be used to remap literal labels to numerical labels (e.g. :obj:`{"positive": 1, "negative": 0}`).
24
+
label_names (:obj:`list[str]`, `optional`, defaults to :obj:`None`):
25
+
List of label names in corresponding order (e.g. :obj:`["World", "Sports", "Business", "Sci/Tech"]` for AG-News dataset).
26
+
If not set, labels will printed as is (e.g. "0", "1", ...). This should be set to :obj:`None` for non-classification datasets.
27
+
output_scale_factor (:obj:`float`, `optional`, defaults to :obj:`None`):
28
+
Factor to divide ground-truth outputs by. Generally, TextAttack goal functions require model outputs between 0 and 1.
29
+
Some datasets are regression tasks, in which case this is necessary.
30
+
shuffle (:obj:`bool`, `optional`, defaults to :obj:`False`): Whether to shuffle the underlying dataset.
31
+
32
+
.. note::
33
+
Generally not recommended to shuffle the underlying dataset. Shuffling can be performed using DataLoader or by shuffling the order of indices we attack.
25
34
26
35
Examples::
27
36
@@ -93,13 +102,21 @@ def shuffle(self):
93
102
random.shuffle(self._dataset)
94
103
self.shuffled=True
95
104
96
-
def__getitem__(self, i):
97
-
ifisinstance(i, int):
98
-
returnself._format_as_dict(self._dataset[i])
105
+
def__getitem__(self, idx):
106
+
"""Retrieves the specified item from dataset.
107
+
108
+
Args:
109
+
idx (:obj:`int` or :obj:`slice`): Index or slice.
110
+
Returns:
111
+
:obj:`tuple[OrderedDict, Union[int, str]]` - Tuple of input as :obj:`collections.OrderedDict` and output as :obj:`int` or :obj:`str`.
112
+
"""
113
+
ifisinstance(idx, int):
114
+
returnself._format_as_dict(self._dataset[idx])
99
115
else:
100
-
# `i` could be a slice or an integer. if it's a slice,
116
+
# `idx` could be a slice or an integer. if it's a slice,
101
117
# return the formatted version of the proper slice of the list
Copy file name to clipboardExpand all lines: textattack/datasets/huggingface_dataset.py
+37-19Lines changed: 37 additions & 19 deletions
Original file line number
Diff line number
Diff line change
@@ -57,27 +57,38 @@ class HuggingFaceDataset(Dataset):
57
57
"""Loads a dataset from 🤗 Datasets and prepares it as a TextAttack dataset.
58
58
59
59
Args:
60
-
name_or_dataset (Union[datasets.Dataset, str]): the dataset name or actual ``datasets.Dataset`` object. If it's your custom ``datasets.Dataset`` object, please pass the input and output columns via ``dataset_columns`` argument.
61
-
subset (:obj:`str`, optional, defaults to :obj:`None`): the subset of the main dataset. Dataset will be loaded as ``datasets.load_dataset(name, subset)``. Default is ``None``.
62
-
split (str, optioanl): the split of the dataset. Default is "train".
63
-
dataset_columns (tuple(list[str], str)), optional): Pair of ``list[str]`` representing list of input column names (e.g. ["premise", "hypothesis"]) and ``str`` representing the output column name (e.g. ``label``). If not set, we will try to automatically determine column names from known designs.
64
-
label_map (dict, optional): Mapping if output labels of the dataset should be re-mapped. Useful if model was trained with a different label arrangement than
65
-
provided in the ``datasets`` version of the dataset. For example, if dataset's arrangement is 0 for negative and 1 for positive, but model's label
66
-
arrangement is 1 for negative and 0 for positive, pass ``{0: 1, 1: 0}``. Could also be used to remap literal labels to numerical labels,
67
-
(e.g. ``{"positive": 1, "negative": 0}``)
68
-
label_names (list[str], optional): List of label names in corresponding order (e.g. ``["World", "Sports", "Business", "Sci/Tech"] for AG-News dataset).
69
-
If ``datasets.Dataset`` object already has label names, then this is not required. Also, this should be set to ``None`` for non-classification datasets.
70
-
output_scale_factor (float): Factor to divide ground-truth outputs by.
71
-
Generally, TextAttack goal functions require model outputs between 0 and 1.
The dataset name as :obj:`str` or actual :obj:`datasets.Dataset` object.
62
+
If it's your custom :obj:`datasets.Dataset` object, please pass the input and output columns via :obj:`dataset_columns` argument.
63
+
subset (:obj:`str`, `optional`, defaults to :obj:`None`):
64
+
The subset of the main dataset. Dataset will be loaded as :obj:`datasets.load_dataset(name, subset)`.
65
+
split (:obj:`str`, `optional`, defaults to :obj:`"train"`):
66
+
The split of the dataset.
67
+
dataset_columns (:obj:`tuple(list[str], str))`, `optional`, defaults to :obj:`None`):
68
+
Pair of :obj:`list[str]` representing list of input column names (e.g. :obj:`["premise", "hypothesis"]`)
69
+
and :obj:`str` representing the output column name (e.g. :obj:`label`). If not set, we will try to automatically determine column names from known designs.
70
+
label_map (:obj:`dict[int, int]`, `optional`, defaults to :obj:`None`):
71
+
Mapping if output labels of the dataset should be re-mapped. Useful if model was trained with a different label arrangement.
72
+
For example, if dataset's arrangement is 0 for `Negative` and 1 for `Positive`, but model's label
73
+
arrangement is 1 for `Negative` and 0 for `Positive`, passing :obj:`{0: 1, 1: 0}` will remap the dataset's label to match with model's arrangements.
74
+
Could also be used to remap literal labels to numerical labels (e.g. :obj:`{"positive": 1, "negative": 0}`).
75
+
label_names (:obj:`list[str]`, `optional`, defaults to :obj:`None`):
76
+
List of label names in corresponding order (e.g. :obj:`["World", "Sports", "Business", "Sci/Tech"]` for AG-News dataset).
77
+
If not set, labels will printed as is (e.g. "0", "1", ...). This should be set to :obj:`None` for non-classification datasets.
78
+
output_scale_factor (:obj:`float`, `optional`, defaults to :obj:`None`):
79
+
Factor to divide ground-truth outputs by. Generally, TextAttack goal functions require model outputs between 0 and 1.
72
80
Some datasets are regression tasks, in which case this is necessary.
73
-
shuffle (bool): Whether to shuffle the dataset on load.
81
+
shuffle (:obj:`bool`, `optional`, defaults to :obj:`False`): Whether to shuffle the underlying dataset.
82
+
83
+
.. note::
84
+
Generally not recommended to shuffle the underlying dataset. Shuffling can be performed using DataLoader or by shuffling the order of indices we attack.
0 commit comments