-
Notifications
You must be signed in to change notification settings - Fork 0
/
data_process.py
253 lines (189 loc) · 12.4 KB
/
data_process.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
import torch
from torch.utils.data import Dataset, DataLoader
import pandas as pd
from transformers import AutoTokenizer
import os
class MyDatasets(Dataset):
def __init__(self, df, tokenizer, max_len=64, mode='train'):
super(MyDatasets, self).__init__()
self.df = df
self.tokenizer = tokenizer
self.max_len = max_len
self.mode = mode
self.datalist = self.df2list(df)
def df2list(self, df):
text_a = df['text_a'].values.tolist()
text_b = df['text_b'].values.tolist()
return text_a + text_b
def __getitem__(self, index):
if self.mode == 'train':
# 我爱你
data = self.datalist[index]
# 我爱你这句话的意思是[MASK] h [x][x][x]这句话的意思是[MASK] h^ h' 我爱你,他的意思是[MASK] h'^ [x][x][x],他的意思是[MASK]
sentences_tem = self.template(data)
prompt_sen_1 = sentences_tem[0]
template_sen_1 = sentences_tem[1]
prompt_sen_2 = sentences_tem[2]
template_sen_2 = sentences_tem[3]
inputs_prompt_sen_1 = self.tokenizer(prompt_sen_1, truncation=True, max_length=self.max_len)
inputs_template_sen_1 = self.tokenizer(template_sen_1, truncation=True, max_length=self.max_len)
inputs_prompt_sen_2 = self.tokenizer(prompt_sen_2, truncation=True, max_length=self.max_len)
inputs_template_sen_2 = self.tokenizer(template_sen_2, truncation=True, max_length=self.max_len)
return {
'input_id_prompt_sen_1': torch.as_tensor(inputs_prompt_sen_1['input_ids'], dtype=torch.long),
'attention_mask_prompt_sen_1': torch.as_tensor(inputs_prompt_sen_1['attention_mask'], dtype=torch.long),
'input_id_template_sen_1': torch.as_tensor(inputs_template_sen_1['input_ids'], dtype=torch.long),
'attention_mask_template_sen_1': torch.as_tensor(inputs_template_sen_1['attention_mask'],
dtype=torch.long),
'input_id_prompt_sen_2': torch.as_tensor(inputs_prompt_sen_2['input_ids'], dtype=torch.long),
'attention_mask_prompt_sen_2': torch.as_tensor(inputs_prompt_sen_2['attention_mask'], dtype=torch.long),
'input_id_template_sen_2': torch.as_tensor(inputs_template_sen_2['input_ids'], dtype=torch.long),
'attention_mask_template_sen_2': torch.as_tensor(inputs_template_sen_2['attention_mask'],
dtype=torch.long)
}
else:
"""
去实现一下将一句话过模板 【X】这句话代表的意思是 [MASK] h h^ h-h^ h
"""
data = self.df.iloc[index]
text_a = data['text_a']
text_b = data['text_b']
label = data['label']
# text_a 生成的模板
sentences_tem_a = self.single_template(text_a)
# text_b 生成的模板
sentences_tem_b = self.single_template(text_b)
# 带入模板之后的 a,b
prompt_sen_1_a = sentences_tem_a[0]
template_sen_1_a = sentences_tem_a[1]
prompt_sen_1_b = sentences_tem_b[0]
template_sen_1_b = sentences_tem_b[1]
# 分词
input_prompt_sen_1_a = self.tokenizer(prompt_sen_1_a, truncation=True, max_length=self.max_len)
input_template_sen_1_a = self.tokenizer(template_sen_1_a, truncation=True, max_length=self.max_len)
input_prompt_sen_1_b = self.tokenizer(prompt_sen_1_b, truncation=True, max_length=self.max_len)
input_template_sen_1_b = self.tokenizer(template_sen_1_b, truncation=True, max_length=self.max_len)
return {
"input_ids_prompt_sen_1_a": torch.as_tensor(input_prompt_sen_1_a["input_ids"], dtype=torch.long),
"attention_mask_prompt_sen_1_a": torch.as_tensor(input_prompt_sen_1_a["attention_mask"],
dtype=torch.long),
"input_ids_template_sen_1_a": torch.as_tensor(input_template_sen_1_a["input_ids"], dtype=torch.long),
"attention_mask_template_sen_1_a": torch.as_tensor(input_template_sen_1_a["attention_mask"],
dtype=torch.long),
"input_ids_prompt_sen_1_b": torch.as_tensor(input_prompt_sen_1_b["input_ids"], dtype=torch.long),
"attention_mask_prompt_sen_1_b": torch.as_tensor(input_prompt_sen_1_b["attention_mask"],
dtype=torch.long),
"input_ids_template_sen_1_b": torch.as_tensor(input_template_sen_1_b["input_ids"], dtype=torch.long),
"attention_mask_template_sen_1_b": torch.as_tensor(input_template_sen_1_b["attention_mask"],
dtype=torch.long),
"label": torch.as_tensor(label, dtype=torch.long)
}
def single_template(self, sentence):
prompt_tem = ['[X],这句话的意思是[MASK]']
sentence_tem = []
for template in prompt_tem:
prompt_sentence = template.replace("[X]", sentence)
words_len = len(sentence)
template_senence = template.replace("[X]", "[X]" * words_len)
sentence_tem += [prompt_sentence, template_senence]
return sentence_tem
def template(self, sentence):
prompt_tem = ['[X],这句话的意思是[MASK]', '[X],它的意思是[MASK]']
sentence_tem = []
for template in prompt_tem:
prompt_setence = template.replace('[X]', sentence)
words_len = len(sentence)
template_sentence = template.replace('[X]', '[X]' * words_len)
sentence_tem += [prompt_setence, template_sentence]
return sentence_tem
def __len__(self):
if self.mode == 'train':
return len(self.datalist)
else:
return len(self.df)
def collate_fn_train(batch):
max_prompt_len_sen_1 = max([len(x['input_id_prompt_sen_1']) for x in batch])
#
max_template_len_sen_1 = max([len(x['input_id_template_sen_1']) for x in batch])
max_prompt_len_sen_2 = max([len(x['input_id_prompt_sen_2']) for x in batch])
max_template_len_sen_2 = max([len(x['input_id_template_sen_2']) for x in batch])
input_ids_prompt_sen_1 = torch.zeros(len(batch), max_prompt_len_sen_1, dtype=torch.long)
attention_masks_prompt_sen_1 = torch.zeros(len(batch), max_prompt_len_sen_1, dtype=torch.long)
input_ids_template_sen_1 = torch.zeros(len(batch), max_template_len_sen_1, dtype=torch.long)
attention_masks_template_sen_1 = torch.zeros(len(batch), max_template_len_sen_1, dtype=torch.long)
input_ids_prompt_sen_2 = torch.zeros(len(batch), max_prompt_len_sen_2, dtype=torch.long)
attention_masks_prompt_sen_2 = torch.zeros(len(batch), max_prompt_len_sen_2, dtype=torch.long)
input_ids_template_sen_2 = torch.zeros(len(batch), max_template_len_sen_2, dtype=torch.long)
attention_masks_template_sen_2 = torch.zeros(len(batch), max_template_len_sen_2, dtype=torch.long)
for i, x in enumerate(batch):
input_ids_prompt_sen_1[i, :len(x['input_id_prompt_sen_1'])] = x['input_id_prompt_sen_1']
attention_masks_prompt_sen_1[i, :len(x['attention_mask_prompt_sen_1'])] = x['attention_mask_prompt_sen_1']
input_ids_template_sen_1[i, :len(x['input_id_template_sen_1'])] = x['input_id_template_sen_1']
attention_masks_template_sen_1[i, :len(x['attention_mask_template_sen_1'])] = x['attention_mask_template_sen_1']
input_ids_prompt_sen_2[i, :len(x['input_id_prompt_sen_2'])] = x['input_id_prompt_sen_2']
attention_masks_prompt_sen_2[i, :len(x['attention_mask_prompt_sen_2'])] = x['attention_mask_prompt_sen_2']
input_ids_template_sen_2[i, :len(x['input_id_template_sen_2'])] = x['input_id_template_sen_2']
attention_masks_template_sen_2[i, :len(x['attention_mask_template_sen_2'])] = x['attention_mask_template_sen_2']
return {
'input_ids_prompt_sen_1': input_ids_prompt_sen_1,
'attention_masks_prompt_sen_1': attention_masks_prompt_sen_1,
'input_ids_template_sen_1': input_ids_template_sen_1,
'attention_masks_template_sen_1': attention_masks_template_sen_1,
'input_ids_prompt_sen_2': input_ids_prompt_sen_2,
'attention_masks_prompt_sen_2': attention_masks_prompt_sen_2,
'input_ids_template_sen_2': input_ids_template_sen_2,
'attention_masks_template_sen_2': attention_masks_template_sen_2,
}
def collate_fn_dev(batch):
max_prompt_len_sen_1 = max([len(x['input_ids_prompt_sen_1_a']) for x in batch])
#
max_template_len_sen_1 = max([len(x['input_ids_template_sen_1_a']) for x in batch])
max_prompt_len_sen_2 = max([len(x['input_ids_prompt_sen_1_b']) for x in batch])
max_template_len_sen_2 = max([len(x['input_ids_template_sen_1_b']) for x in batch])
input_ids_prompt_sen_1 = torch.zeros(len(batch), max_prompt_len_sen_1, dtype=torch.long)
attention_masks_prompt_sen_1 = torch.zeros(len(batch), max_prompt_len_sen_1, dtype=torch.long)
input_ids_template_sen_1 = torch.zeros(len(batch), max_template_len_sen_1, dtype=torch.long)
attention_masks_template_sen_1 = torch.zeros(len(batch), max_template_len_sen_1, dtype=torch.long)
input_ids_prompt_sen_2 = torch.zeros(len(batch), max_prompt_len_sen_2, dtype=torch.long)
attention_masks_prompt_sen_2 = torch.zeros(len(batch), max_prompt_len_sen_2, dtype=torch.long)
input_ids_template_sen_2 = torch.zeros(len(batch), max_template_len_sen_2, dtype=torch.long)
attention_masks_template_sen_2 = torch.zeros(len(batch), max_template_len_sen_2, dtype=torch.long)
label = []
for i, x in enumerate(batch):
input_ids_prompt_sen_1[i, :len(x['input_ids_prompt_sen_1_a'])] = x['input_ids_prompt_sen_1_a']
attention_masks_prompt_sen_1[i, :len(x['attention_mask_prompt_sen_1_a'])] = x['attention_mask_prompt_sen_1_a']
input_ids_template_sen_1[i, :len(x['input_ids_template_sen_1_a'])] = x['input_ids_template_sen_1_a']
attention_masks_template_sen_1[i, :len(x['attention_mask_template_sen_1_a'])] = x[
'attention_mask_template_sen_1_a']
input_ids_prompt_sen_2[i, :len(x['input_ids_prompt_sen_1_b'])] = x['input_ids_prompt_sen_1_b']
attention_masks_prompt_sen_2[i, :len(x['attention_mask_prompt_sen_1_b'])] = x['attention_mask_prompt_sen_1_b']
input_ids_template_sen_2[i, :len(x['input_ids_template_sen_1_b'])] = x['input_ids_template_sen_1_b']
attention_masks_template_sen_2[i, :len(x['attention_mask_template_sen_1_b'])] = x[
'attention_mask_template_sen_1_b']
label.append(x['label'])
return {
'input_ids_prompt_sen_1': input_ids_prompt_sen_1,
'attention_masks_prompt_sen_1': attention_masks_prompt_sen_1,
'input_ids_template_sen_1': input_ids_template_sen_1,
'attention_masks_template_sen_1': attention_masks_template_sen_1,
'input_ids_prompt_sen_2': input_ids_prompt_sen_2,
'attention_masks_prompt_sen_2': attention_masks_prompt_sen_2,
'input_ids_template_sen_2': input_ids_template_sen_2,
'attention_masks_template_sen_2': attention_masks_template_sen_2,
'label': torch.tensor(label, dtype=torch.long)
}
def load_data(tokenizer, batch_size=32):
train_df = pd.read_csv('data/ants/train.csv')
train_sets = MyDatasets(train_df, tokenizer, 64, mode='train')
train_dataloader = DataLoader(train_sets, batch_size, collate_fn=collate_fn_train, shuffle=True)
dev_df = pd.read_csv('data/ants/dev.csv')
dev_sets = MyDatasets(dev_df, tokenizer, 64, mode='dev')
dev_dataloader = DataLoader(dev_sets, batch_size, collate_fn=collate_fn_dev, shuffle=False)
return train_dataloader, dev_dataloader
if __name__ == '__main__':
tokenizer = AutoTokenizer.from_pretrained('roberta_data')
tokenizer.add_tokens(['[X]'])
train_dataloader, dev_dataloader = load_data(tokenizer)
for batch in dev_dataloader:
print(batch['input_ids_prompt_sen_1'].shape)
break