File size: 8,681 Bytes
e9066e1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/usr/bin/env python
# coding: utf-8

# In[7]:


from dataclasses import dataclass, field
from datetime import datetime
from typing import List, Optional
from transformers.file_utils import ExplicitEnum

task_to_keys = {
    "mimic3-50": ("mimic3-50"),
    "mimic3-full": ("mimic3-full"),
}

class TransformerLayerUpdateStrategy(ExplicitEnum):
    NO = "no"
    LAST = "last"
    ALL = "all"
    
class DocumentPoolingStrategy(ExplicitEnum):
    FLAT = "flat"
    MAX = "max"
    MEAN = "mean"


@dataclass
class DataTrainingArguments:
    """
    Arguments pertaining to what data we are going to input our model for training and eval.

    Using `HfArgumentParser` we can turn this class
    into argparse arguments to be able to specify them on
    the command line.
    """

    task_name: Optional[str] = field(
        default=None,
        metadata={"help": "The name of the task to train on: " + ", ".join(task_to_keys.keys())},
    )
    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)."}
    )
    max_seq_length: int = field(
        default=128,
        metadata={
            "help": "The maximum total input sequence length after tokenization. Sequences longer "
                    "than this will be truncated, sequences shorter will be padded."
        },
    )
    overwrite_cache: bool = field(
        default=False, metadata={"help": "Overwrite the cached preprocessed datasets or not."}
    )
    pad_to_max_length: bool = field(
        default=True,
        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."
        },
    )
    max_train_samples: Optional[int] = field(
        default=None,
        metadata={
            "help": "For debugging purposes or quicker training, truncate the number of training examples to this "
                    "value if set."
        },
    )
    max_eval_samples: Optional[int] = field(
        default=None,
        metadata={
            "help": "For debugging purposes or quicker training, truncate the number of evaluation examples to this "
                    "value if set."
        },
    )
    max_predict_samples: Optional[int] = field(
        default=None,
        metadata={
            "help": "For debugging purposes or quicker training, truncate the number of prediction examples to this "
                    "value if set."
        },
    )
    train_file: Optional[str] = field(
        default=None, metadata={"help": "A csv or a json file containing the training data."}
    )
    validation_file: Optional[str] = field(
        default=None, metadata={"help": "A csv or a json file containing the validation data."}
    )
    test_file: Optional[str] = field(default=None, metadata={"help": "A csv or a json file containing the test data."})

    # customized data arguments
    label_dictionary_file: Optional[str] = field(
        default=None, metadata={"help": "The name of the test data file."}
    )
    code_max_seq_length: int = field(
        default=128,
        metadata={
            "help": "The maximum total input sequence length after tokenization for code long titles"
        },
    )
    code_batch_size: int = field(
        default=8,
        metadata={
            "help": "The batch size for generating code representation"
        },
    )
    ignore_keys_for_eval: Optional[List[str]] = field(
        default=None, metadata={"help": "The list of keys to be ignored during evaluation process."}
    )
    use_cached_datasets: bool = field(
        default=True,
        metadata={"help": "if use cached datasets to save preprocessing time. The cached datasets were preprocessed "
                          "and saved into data folder."})
    data_segmented: bool = field(
        default=False,
        metadata={"help": "if dataset is segmented or not"})

    lazy_loading: bool = field(
        default=False,
        metadata={"help": "if dataset is larger than 500MB, please use lazy_loading"})

    def __post_init__(self):
        if self.task_name is not None:
            self.task_name = self.task_name.lower()
            if self.task_name not in task_to_keys.keys():
                raise ValueError("Unknown task, you should pick one in " + ",".join(task_to_keys.keys()))
        elif self.dataset_name is not None:
            pass
        elif self.train_file is None or self.validation_file is None:
            raise ValueError("Need a training/validation file")
        elif self.label_dictionary_file is None:
            raise ValueError("label dictionary must be provided")
        else:
            train_extension = self.train_file.split(".")[-1]
            assert train_extension in ["csv", "json"], "`train_file` should be a csv or a json file."
            validation_extension = self.validation_file.split(".")[-1]
            assert (
                    validation_extension == train_extension
            ), "`validation_file` should have the same extension (csv or json) as `train_file`."


@dataclass
class ModelArguments:
    """
    Arguments pertaining to which model/config/tokenizer we are going to fine-tune from.
    """

    model_name_or_path: str = field(
        metadata={"help": "Path to pretrained model or model identifier from huggingface.co/models"}
    )
    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)."
        },
    )
    # Customized model arguments
    d_model: int = field(default=768, metadata={"help": "hidden size of model. should be the same as base transformer "
                                                        "model"})
    dropout: float = field(default=0.1, metadata={"help": "Dropout of transformer layer"})
    dropout_att: float = field(default=0.1, metadata={"help": "Dropout of label-wise attention layer"})
    num_chunks_per_document: int = field(default=0.1, metadata={"help": "Num of chunks per document"})
    transformer_layer_update_strategy: TransformerLayerUpdateStrategy = field(
        default="all",
        metadata={"help": "Update which transformer layers when training"})
    use_code_representation: bool = field(
        default=True,
        metadata={"help": "if use code representation as the "
                          "initial parameters of code vectors in attention layer"})
    multi_head_attention: bool = field(
        default=True,
        metadata={"help": "if use multi head attention for different chunks"})
    chunk_attention: bool = field(
        default=True,
        metadata={"help": "if use chunk attention for each label"})

    multi_head_chunk_attention: bool = field(
        default=True,
        metadata={"help": "if use multi head chunk attention for each label"})

    num_hidden_layers: int = field(
        default=2, metadata={"help": "NUm of hidden layers in longformer"}
    )

    linear_init_mean: float = field(default=0.0, metadata={"help": "mean value for initializing linear layer weights"})
    linear_init_std: float = field(default=0.03, metadata={"help": "standard deviation value for initializing linear "
                                                                   "layer weights"})
    document_pooling_strategy: DocumentPoolingStrategy = field(
        default="flat",
        metadata={"help": "how to pool document representation after label-wise attention layer for each label"})