-
Notifications
You must be signed in to change notification settings - Fork 30
/
excelformer.py
146 lines (123 loc) · 4.02 KB
/
excelformer.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
import argparse
import os.path as osp
import sys
from typing import Any, Dict, List
from tqdm import tqdm
from sklearn.metrics import roc_auc_score
import torch
from torch import Tensor
from torch.utils.data import DataLoader
import torch.nn.functional as F
sys.path.append("./")
sys.path.append("../")
from rllm.types import ColType
from rllm.nn.models import MODEL_CONFIG
from rllm.datasets.titanic import Titanic
from rllm.nn.conv.table_conv import ExcelFormerConv
parser = argparse.ArgumentParser()
parser.add_argument("--dataset", type=str, default="titanic")
parser.add_argument("--dim", help="embedding dim.", type=int, default=32)
parser.add_argument("--num_layers", type=int, default=3)
parser.add_argument("--batch_size", type=int, default=128)
parser.add_argument("--lr", type=float, default=1e-3)
parser.add_argument("--epochs", type=int, default=50)
parser.add_argument("--seed", type=int, default=42)
parser.add_argument("--wd", type=float, default=5e-4)
args = parser.parse_args()
torch.manual_seed(args.seed)
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
# Prepare datasets
path = osp.join(osp.dirname(osp.realpath(__file__)), "..", "data")
dataset = Titanic(cached_dir=path)[0]
dataset.to(device)
# Split dataset, here the ratio of train-val-test is 80%-10%-10%
train_loader, val_loader, test_loader = dataset.get_dataloader(
0.8, 0.1, 0.1, batch_size=args.batch_size
)
# Set up model and optimizer
class ExcelFormer(torch.nn.Module):
def __init__(
self,
hidden_dim: int,
out_dim: int,
num_layers: int,
metadata: Dict[ColType, List[Dict[str, Any]]],
):
super().__init__()
self.transform = MODEL_CONFIG[ExcelFormerConv](
out_dim=hidden_dim,
metadata=metadata,
)
self.convs = torch.nn.ModuleList(
[ExcelFormerConv(dim=hidden_dim) for _ in range(num_layers)]
)
self.fc = torch.nn.Sequential(
torch.nn.LayerNorm(hidden_dim),
torch.nn.ReLU(),
torch.nn.Linear(hidden_dim, out_dim),
)
def forward(self, x) -> Tensor:
x = self.transform(x)
for conv in self.convs:
x = conv(x)
out = self.fc(x.mean(dim=1))
return out
model = ExcelFormer(
hidden_dim=args.dim,
out_dim=dataset.num_classes,
num_layers=args.num_layers,
metadata=dataset.metadata,
).to(device)
optimizer = torch.optim.Adam(
model.parameters(),
lr=args.lr,
weight_decay=args.wd,
)
def train(epoch: int) -> float:
model.train()
loss_accum = total_count = 0
for batch in tqdm(train_loader, desc=f"Epoch: {epoch}"):
x, y = batch
pred = model.forward(x)
loss = F.cross_entropy(pred, y.long())
optimizer.zero_grad()
loss.backward()
loss_accum += float(loss) * y.size(0)
total_count += y.size(0)
optimizer.step()
return loss_accum / total_count
@torch.no_grad()
def test(loader: DataLoader) -> float:
model.eval()
all_preds = []
all_labels = []
for batch in loader:
x, y = batch
pred = model.forward(x)
all_labels.append(y.cpu())
all_preds.append(pred[:, 1].detach().cpu())
all_labels = torch.cat(all_labels).numpy()
all_preds = torch.cat(all_preds).numpy()
# Compute the overall AUC
overall_auc = roc_auc_score(all_labels, all_preds)
return overall_auc
metric = "AUC"
best_val_metric = 0
best_test_metric = 0
for epoch in range(1, args.epochs + 1):
train_loss = train(epoch)
train_metric = test(train_loader)
val_metric = test(val_loader)
test_metric = test(test_loader)
if val_metric > best_val_metric:
best_val_metric = val_metric
best_test_metric = test_metric
print(
f"Train Loss: {train_loss:.4f}, Train {metric}: {train_metric:.4f}, "
f"Val {metric}: {val_metric:.4f}, Test {metric}: {test_metric:.4f}"
)
optimizer.step()
print(
f"Best Val {metric}: {best_val_metric:.4f}, "
f"Best Test {metric}: {best_test_metric:.4f}"
)