-
Notifications
You must be signed in to change notification settings - Fork 48
/
main.py
217 lines (198 loc) · 10.2 KB
/
main.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
import torch
import tqdm
from sklearn.metrics import roc_auc_score
from torch.utils.data import DataLoader
import os
import numpy as np
from datasets.aliexpress import AliExpressDataset
from models.sharedbottom import SharedBottomModel
from models.singletask import SingleTaskModel
from models.omoe import OMoEModel
from models.mmoe import MMoEModel
from models.ple import PLEModel
from models.aitm import AITMModel
from models.metaheac import MetaHeacModel
def get_dataset(name, path):
if 'AliExpress' in name:
return AliExpressDataset(path)
else:
raise ValueError('unknown dataset name: ' + name)
def get_model(name, categorical_field_dims, numerical_num, task_num, expert_num, embed_dim):
"""
Hyperparameters are empirically determined, not opitmized.
"""
if name == 'sharedbottom':
print("Model: Shared-Bottom")
return SharedBottomModel(categorical_field_dims, numerical_num, embed_dim=embed_dim, bottom_mlp_dims=(512, 256), tower_mlp_dims=(128, 64), task_num=task_num, dropout=0.2)
elif name == 'singletask':
print("Model: SingleTask")
return SingleTaskModel(categorical_field_dims, numerical_num, embed_dim=embed_dim, bottom_mlp_dims=(512, 256), tower_mlp_dims=(128, 64), task_num=task_num, dropout=0.2)
elif name == 'omoe':
print("Model: OMoE")
return OMoEModel(categorical_field_dims, numerical_num, embed_dim=embed_dim, bottom_mlp_dims=(512, 256), tower_mlp_dims=(128, 64), task_num=task_num, expert_num=expert_num, dropout=0.2)
elif name == 'mmoe':
print("Model: MMoE")
return MMoEModel(categorical_field_dims, numerical_num, embed_dim=embed_dim, bottom_mlp_dims=(512, 256), tower_mlp_dims=(128, 64), task_num=task_num, expert_num=expert_num, dropout=0.2)
elif name == 'ple':
print("Model: PLE")
return PLEModel(categorical_field_dims, numerical_num, embed_dim=embed_dim, bottom_mlp_dims=(512, 256), tower_mlp_dims=(128, 64), task_num=task_num, shared_expert_num=int(expert_num / 2), specific_expert_num=int(expert_num / 2), dropout=0.2)
elif name == 'aitm':
print("Model: AITM")
return AITMModel(categorical_field_dims, numerical_num, embed_dim=embed_dim, bottom_mlp_dims=(512, 256), tower_mlp_dims=(128, 64), task_num=task_num, dropout=0.2)
elif name == 'metaheac':
print("Model: MetaHeac")
return MetaHeacModel(categorical_field_dims, numerical_num, embed_dim=embed_dim, bottom_mlp_dims=(512, 256), tower_mlp_dims=(128, 64), task_num=task_num, expert_num=expert_num, critic_num=5, dropout=0.2)
else:
raise ValueError('unknown model name: ' + name)
class EarlyStopper(object):
def __init__(self, num_trials, save_path):
self.num_trials = num_trials
self.trial_counter = 0
self.best_accuracy = 0
self.save_path = save_path
def is_continuable(self, model, accuracy):
if accuracy > self.best_accuracy:
self.best_accuracy = accuracy
self.trial_counter = 0
torch.save(model.state_dict(), self.save_path)
return True
elif self.trial_counter + 1 < self.num_trials:
self.trial_counter += 1
return True
else:
return False
def train(model, optimizer, data_loader, criterion, device, log_interval=100):
model.train()
total_loss = 0
loader = tqdm.tqdm(data_loader, smoothing=0, mininterval=1.0)
for i, (categorical_fields, numerical_fields, labels) in enumerate(loader):
categorical_fields, numerical_fields, labels = categorical_fields.to(device), numerical_fields.to(device), labels.to(device)
y = model(categorical_fields, numerical_fields)
loss_list = [criterion(y[i], labels[:, i].float()) for i in range(labels.size(1))]
loss = 0
for item in loss_list:
loss += item
loss /= len(loss_list)
model.zero_grad()
loss.backward()
optimizer.step()
total_loss += loss.item()
if (i + 1) % log_interval == 0:
loader.set_postfix(loss=total_loss / log_interval)
total_loss = 0
def metatrain(model, optimizer, data_loader, device, log_interval=100):
model.train()
total_loss = 0
loader = tqdm.tqdm(data_loader, smoothing=0, mininterval=1.0)
list_sup_categorical, list_sup_numerical, list_sup_y, list_qry_categorical, list_qry_numerical, list_qry_y = list(), list(), list(), list(), list(), list()
for i, (categorical_fields, numerical_fields, labels) in enumerate(loader):
categorical_fields, numerical_fields, labels = categorical_fields.to(device), numerical_fields.to(device), labels.to(device)
batch_size = int(categorical_fields.size(0) / 2)
list_sup_categorical.append(categorical_fields[:batch_size])
list_qry_categorical.append(categorical_fields[batch_size:])
list_sup_numerical.append(numerical_fields[:batch_size])
list_qry_numerical.append(numerical_fields[batch_size:])
list_sup_y.append(labels[:batch_size])
list_qry_y.append(labels[batch_size:])
if (i + 1) % 2 == 0:
loss = model.global_update(list_sup_categorical, list_sup_numerical, list_sup_y, list_qry_categorical, list_qry_numerical, list_qry_y)
model.zero_grad()
loss.backward()
optimizer.step()
total_loss += loss.item()
list_sup_categorical, list_sup_numerical, list_sup_y, list_qry_categorical, list_qry_numerical, list_qry_y = list(), list(), list(), list(), list(), list()
if (i + 1) % log_interval == 0:
loader.set_postfix(loss=total_loss / log_interval)
total_loss = 0
def test(model, data_loader, task_num, device):
model.eval()
labels_dict, predicts_dict, loss_dict = {}, {}, {}
for i in range(task_num):
labels_dict[i], predicts_dict[i], loss_dict[i] = list(), list(), list()
with torch.no_grad():
for categorical_fields, numerical_fields, labels in tqdm.tqdm(data_loader, smoothing=0, mininterval=1.0):
categorical_fields, numerical_fields, labels = categorical_fields.to(device), numerical_fields.to(device), labels.to(device)
y = model(categorical_fields, numerical_fields)
for i in range(task_num):
labels_dict[i].extend(labels[:, i].tolist())
predicts_dict[i].extend(y[i].tolist())
loss_dict[i].extend(torch.nn.functional.binary_cross_entropy(y[i], labels[:, i].float(), reduction='none').tolist())
auc_results, loss_results = list(), list()
for i in range(task_num):
auc_results.append(roc_auc_score(labels_dict[i], predicts_dict[i]))
loss_results.append(np.array(loss_dict[i]).mean())
return auc_results, loss_results
def main(dataset_name,
dataset_path,
task_num,
expert_num,
model_name,
epoch,
learning_rate,
batch_size,
embed_dim,
weight_decay,
device,
save_dir):
device = torch.device(device)
train_dataset = get_dataset(dataset_name, os.path.join(dataset_path, dataset_name) + '/train.csv')
test_dataset = get_dataset(dataset_name, os.path.join(dataset_path, dataset_name) + '/test.csv')
train_data_loader = DataLoader(train_dataset, batch_size=batch_size, num_workers=4, shuffle=True)
test_data_loader = DataLoader(test_dataset, batch_size=batch_size, num_workers=4, shuffle=False)
field_dims = train_dataset.field_dims
numerical_num = train_dataset.numerical_num
model = get_model(model_name, field_dims, numerical_num, task_num, expert_num, embed_dim).to(device)
criterion = torch.nn.BCELoss()
optimizer = torch.optim.Adam(params=model.parameters(), lr=learning_rate, weight_decay=weight_decay)
save_path=f'{save_dir}/{dataset_name}_{model_name}.pt'
early_stopper = EarlyStopper(num_trials=2, save_path=save_path)
for epoch_i in range(epoch):
if model_name == 'metaheac':
metatrain(model, optimizer, train_data_loader, device)
else:
train(model, optimizer, train_data_loader, criterion, device)
auc, loss = test(model, test_data_loader, task_num, device)
print('epoch:', epoch_i, 'test: auc:', auc)
for i in range(task_num):
print('task {}, AUC {}, Log-loss {}'.format(i, auc[i], loss[i]))
if not early_stopper.is_continuable(model, np.array(auc).mean()):
print(f'test: best auc: {early_stopper.best_accuracy}')
break
model.load_state_dict(torch.load(save_path))
auc, loss = test(model, test_data_loader, task_num, device)
f = open('{}_{}.txt'.format(model_name, dataset_name), 'a', encoding = 'utf-8')
f.write('learning rate: {}\n'.format(learning_rate))
for i in range(task_num):
print('task {}, AUC {}, Log-loss {}'.format(i, auc[i], loss[i]))
f.write('task {}, AUC {}, Log-loss {}\n'.format(i, auc[i], loss[i]))
print('\n')
f.write('\n')
f.close()
if __name__ == '__main__':
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('--dataset_name', default='AliExpress_NL', choices=['AliExpress_NL', 'AliExpress_ES', 'AliExpress_FR', 'AliExpress_US'])
parser.add_argument('--dataset_path', default='./data/')
parser.add_argument('--model_name', default='metaheac', choices=['singletask', 'sharedbottom', 'omoe', 'mmoe', 'ple', 'aitm', 'metaheac'])
parser.add_argument('--epoch', type=int, default=50)
parser.add_argument('--task_num', type=int, default=2)
parser.add_argument('--expert_num', type=int, default=8)
parser.add_argument('--learning_rate', type=float, default=0.001)
parser.add_argument('--batch_size', type=int, default=2048)
parser.add_argument('--embed_dim', type=int, default=128)
parser.add_argument('--weight_decay', type=float, default=1e-6)
parser.add_argument('--device', default='cuda:0')
parser.add_argument('--save_dir', default='chkpt')
args = parser.parse_args()
main(args.dataset_name,
args.dataset_path,
args.task_num,
args.expert_num,
args.model_name,
args.epoch,
args.learning_rate,
args.batch_size,
args.embed_dim,
args.weight_decay,
args.device,
args.save_dir)