-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathexcelformer.py
166 lines (139 loc) · 4.61 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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# The ExcelFormer method from the
# "ExcelFormer: A neural network surpassing GBDTs on tabular data" paper.
# ArXiv: https://arxiv.org/abs/2301.02819
# Datasets Titanic Adult
# AUC 0.920 0.913
# Time 7.3s 231.1s
import argparse
import sys
import time
from typing import Any, Dict, List
import os.path as osp
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.datasets.titanic import Titanic
from rllm.transforms.table_transforms import DefaultTableTransform
from rllm.nn.conv.table_conv import ExcelFormerConv
parser = argparse.ArgumentParser()
parser.add_argument("--emb_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("--epochs", type=int, default=50)
parser.add_argument("--lr", type=float, default=1e-3)
parser.add_argument("--wd", type=float, default=5e-4)
parser.add_argument("--seed", type=int, default=0)
args = parser.parse_args()
# Set random seed and device
torch.manual_seed(args.seed)
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
# Load dataset
path = osp.join(osp.dirname(osp.realpath(__file__)), "..", "data")
data = Titanic(cached_dir=path)[0]
# Transform data
transform = DefaultTableTransform(out_dim=args.emb_dim)
data = transform(data).to(device)
data.shuffle()
# Split dataset, here the ratio of train-val-test is 80%-10%-10%
train_loader, val_loader, test_loader = data.get_dataloader(
train_split=0.8, val_split=0.1, test_split=0.1, batch_size=args.batch_size
)
# Define model
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.convs = torch.nn.ModuleList()
self.convs.append(
ExcelFormerConv(
conv_dim=hidden_dim,
use_pre_encoder=True,
metadata=metadata,
)
)
for _ in range(num_layers - 1):
self.convs.append(ExcelFormerConv(conv_dim=hidden_dim))
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:
for conv in self.convs:
x = conv(x)
out = self.fc(x.mean(dim=1))
return out
# Set up model and optimizer
model = ExcelFormer(
hidden_dim=args.emb_dim,
out_dim=data.num_classes,
num_layers=args.num_layers,
metadata=data.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 = best_test_metric = 0
times = []
for epoch in range(1, args.epochs + 1):
start = time.time()
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
times.append(time.time() - start)
print(
f"Train Loss: {train_loss:.4f}, Train {metric}: {train_metric:.4f}, "
f"Val {metric}: {val_metric:.4f}, Test {metric}: {test_metric:.4f}"
)
print(f"Mean time per epoch: {torch.tensor(times).mean():.4f}s")
print(f"Total time: {sum(times):.4f}s")
print(
f"Best Val {metric}: {best_val_metric:.4f}, "
f"Best Test {metric}: {best_test_metric:.4f}"
)