-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnll_utils.py
237 lines (215 loc) · 7.46 KB
/
nll_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
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
import torch
import torch.nn as nn
from torch.utils.data import DataLoader
from tqdm import tqdm
from datasets import TaskSpecificDataset
def stage_wise_NLL(net, mult_val_df, device):
"""
Stage wise NLL for comparison to Hunt baseline
"""
with torch.no_grad():
CE = nn.CrossEntropyLoss(reduction="none")
loss1sample = torch.tensor(0.0)
loss1guess = torch.tensor(0.0)
loss2sample = torch.tensor(0.0)
loss2guess = torch.tensor(0.0)
loss2abSample = torch.tensor(0.0)
loss3sample = torch.tensor(0.0)
loss3guess = torch.tensor(0.0)
loss4sample = torch.tensor(0.0)
net.eval()
mult_val = TaskSpecificDataset(mult_val_df, 4)
for i, batch_data in enumerate(
tqdm(
DataLoader(
mult_val,
batch_size=1,
shuffle=False,
num_workers=4,
pin_memory=True,
),
desc="Model NLL Calculation"
)
):
batch_data = [t.to(device) for t in batch_data]
(
stage1inp,
stage1tgt,
stage2mask,
stage2inp,
stage2tgt,
stage3mask,
stage3inp,
stage3tgt,
stage4mask,
stage4inp,
stage4tgt,
uid,
) = batch_data
subjemb = net.sub_emb(uid)
outputs = net.parallel_nets[0](
stage1inp, stage2inp, stage3inp, stage4inp, subjemb
)
(stage1out, stage2out, stage3out, stage4out, _) = outputs
aa = stage1inp[:, 1].cpu()
guess_1 = stage1tgt[:, 0].cpu()
rowAchosen = stage1tgt[:, 1].cpu()
guess_2 = stage2tgt[:, 0].cpu()
sampleA_2 = stage2tgt[:, 2].cpu()
guess_3 = stage3tgt[:, 0].cpu()
guess_4 = stage4tgt.cpu()
stage2mask = stage2mask.cpu()
sampleA_2 = sampleA_2.cpu()
# Stage1
loss1sample += CE(stage1out[0].cpu(), guess_1).mean()
if int(guess_1) > 0:
loss1guess += (
guess_1 * CE(stage1out[1].cpu(), rowAchosen)
).sum() / guess_1.sum()
# Stage2
if int(stage2mask) > 0 and aa == 0:
loss2sample += (
stage2mask * CE(stage2out[0].cpu(), guess_2)
).sum() / stage2mask.sum()
if int(guess_2) > 0:
loss2guess += (
stage2mask * guess_2 * CE(stage2out[1].cpu(), rowAchosen)
).sum() / (stage2mask * guess_2).sum()
else:
loss2abSample += (
stage2mask
* (1 - guess_2)
* (1 - aa)
* CE(stage2out[2].cpu(), sampleA_2)
).sum() / (stage2mask * (1 - guess_2) * (1 - aa)).sum()
i += 1
print(
loss1sample.item() / i,
loss1guess.item() / i,
loss2sample.item() / i,
loss2guess.item() / i,
loss2abSample.item() / i,
loss3sample.item() / i,
loss3guess.item() / i,
loss4sample.item() / i,
)
def baseline_stage_wise_NLL(valsAA, valsAB, vals2, mult_val_df):
with torch.no_grad():
CE = nn.CrossEntropyLoss(reduction="none")
loss1sample = torch.tensor(0.0)
loss1guess = torch.tensor(0.0)
loss2sample = torch.tensor(0.0)
loss2guess = torch.tensor(0.0)
loss2abSample = torch.tensor(0.0)
loss3sample = torch.tensor(0.0)
loss3guess = torch.tensor(0.0)
loss4sample = torch.tensor(0.0)
mult_val = TaskSpecificDataset(mult_val_df, 4)
for i, batch_data in enumerate(
tqdm(
DataLoader(
mult_val,
batch_size=1,
shuffle=False,
num_workers=4,
pin_memory=True,
),
desc = "Baseline NLL Calculation"
)
):
(
stage1inp,
stage1tgt,
stage2mask,
stage2inp,
stage2tgt,
stage3mask,
stage3inp,
stage3tgt,
stage4mask,
stage4inp,
stage4tgt,
uid,
) = batch_data
aa = stage1inp[:, 1]
card1 = int(stage1inp[:, 0] * 4.5 + 5.5) - 1
card2 = int(stage2inp[:, 0] * 4.5 + 5.5) - 1
guess_1 = stage1tgt[:, 0]
rowAchosen = stage1tgt[:, 1]
guess_2 = stage2tgt[:, 0]
sampleA_2 = stage2tgt[:, 2]
if int(aa) == 1:
vals = valsAA
else:
vals = valsAB
# Stage1
loss1sample += CE(
torch.tensor(
[[vals[2, card1], vals[0, card1] + vals[1, card1]]],
dtype=torch.float,
),
guess_1,
).mean()
if int(guess_1) > 0:
loss1guess += (
guess_1
* CE(
torch.tensor(
[[vals[1, card1], vals[2, card1]]], dtype=torch.float
),
rowAchosen,
)
).sum() / guess_1.sum()
# Stage2
if int(stage2mask) > 0 and aa == 0:
loss2sample += (
stage2mask
* CE(
torch.tensor(
[
[
vals2[card1, card2, 2:4].sum(),
vals2[card1, card2, 0:2].sum(),
]
],
dtype=torch.float,
),
guess_2,
)
).sum() / stage2mask.sum()
if int(guess_2) > 0:
loss2guess += (
stage2mask
* guess_2
* CE(
torch.tensor(
[[vals2[card1, card2, 1], vals2[card1, card2, 0]]],
dtype=torch.float,
),
rowAchosen,
)
).sum() / (stage2mask * guess_2).sum()
else:
loss2abSample += (
stage2mask
* (1 - guess_2)
* (1 - aa)
* CE(
torch.tensor(
[[vals2[card1, card2, 3], vals2[card1, card2, 2]]],
dtype=torch.float,
),
sampleA_2,
)
).sum() / (stage2mask * (1 - guess_2) * (1 - aa)).sum()
i += 1
print(
loss1sample.item() / i,
loss1guess.item() / i,
loss2sample.item() / i,
loss2guess.item() / i,
loss2abSample.item() / i,
loss3sample.item() / i,
loss3guess.item() / i,
loss4sample.item() / i,
)