-
Notifications
You must be signed in to change notification settings - Fork 134
/
Copy pathtrain_models.py
160 lines (155 loc) · 8.96 KB
/
train_models.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
from models import *
from helper import *
from tqdm import tqdm
import pdb
import torch
import gc
def train_GBDT2NN(args, num_data, plot_title, key="", trained_gbdt_model=None):
tree_layers = [int(x) for x in args.tree_layers.split(',')]
train_x, train_y, test_x, test_y = num_data
if trained_gbdt_model:
gbm, train_tree_pred = trained_gbdt_model
else:
gbm, train_tree_pred = TrainGBDT(train_x, train_y, test_x, test_y, args.tree_lr, args.ntrees, args.maxleaf, args.mindata, args.task)
gbms = SubGBDTLeaf_cls(train_x, test_x, gbm, args.maxleaf, num_slices=args.nslices, args = args)
min_len_features = train_x.shape[1]
used_features = []
tree_outputs = []
leaf_preds = []
test_leaf_preds = []
n_output = train_y.shape[1]
max_ntree_per_split = 0
group_average = []
for used_feature, new_train_y, leaf_pred, test_leaf_pred, avg, all_avg in gbms:
used_features.append(used_feature)
min_len_features = min(min_len_features, len(used_feature))
tree_outputs.append(new_train_y)
leaf_preds.append(leaf_pred)
test_leaf_preds.append(test_leaf_pred)
group_average.append(avg)
max_ntree_per_split = max(max_ntree_per_split, leaf_pred.shape[1])
for i in range(len(used_features)):
used_features[i] = sorted(used_features[i][:min_len_features])
n_models = len(used_features)
group_average = np.asarray(group_average).reshape(n_models, 1, 1)
for i in range(n_models):
if leaf_preds[i].shape[1] < max_ntree_per_split:
leaf_preds[i] = np.concatenate([leaf_preds[i] + 1,
np.zeros([leaf_preds[i].shape[0],
max_ntree_per_split-leaf_preds[i].shape[1]],
dtype=np.int32)], axis=1)
test_leaf_preds[i] = np.concatenate([test_leaf_preds[i] + 1,
np.zeros([test_leaf_preds[i].shape[0],
max_ntree_per_split-test_leaf_preds[i].shape[1]],
dtype=np.int32)], axis=1)
leaf_preds = np.concatenate(leaf_preds, axis=1)
test_leaf_preds = np.concatenate(test_leaf_preds, axis=1)
emb_model = EmbeddingModel(n_models, max_ntree_per_split, args.embsize, args.maxleaf+1, n_output, group_average, task=args.task).to(device)
tree_layers.append(args.embsize)
opt = AdamW(emb_model.parameters(), lr=args.emb_lr, weight_decay=args.l2_reg)
tree_outputs = np.asarray(tree_outputs).reshape((n_models, leaf_preds.shape[0])).transpose((1,0))
TrainWithLog(args, plot_title, leaf_preds, train_y, tree_outputs,
test_leaf_preds, test_y, emb_model, opt,
args.emb_epoch, args.batch_size, n_output, key+"emb-")
output_w = emb_model.bout.weight.data.cpu().numpy().reshape(n_models*args.embsize, n_output)
output_b = np.array(emb_model.bout.bias.data.cpu().numpy().sum())
train_embs = GetEmbPred(emb_model, emb_model.lastlayer, leaf_preds, args.test_batch_size)
del tree_outputs, leaf_preds, test_leaf_preds
gc.collect()
concate_train_x = np.concatenate([train_x, np.zeros((train_x.shape[0],1), dtype=np.float32)], axis=-1)
concate_test_x = np.concatenate([test_x, np.zeros((test_x.shape[0],1), dtype=np.float32)], axis=-1)
tree_outputs = train_embs
for seed in args.seeds:
np.random.seed(seed)
torch.cuda.manual_seed_all(seed)
gbdt2nn_model = GBDT2NN(concate_train_x.shape[1],
np.asarray(used_features,dtype=np.int64),
tree_layers,
output_w, output_b, args.task).to(device)
opt = AdamW(gbdt2nn_model.parameters(), lr=args.lr, weight_decay=args.l2_reg, amsgrad=False)
TrainWithLog(args, plot_title+'seed'+str(seed), concate_train_x, train_y, tree_outputs,
concate_test_x, test_y, gbdt2nn_model, opt,
args.max_epoch, args.batch_size, n_output, key)
_,pred_y = EvalTestset(concate_test_x, test_y, gbdt2nn_model, args.test_batch_size)
metric = eval_metrics(args.task, test_y, pred_y)
print('Final metrics: %s'%str(metric))
return gbdt2nn_model, opt, metric
def train_DEEPGBM(args, num_data, cate_data, plot_title, key="", trained_gbdt_model=None):
tree_layers = [int(x) for x in args.tree_layers.split(',')]
cate_layers = [int(x) for x in args.cate_layers.split(',')]
train_x, train_y, test_x, test_y = num_data
if trained_gbdt_model:
gbm, train_tree_pred = trained_gbdt_model
else:
gbm, train_tree_pred = TrainGBDT(train_x, train_y, test_x, test_y, args.tree_lr, args.ntrees, args.maxleaf, args.mindata, args.task)
gbms = SubGBDTLeaf_cls(train_x, test_x, gbm, args.maxleaf, num_slices=args.nslices, args = args)
min_len_features = train_x.shape[1]
used_features = []
tree_outputs = []
leaf_preds = []
test_leaf_preds = []
n_output = train_y.shape[1]
max_ntree_per_split = 0
group_average = []
for used_feature, new_train_y, leaf_pred, test_leaf_pred, avg, all_avg in gbms:
used_features.append(used_feature)
min_len_features = min(min_len_features, len(used_feature))
tree_outputs.append(new_train_y)
leaf_preds.append(leaf_pred)
test_leaf_preds.append(test_leaf_pred)
group_average.append(avg)
max_ntree_per_split = max(max_ntree_per_split, leaf_pred.shape[1])
for i in range(len(used_features)):
used_features[i] = sorted(used_features[i][:min_len_features])
n_models = len(used_features)
group_average = np.asarray(group_average).reshape(n_models, 1, 1)
for i in range(n_models):
if leaf_preds[i].shape[1] < max_ntree_per_split:
leaf_preds[i] = np.concatenate([leaf_preds[i] + 1,
np.zeros([leaf_preds[i].shape[0],
max_ntree_per_split-leaf_preds[i].shape[1]],
dtype=np.int32)], axis=1)
test_leaf_preds[i] = np.concatenate([test_leaf_preds[i] + 1,
np.zeros([test_leaf_preds[i].shape[0],
max_ntree_per_split-test_leaf_preds[i].shape[1]],
dtype=np.int32)], axis=1)
leaf_preds = np.concatenate(leaf_preds, axis=1)
test_leaf_preds = np.concatenate(test_leaf_preds, axis=1)
emb_model = EmbeddingModel(n_models, max_ntree_per_split, args.embsize, args.maxleaf+1, n_output, group_average, task=args.task).to(device)
tree_layers.append(args.embsize)
opt = AdamW(emb_model.parameters(), lr=args.emb_lr, weight_decay=args.l2_reg)
tree_outputs = np.asarray(tree_outputs).reshape((n_models, leaf_preds.shape[0])).transpose((1,0))
TrainWithLog(args, plot_title, leaf_preds, train_y, tree_outputs,
test_leaf_preds, test_y, emb_model, opt,
args.emb_epoch, args.batch_size, n_output, key+"emb-")
output_w = emb_model.bout.weight.data.cpu().numpy().reshape(n_models*args.embsize, n_output)
output_b = np.array(emb_model.bout.bias.data.cpu().numpy().sum())
train_embs = GetEmbPred(emb_model, emb_model.lastlayer, leaf_preds, args.test_batch_size)
del tree_outputs, leaf_preds, test_leaf_preds
gc.collect()
tree_outputs = train_embs
# cate_model dataset loading
train, test = cate_data
train_xc, _, feature_sizes = train
test_xc, _, _ = test
field_size = train_xc.shape[1]
concate_train_x = np.concatenate([train_x, np.zeros((train_x.shape[0],1), dtype=np.float32)], axis=-1)
concate_test_x = np.concatenate([test_x, np.zeros((test_x.shape[0],1), dtype=np.float32)], axis=-1)
del train_x, test_x
gc.collect()
for seed in args.seeds:
np.random.seed(seed)
torch.cuda.manual_seed_all(seed)
deepgbm_model = DeepGBM(concate_train_x.shape[1],np.asarray(used_features,dtype=np.int64),
tree_layers, output_w, output_b, args.task,
field_size, feature_sizes,
embedding_size=args.embsize).to(device)
opt = AdamW(deepgbm_model.parameters(), lr=args.lr, weight_decay=args.l2_reg, amsgrad=False, model_decay_opt=deepgbm_model, weight_decay_opt=args.l2_reg_opt, key_opt='deepfm')
TrainWithLog(args, plot_title+'seed'+str(seed), concate_train_x, train_y, tree_outputs,
concate_test_x, test_y, deepgbm_model, opt,
args.max_epoch, args.batch_size, n_output, key,
train_x_opt=train_xc, test_x_opt=test_xc)
_,pred_y = EvalTestset(concate_test_x, test_y, deepgbm_model, args.test_batch_size, test_x_opt=test_xc)
metric = eval_metrics(args.task, test_y, pred_y)
print('Final metrics: %s'%str(metric))
return deepgbm_model, opt, metric