-
Notifications
You must be signed in to change notification settings - Fork 12
/
data_loader.py
180 lines (161 loc) · 7.97 KB
/
data_loader.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
import numpy as np
import torch
import torch.nn as nn
from sklearn.model_selection import StratifiedKFold
from torch.utils.data import DataLoader, RandomSampler, SequentialSampler, TensorDataset
from run_classifier_dataset_utils import (
convert_examples_to_two_features,
convert_examples_to_features,
convert_two_examples_to_features,
)
def load_train_data(args, logger, processor, task_name, label_list, tokenizer, output_mode, k=None):
# Prepare data loader
if task_name == "vua":
train_examples = processor.get_train_examples(args.data_dir)
elif task_name == "trofi":
train_examples = processor.get_train_examples(args.data_dir, k)
else:
raise ("task_name should be 'vua' or 'trofi'!")
# make features file
if args.model_type == "BERT_BASE":
train_features = convert_two_examples_to_features(
train_examples, label_list, args.max_seq_length, tokenizer, output_mode
)
if args.model_type in ["BERT_SEQ", "MELBERT_SPV"]:
train_features = convert_examples_to_features(
train_examples, label_list, args.max_seq_length, tokenizer, output_mode, args
)
if args.model_type in ["MELBERT_MIP", "MELBERT"]:
train_features = convert_examples_to_two_features(
train_examples, label_list, args.max_seq_length, tokenizer, output_mode, args
)
# make features into tensor
all_input_ids = torch.tensor([f.input_ids for f in train_features], dtype=torch.long)
all_input_mask = torch.tensor([f.input_mask for f in train_features], dtype=torch.long)
all_segment_ids = torch.tensor([f.segment_ids for f in train_features], dtype=torch.long)
all_label_ids = torch.tensor([f.label_id for f in train_features], dtype=torch.long)
# add additional features for MELBERT_MIP and MELBERT
if args.model_type in ["MELBERT_MIP", "MELBERT"]:
all_input_ids_2 = torch.tensor([f.input_ids_2 for f in train_features], dtype=torch.long)
all_input_mask_2 = torch.tensor([f.input_mask_2 for f in train_features], dtype=torch.long)
all_segment_ids_2 = torch.tensor(
[f.segment_ids_2 for f in train_features], dtype=torch.long
)
train_data = TensorDataset(
all_input_ids,
all_input_mask,
all_segment_ids,
all_label_ids,
all_input_ids_2,
all_input_mask_2,
all_segment_ids_2,
)
else:
train_data = TensorDataset(all_input_ids, all_input_mask, all_segment_ids, all_label_ids)
train_sampler = RandomSampler(train_data)
train_dataloader = DataLoader(
train_data, sampler=train_sampler, batch_size=args.train_batch_size
)
return train_dataloader
def load_train_data_kf(
args, logger, processor, task_name, label_list, tokenizer, output_mode, k=None
):
# Prepare data loader
if task_name == "vua":
train_examples = processor.get_train_examples(args.data_dir)
elif task_name == "trofi":
train_examples = processor.get_train_examples(args.data_dir, k)
else:
raise ("task_name should be 'vua' or 'trofi'!")
# make features file
if args.model_type == "BERT_BASE":
train_features = convert_two_examples_to_features(
train_examples, label_list, args.max_seq_length, tokenizer, output_mode
)
if args.model_type in ["BERT_SEQ", "MELBERT_SPV"]:
train_features = convert_examples_to_features(
train_examples, label_list, args.max_seq_length, tokenizer, output_mode, args
)
if args.model_type in ["MELBERT_MIP", "MELBERT"]:
train_features = convert_examples_to_two_features(
train_examples, label_list, args.max_seq_length, tokenizer, output_mode, args
)
# make features into tensor
all_input_ids = torch.tensor([f.input_ids for f in train_features], dtype=torch.long)
all_input_mask = torch.tensor([f.input_mask for f in train_features], dtype=torch.long)
all_segment_ids = torch.tensor([f.segment_ids for f in train_features], dtype=torch.long)
all_label_ids = torch.tensor([f.label_id for f in train_features], dtype=torch.long)
# add additional features for MELBERT_MIP and MELBERT
if args.model_type in ["MELBERT_MIP", "MELBERT"]:
all_input_ids_2 = torch.tensor([f.input_ids_2 for f in train_features], dtype=torch.long)
all_input_mask_2 = torch.tensor([f.input_mask_2 for f in train_features], dtype=torch.long)
all_segment_ids_2 = torch.tensor(
[f.segment_ids_2 for f in train_features], dtype=torch.long
)
train_data = TensorDataset(
all_input_ids,
all_input_mask,
all_segment_ids,
all_label_ids,
all_input_ids_2,
all_input_mask_2,
all_segment_ids_2,
)
else:
train_data = TensorDataset(all_input_ids, all_input_mask, all_segment_ids, all_label_ids)
gkf = StratifiedKFold(n_splits=args.num_bagging).split(X=all_input_ids, y=all_label_ids.numpy())
return train_data, gkf
def load_test_data(args, logger, processor, task_name, label_list, tokenizer, output_mode, k=None):
if task_name == "vua":
eval_examples = processor.get_test_examples(args.data_dir)
elif task_name == "trofi":
eval_examples = processor.get_test_examples(args.data_dir, k)
else:
raise ("task_name should be 'vua' or 'trofi'!")
if args.model_type == "BERT_BASE":
eval_features = convert_two_examples_to_features(
eval_examples, label_list, args.max_seq_length, tokenizer, output_mode
)
if args.model_type in ["BERT_SEQ", "MELBERT_SPV"]:
eval_features = convert_examples_to_features(
eval_examples, label_list, args.max_seq_length, tokenizer, output_mode, args
)
if args.model_type in ["MELBERT_MIP", "MELBERT"]:
eval_features = convert_examples_to_two_features(
eval_examples, label_list, args.max_seq_length, tokenizer, output_mode, args
)
logger.info("***** Running evaluation *****")
if args.model_type in ["MELBERT_MIP", "MELBERT"]:
all_input_ids = torch.tensor([f.input_ids for f in eval_features], dtype=torch.long)
all_input_mask = torch.tensor([f.input_mask for f in eval_features], dtype=torch.long)
all_segment_ids = torch.tensor([f.segment_ids for f in eval_features], dtype=torch.long)
all_guids = [f.guid for f in eval_features]
all_idx = torch.tensor([i for i in range(len(eval_features))], dtype=torch.long)
all_label_ids = torch.tensor([f.label_id for f in eval_features], dtype=torch.long)
all_input_ids_2 = torch.tensor([f.input_ids_2 for f in eval_features], dtype=torch.long)
all_input_mask_2 = torch.tensor([f.input_mask_2 for f in eval_features], dtype=torch.long)
all_segment_ids_2 = torch.tensor([f.segment_ids_2 for f in eval_features], dtype=torch.long)
eval_data = TensorDataset(
all_input_ids,
all_input_mask,
all_segment_ids,
all_label_ids,
all_idx,
all_input_ids_2,
all_input_mask_2,
all_segment_ids_2,
)
else:
all_input_ids = torch.tensor([f.input_ids for f in eval_features], dtype=torch.long)
all_input_mask = torch.tensor([f.input_mask for f in eval_features], dtype=torch.long)
all_segment_ids = torch.tensor([f.segment_ids for f in eval_features], dtype=torch.long)
all_guids = [f.guid for f in eval_features]
all_idx = torch.tensor([i for i in range(len(eval_features))], dtype=torch.long)
all_label_ids = torch.tensor([f.label_id for f in eval_features], dtype=torch.long)
eval_data = TensorDataset(
all_input_ids, all_input_mask, all_segment_ids, all_label_ids, all_idx
)
# Run prediction for full data
eval_sampler = SequentialSampler(eval_data)
eval_dataloader = DataLoader(eval_data, sampler=eval_sampler, batch_size=args.eval_batch_size)
return all_guids, eval_dataloader