-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
66 lines (54 loc) · 2.5 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
import torch
from sklearn.metrics import roc_auc_score
import numpy as np
from augmentations import embed_data_mask_train
import torch.nn as nn
def make_default_mask(x):
mask = np.ones_like(x)
mask[:,-1] = 0
return mask
def tag_gen(tag,y):
return np.repeat(tag,len(y['data']))
def count_parameters(model):
return sum(p.numel() for p in model.parameters() if p.requires_grad)
def imputations_acc_justy(model,dloader,device):
model.eval()
m = nn.Softmax(dim=1)
y_test = torch.empty(0).to(device)
y_pred = torch.empty(0).to(device)
prob = torch.empty(0).to(device)
with torch.no_grad():
for i, data in enumerate(dloader, 0):
x_categ, x_cont, cat_mask, con_mask = data[0].to(device), data[1].to(device),data[2].to(device),data[3].to(device)
_ , x_categ_enc, x_cont_enc = embed_data_mask_train(x_categ, x_cont, cat_mask, con_mask, model)
reps = model.transformer(x_categ_enc, x_cont_enc)
y_reps = reps[:,model.num_categories-1,:]
y_outs = model.mlpfory(y_reps)
# import ipdb; ipdb.set_trace()
y_test = torch.cat([y_test,x_categ[:,-1].float()],dim=0)
y_pred = torch.cat([y_pred,torch.argmax(m(y_outs), dim=1).float()],dim=0)
prob = torch.cat([prob,m(y_outs)[:,-1].float()],dim=0)
correct_results_sum = (y_pred == y_test).sum().float()
acc = correct_results_sum/y_test.shape[0]*100
auc = roc_auc_score(y_score=prob.cpu(), y_true=y_test.cpu())
return acc, auc
def multiclass_acc_justy(model,dloader,device):
model.eval()
vision_dset = True
m = nn.Softmax(dim=1)
y_test = torch.empty(0).to(device)
y_pred = torch.empty(0).to(device)
prob = torch.empty(0).to(device)
with torch.no_grad():
for i, data in enumerate(dloader, 0):
x_categ, x_cont, cat_mask, con_mask = data[0].to(device), data[1].to(device),data[2].to(device),data[3].to(device)
_ , x_categ_enc, x_cont_enc = embed_data_mask_train(x_categ, x_cont, cat_mask, con_mask,model)
reps = model.transformer(x_categ_enc, x_cont_enc)
y_reps = reps[:,model.num_categories-1,:]
y_outs = model.mlpfory(y_reps)
# import ipdb; ipdb.set_trace()
y_test = torch.cat([y_test,x_categ[:,-1].float()],dim=0)
y_pred = torch.cat([y_pred,torch.argmax(m(y_outs), dim=1).float()],dim=0)
correct_results_sum = (y_pred == y_test).sum().float()
acc = correct_results_sum/y_test.shape[0]*100
return acc, 0