-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutils.py
155 lines (142 loc) · 4.95 KB
/
utils.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
import torch
import random
import numpy as np
def set_seed(args):
random.seed(args.seed)
np.random.seed(args.seed)
torch.manual_seed(args.seed)
if args.n_gpu > 0 and torch.cuda.is_available():
torch.cuda.manual_seed_all(args.seed)
def collate_fn(batch):
max_len = max([len(f["input_ids"]) for f in batch])
input_ids = [f["input_ids"] + [0] * (max_len - len(f["input_ids"])) for f in batch]
input_mask = [[1.0] * len(f["input_ids"]) + [0.0] * (max_len - len(f["input_ids"])) for f in batch]
labels = [f["labels"] for f in batch]
input_ids = torch.tensor(input_ids, dtype=torch.long)
input_mask = torch.tensor(input_mask, dtype=torch.float)
labels = torch.tensor(labels, dtype=torch.long)
outputs = {
"input_ids": input_ids,
"attention_mask": input_mask,
"labels": labels,
}
return outputs
def pad0s(lis ,tolen):
len_ = len(lis)
if len_<tolen:
lis+=[0]*(tolen-len_)
return lis
def pad1s(lis ,tolen):
len_ = len(lis)
if len_<tolen:
lis+=[1]*(tolen-len_)
return lis
def CDataLoader(dataset, batch_size, collate_fn, shuffle, drop_last=True):
loader=[]
if shuffle:
random.shuffle(dataset)
length = len(dataset)
num_b = length//batch_size
last_num_b=length%batch_size
k=0
if last_num_b>0:
k=1
for i in range(num_b)
if i!=num_b:
batch = dataset[i*batch_size : (i+1)*batch_size]
else:
batch = dataset[-last_num_b : ]
input_ids=[]
attention_mask=[]
labels=[]
SRL_verb=[]
SRL_arg0=[]
SRL_arg1=[]
all_len=[]
for entry in batch:
all_len.append(len(entry['attention_mask']))
max_len = max(all_len)
if max_len>=254:
max_len=254
for dic in batch:
selflen = len(dic['input_ids'])
if selflen>max_len or selflen>255:
more = selflen-max_len
inputs = dic['input_ids'][:-more]
input_ids.append( pad1s( inputs,max_len))
attentions = dic['attention_mask'][:-more]
attention_mask.append(pad0s( attentions,max_len))
else:
inputs = dic['input_ids']
input_ids.append( pad1s( inputs,max_len))
attentions = dic['attention_mask']
attention_mask.append(pad0s( attentions,max_len))
labels.append( dic['labels'] )
V=dic['SRL_verb']
newV=[]
for v in V:
if v>selflen or v>max_len:
pass
else:
newV.append(v)
SRL_verb.append( newV )
A0 = dic['SRL_arg0']
newA0=[]
for a in A0:
if a>selflen or a>max_len:
pass
else:
newA0.append(a)
SRL_arg0.append(A0)
A1 = dic['SRL_arg1']
newA1=[]
for a in A1:
if a>selflen or a>max_len:
pass
else:
newA1.append(a)
SRL_arg1.append( A1 )
input_ids = torch.LongTensor(input_ids)
attention_mask= torch.LongTensor(attention_mask)
labels= torch.LongTensor(labels)
Batch_is_dic = {'input_ids': input_ids, 'attention_mask':attention_mask,'labels':labels,'SRL_verb':SRL_verb,'SRL_arg0':SRL_arg0, 'SRL_arg1':SRL_arg1}
loader.append(Batch_is_dic)
return loader
def CDataLoaderDev(dataset, batch_size, collate_fn, shuffle, drop_last=True):
loader=[]
if shuffle:
random.shuffle(dataset)
length = len(dataset)
num_b = length//batch_size
last_num_b=length%batch_size
k=0
if last_num_b>0:
k=1
for i in range(num_b):
if i!=num_b:
batch = dataset[i*batch_size : (i+1)*batch_size]
else:
batch = dataset[-last_num_b : ]
input_ids=[]
attention_mask=[]
labels=[]
SRL_verb=[]
SRL_arg0=[]
SRL_arg1=[]
all_len=[]
for entry in batch:
all_len.append(len(entry['attention_mask']))
max_len = max(all_len)
for dic in batch:
input_ids.append( pad0s( dic['input_ids'],max_len))
attention_mask.append(pad0s( dic['attention_mask'],max_len))
labels.append( dic['labels'] )
SRL_verb.append( dic['SRL_verb'][0][0])
SRL_arg0.append( dic['SRL_arg0'][0][0])
SRL_arg1.append( dic['SRL_arg1'][0][0])
input_ids = torch.Tensor(input_ids)
attention_mask= torch.Tensor(attention_mask)
labels= torch.Tensor(labels)
Batch_is_dic = {'input_ids': input_ids, 'attention_mask':attention_mask,'labels':labels,'SRL_verb':SRL_verb,'SRL_arg0':SRL_arg0, 'SRL_arg1':SRL_arg1}
loader.append(Batch_is_dic)
return loader