-
Notifications
You must be signed in to change notification settings - Fork 0
/
arguments.py
257 lines (243 loc) · 8.99 KB
/
arguments.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
import math
import os
from dataclasses import dataclass, field
from typing import Optional, Union, List, Dict, Tuple
import torch
from transformers.file_utils import cached_property, torch_required, is_torch_tpu_available
from transformers import TrainingArguments, MODEL_FOR_MASKED_LM_MAPPING
MODEL_CONFIG_CLASSES = list(MODEL_FOR_MASKED_LM_MAPPING.keys())
MODEL_TYPES = tuple(conf.model_type for conf in MODEL_CONFIG_CLASSES)
@dataclass
class ModelArguments:
"""
Arguments pertaining to which model/config/tokenizer we are going to fine-tune, or train from scratch.
"""
# Huggingface's original arguments
model_name_or_path: str = field(
default=None,
metadata={
"help": "The model checkpoint for weights initialization."
"Don't set if you want to train a model from scratch."
},
)
model_type: Optional[str] = field(
default=None,
metadata={"help": "If training from scratch, pass a model type from the list: " + ", ".join(MODEL_TYPES)},
)
config_name: Optional[str] = field(
default=None, metadata={"help": "Pretrained config name or path if not the same as model_name"}
)
tokenizer_name: Optional[str] = field(
default=None, metadata={"help": "Pretrained tokenizer name or path if not the same as model_name"}
)
cache_dir: Optional[str] = field(
default=None,
metadata={"help": "Where do you want to store the pretrained models downloaded from huggingface.co"},
)
use_fast_tokenizer: bool = field(
default=True,
metadata={"help": "Whether to use one of the fast tokenizer (backed by the tokenizers library) or not."},
)
model_revision: str = field(
default="main",
metadata={"help": "The specific model version to use (can be a branch name, tag name or commit id)."},
)
use_auth_token: bool = field(
default=False,
metadata={
"help": "Will use the token generated when running `transformers-cli login` (necessary to use this script "
"with private models)."
},
)
# SimCSE's arguments
temp: float = field(
default=0.05,
metadata={
"help": "Temperature for softmax."
}
)
pooler_type: str = field(
default="cls",
metadata={
"help": "What kind of pooler to use (cls, cls_before_pooler, avg, avg_top2, avg_first_last)."
}
)
hard_negative_weight: float = field(
default=0,
metadata={
"help": "The **logit** of weight for hard negatives (only effective if hard negatives are used)."
}
)
do_mlm: bool = field(
default=False,
metadata={
"help": "Whether to use MLM auxiliary objective."
}
)
mlm_weight: float = field(
default=0.1,
metadata={
"help": "Weight for MLM auxiliary objective (only effective if --do_mlm)."
}
)
mlp_only_train: bool = field(
default=False,
metadata={
"help": "Use MLP only during training"
}
)
# SCAN Args
entropy_weight: float = field(
default=2,
metadata={
"help": "Temperature for softmax."
}
)
n_heads: int = field(
default=2,
metadata={
"help": "No of Heads in Scan Model"
}
)
n_clusters: int = field(
default=16,
metadata={
"help": "No of Classes in Dataset"
}
)
pretrained_weight_path: str = field(
default=None,
metadata={
"help": "Path of model weights finetuned with SimCSE Loss"
},
)
@dataclass
class DataTrainingArguments:
"""
Arguments pertaining to what data we are going to input our model for training and eval.
"""
# Huggingface's original arguments.
dataset_name: Optional[str] = field(
default=None, metadata={"help": "The name of the dataset to use (via the datasets library)."}
)
dataset_config_name: Optional[str] = field(
default=None, metadata={"help": "The configuration name of the dataset to use (via the datasets library)."}
)
overwrite_cache: bool = field(
default=False, metadata={"help": "Overwrite the cached training and evaluation sets"}
)
validation_split_percentage: Optional[int] = field(
default=5,
metadata={
"help": "The percentage of the train set used as validation set in case there's no validation split"
},
)
preprocessing_num_workers: Optional[int] = field(
default=None,
metadata={"help": "The number of processes to use for the preprocessing."},
)
# SimCSE's arguments
batch_size: int = field(
default=16, metadata={"help": "Batch size for training."}
)
best_eval_metric: Optional[float] = field(
default=math.inf, metadata={"help": "The best metric value to use for early stopping."}
)
train_file: Optional[str] = field(
default=None,
metadata={"help": "The training data file (.txt or .csv)."}
)
test_file: Optional[str] = field(
default=None,
metadata={"help": "The test data file (.txt or .csv)."}
)
validation_file: Optional[str] = field(
default=None,
metadata={"help": "The validation data file (.txt or .csv)."}
)
max_seq_length: Optional[int] = field(
default=32,
metadata={
"help": "The maximum total input sequence length after tokenization. Sequences longer "
"than this will be truncated."
},
)
pad_to_max_length: bool = field(
default=False,
metadata={
"help": "Whether to pad all samples to `max_seq_length`. "
"If False, will pad the samples dynamically when batching to the maximum length in the batch."
},
)
mlm_probability: float = field(
default=0.15,
metadata={"help": "Ratio of tokens to mask for MLM (only effective if --do_mlm)"}
)
# SCAN Args
use_augmentation: bool = field(
default=True, metadata={"help": "Whether to use data augmentation or not during training."}
)
nbr_prefix: Optional[str] = field(
default="nbr_",
metadata={"help": "Prefix wit which column names of neighbour data start with."}
)
text_col: Optional[str] = field(
default="text",
metadata={"help": "Prefix wit which column names of neighbour data start with."}
)
aug_prefix: Optional[str] = field(
default="aug_",
metadata={"help": "Prefix wit which column names of augmented data start with."}
)
num_nbr: int = field(
default=5,
metadata={"help": "Number of neighbours to use during training.)"}
)
aug_dataset_name: Optional[str] = field(
default=None, metadata={"help": "The name of the augmented dataset to use."}
)
aug_train_file: Optional[str] = field(
default=None,
metadata={"help": "The augmented training data file (.txt or .csv)."}
)
aug_test_file: Optional[str] = field(
default=None,
metadata={"help": "The augmented test data file (.txt or .csv)."}
)
aug_validation_file: Optional[str] = field(
default=None,
metadata={"help": "The augmented validation data file (.txt or .csv)."}
)
def __post_init__(self):
if self.dataset_name is None and self.train_file is None and self.validation_file is None:
raise ValueError("Need either a dataset name or a training/validation file.")
else:
if self.train_file is not None:
extension = self.train_file.split(".")[-1]
assert extension in ["csv", "json", "txt"], "`train_file` should be a csv, a json or a txt file."
if self.use_augmentation:
if self.aug_dataset_name is None and self.aug_train_file is None and self.aug_validation_file is None:
raise ValueError("Need either a dataset name or a training/validation file.")
else:
if self.aug_train_file is not None:
extension = self.aug_train_file.split(".")[-1]
assert extension in ["csv", "json", "txt"], "`train_file` should be a csv, a json or a txt file."
@dataclass
class OurTrainingArguments(TrainingArguments):
# Evaluation
## By default, we evaluate STS (dev) during training (for selecting best checkpoints) and evaluate
## both STS and transfer tasks (dev) at the end of training. Using --eval_transfer will allow evaluating
## both STS and transfer tasks (dev) during training.
update_cluster_head_only: bool = field(
default=False, metadata={"help": "Whether to update complete model."}
)
keep_eval_mode: bool = field(
default=False,
metadata={
"help": "Keep in eval mode during training, to use no dropout"
}
)
eval_transfer: bool = field(
default=False,
metadata={"help": "Evaluate transfer task dev sets (in validation)."}
)