-
Notifications
You must be signed in to change notification settings - Fork 2
/
model.py
263 lines (149 loc) · 8.33 KB
/
model.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
258
259
260
261
262
263
import torch
import torch.nn as nn
from agg_predictor import AggPredictor
from cond_predictor import CondPredictor
from select_predictor import SelectionPredictor
from wordembedding import WordEmbedding
from torch.autograd import Variable
import numpy as np
class Model(nn.Module):
def __init__(self , hidden_dim , embed_dim , word_emb):
super().__init__()
self.agg_ops = ['', 'MAX', 'MIN', 'COUNT', 'SUM', 'AVG']
self.hidden_dim = hidden_dim
self.embed_dim = embed_dim
self.num_layers = 2
self.max_tok_num = 200
self.agg_predictor = AggPredictor( embed_dim , hidden_dim , self.num_layers)
self.cond_predictor = CondPredictor( embed_dim , hidden_dim , self.num_layers, self.max_tok_num )
self.sel_predictor = SelectionPredictor(embed_dim,hidden_dim , self.num_layers)
self.ce = nn.CrossEntropyLoss()
self.wordembedding = word_emb
self.sigmoid = nn.Sigmoid()
def forward(self, question , columns , pred_entry , where_col=None, gt_where=None):
pred_agg , pred_sel, pred_cond = pred_entry
embedding , length = self.wordembedding.gen_x_batch(question,columns)
col_inp_var , name_length , col_length = self.wordembedding.gen_column_batch(columns)
agg_score = None
sel_score = None
cond_score = None
batch_size = len(question)
if pred_agg:
hidden = ( torch.zeros(self.num_layers*2,batch_size,int(self.hidden_dim/2)) , torch.zeros(self.num_layers*2,batch_size,int(self.hidden_dim/2)) )
agg_score = self.agg_predictor.forward(embedding,length, hidden)
if pred_sel:
hidden = (torch.zeros(self.num_layers*2,batch_size,int(self.hidden_dim/2)) , torch.zeros(self.num_layers*2,batch_size,int(self.hidden_dim/2)) )
sel_score =self.sel_predictor.forward(embedding,length,col_inp_var,name_length,col_length,hidden)
if pred_cond:
cond_score = self.cond_predictor.forward( embedding , length, col_inp_var,name_length,col_length,where_col,gt_where )
return ( agg_score , sel_score, cond_score )
def loss(self, score, truth , pred_entry ):
pred_agg , pred_sel,pred_cond = pred_entry
loss = 0
if pred_agg:
agg_truth = torch.from_numpy(np.asarray(truth[0]))
agg_truth_var = Variable(agg_truth)
loss += self.ce(score[0],agg_truth_var)
if pred_sel:
sel_truth = torch.from_numpy(np.asarray(truth[1]))
sel_truth_var = Variable(sel_truth)
loss+= self.ce(score[1],sel_truth_var)
if pred_cond:
# Loss for the number of conditions
cond_num_score , cond_col_score , cond_op_score,cond_str_score = score[2]
cond_col_num = torch.from_numpy(np.asarray(truth[2]))
cond_col_num_var = Variable(cond_col_num)
loss += self.ce(cond_num_score, cond_col_num_var )
# Loss for columns associated with each condition
T = len( cond_col_score[0] ) #Maximum number of tokens
batch_size = len(cond_col_score)
truth_prob = np.zeros( ( batch_size ,T) , dtype=np.float32)
for b in range(batch_size):
if len( truth[3][b] ) > 0:
truth_prob[b][ list( truth[3][b]) ] =1
#CUDA cond_col_truth_var
cond_col_truth_var = Variable(torch.from_numpy(truth_prob))
cond_col_prob = self.sigmoid( cond_col_score )
bce_loss = -torch.mean(
3*( cond_col_truth_var *torch.log(cond_col_prob+1e-10)) +
(1-cond_col_truth_var)*torch.log(1-cond_col_prob+1e-10)
)
loss += bce_loss
# LOSS for operators corresponding to each condition
for b in range(batch_size):
if len( truth[4][b] ) ==0:
continue #Continue if there is no condition
#CUDA cond_op_truth
cond_op_truth_var = Variable( torch.from_numpy( np.array( truth[4][b] ) ) )
cond_op_pred = cond_op_score[ b , :len(truth[4][b] )]
loss += ( self.ce(cond_op_pred,cond_op_truth_var) / batch_size )
# LOSS for strings in each condition Crossentropy Loss against one hot
# encoding of names
for b in range(len( truth[5] )):
for idx in range(len(truth[5][b])):
cond_str_truth = truth[5][b][idx]
if len(cond_str_truth) == 1:
continue
data = torch.from_numpy(np.array(cond_str_truth[1:]) )
# CUDA cond_str_truth_var
cond_str_truth_var = Variable(data)
str_end = len(cond_str_truth)-1
cond_str_pred = cond_str_score[b,idx, :str_end]
loss += ( self.ce(cond_str_pred,cond_str_truth_var)/ ( len(truth[5])* len(truth[5][b]) ) )
return loss
def validation_loss(self,score,truth , pred_entry ):
pred_agg , pred_sel , pred_cond = pred_entry
agg_loss = 0.0
sel_loss = 0.0
cond_loss= 0.0
if pred_agg :
agg_truth = torch.from_numpy( np.asarray( truth[0] ) )
agg_truth_var = Variable(agg_truth)
agg_loss = self.ce( score[0] , agg_truth_var )
if pred_sel:
sel_truth = torch.from_numpy( np.asarray( truth[1] ) )
sel_truth_var = Variable(sel_truth)
sel_loss = self.ce( score[1] , sel_truth_var )
if pred_cond:
# Loss for the number of conditions
cond_num_score , cond_col_score , cond_op_score,cond_str_score = score[2]
cond_col_num = torch.from_numpy(np.asarray(truth[2]))
cond_col_num_var = Variable(cond_col_num)
cond_loss += self.ce(cond_num_score, cond_col_num_var )
# Loss for columns associated with each condition
T = len( cond_col_score[0] ) #Maximum number of tokens
batch_size = len(cond_col_score)
truth_prob = np.zeros( ( batch_size ,T) , dtype=np.float32)
for b in range(batch_size):
if len( truth[3][b] ) > 0:
truth_prob[b][ list( truth[3][b]) ] =1
#CUDA cond_col_truth_var
cond_col_truth_var = Variable(torch.from_numpy(truth_prob))
cond_col_prob = self.sigmoid( cond_col_score )
bce_loss = -torch.mean(
3*( cond_col_truth_var *torch.log(cond_col_prob+1e-10)) +
(1-cond_col_truth_var)*torch.log(1-cond_col_prob+1e-10)
)
cond_loss += bce_loss
# LOSS for operators corresponding to each condition
for b in range(batch_size):
if len( truth[4][b] ) ==0:
continue #Continue if there is no condition
#CUDA cond_op_truth
cond_op_truth_var = Variable( torch.from_numpy( np.array( truth[4][b] ) ) )
cond_op_pred = cond_op_score[ b , :len(truth[4][b] )]
cond_loss += ( self.ce(cond_op_pred,cond_op_truth_var) / batch_size )
# LOSS for strings in each condition Crossentropy Loss against one hot
# encoding of names
for b in range(len( truth[5] )):
for idx in range(len(truth[5][b])):
cond_str_truth = truth[5][b][idx]
if len(cond_str_truth) == 1:
continue
data = torch.from_numpy(np.array(cond_str_truth[1:]) )
# CUDA cond_str_truth_var
cond_str_truth_var = Variable(data)
str_end = len(cond_str_truth)-1
cond_str_pred = cond_str_score[b,idx, :str_end]
cond_loss += ( self.ce(cond_str_pred,cond_str_truth_var)/ ( len(truth[5])* len(truth[5][b]) ) )
return (agg_loss,sel_loss,cond_loss)