Skip to content

Commit 5b28e63

Browse files
committed
Add MetaHeac model
1 parent c3f4873 commit 5b28e63

File tree

11 files changed

+1118
-0
lines changed

11 files changed

+1118
-0
lines changed
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
16+
runner:
17+
train_data_dir: "./data/train"
18+
train_reader_path: "reader_train" # importlib format
19+
use_gpu: True
20+
use_auc: True
21+
# train_batch_size: 32
22+
epochs: 1
23+
print_interval: 100
24+
#model_init_path: "output_model_esmm/2" # init model
25+
model_save_path: "output_model_esmm"
26+
test_data_dir: "./data/test"
27+
# infer_batch_size: 32
28+
infer_reader_path: "reader_infer" # importlib format
29+
infer_load_path: "output_model_esmm"
30+
infer_start_epoch: 0
31+
infer_end_epoch: 1
32+
#use inference save model
33+
use_inference: False
34+
infer_train_epoch: 2
35+
36+
hyper_parameters:
37+
max_idxs: [[3, 2, 855, 5, 7, 2, 1], [124, 82, 12, 263312, 49780, 10002, 9984], [78, 137, 14, 39,32,3]]
38+
embed_dim: 64
39+
mlp_dims: [64, 64]
40+
local_lr: 0.0002
41+
num_expert: 8
42+
num_output: 5
43+
task_count: 5
44+
batch_size: 2
45+
46+
optimizer:
47+
class: adam
48+
global_learning_rate: 0.001
49+
local_test_learning_rate: 0.001
50+
strategy: async
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
# Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
16+
runner:
17+
train_data_dir: "../../../../data/data137870/Lookalike_data/train"
18+
train_reader_path: "reader_train" # importlib format
19+
use_gpu: True
20+
use_auc: True
21+
# train_batch_size: 32
22+
epochs: 1
23+
print_interval: 100
24+
#model_init_path: "output_model_esmm/2" # init model
25+
model_save_path: "output_model_esmm"
26+
# test_data_dir: "../../../../data/data137870/Lookalike_data/infer_hot"
27+
test_data_dir: "../../../../data/data137870/Lookalike_data/infer_cold"
28+
# infer_batch_size: 32
29+
infer_reader_path: "reader_infer" # importlib format
30+
infer_load_path: "output_model_esmm"
31+
infer_start_epoch: 0
32+
infer_end_epoch: 1
33+
#use inference save model
34+
use_inference: False
35+
infer_train_epoch: 2
36+
37+
hyper_parameters:
38+
max_idxs: [[3, 2, 855, 5, 7, 2, 1], [124, 82, 12, 263312, 49780, 10002, 9984], [78, 137, 14, 39,32,3]]
39+
embed_dim: 64
40+
mlp_dims: [64, 64]
41+
local_lr: 0.0002
42+
num_expert: 8
43+
num_output: 5
44+
task_count: 5
45+
batch_size: 1024
46+
47+
optimizer:
48+
class: adam
49+
global_learning_rate: 0.001
50+
local_test_learning_rate: 0.001
51+
strategy: async
466 KB
Binary file not shown.
466 KB
Binary file not shown.
466 KB
Binary file not shown.
Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
# Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
import paddle
16+
import paddle.nn as nn
17+
import paddle.nn.functional as F
18+
import math
19+
import numpy as np
20+
import pickle
21+
import net
22+
23+
24+
class DygraphModel():
25+
# define model
26+
def create_model(self, config):
27+
max_idxs = config.get("hyper_parameters.max_idxs")
28+
embed_dim = config.get("hyper_parameters.embed_dim")
29+
mlp_dims = config.get("hyper_parameters.mlp_dims")
30+
31+
num_expert = config.get("hyper_parameters.num_expert")
32+
num_output = config.get("hyper_parameters.num_output")
33+
34+
meta_model = net.WideAndDeepModel(max_idxs, embed_dim, mlp_dims,
35+
num_expert, num_output)
36+
# model_state_dict = paddle.load('paddle.pkl')
37+
# meta_model.set_dict(model_state_dict)
38+
39+
return meta_model
40+
41+
# define feeds which convert numpy of batch data to paddle.tensor
42+
def create_feeds(self, batch_data, config):
43+
x_spt = batch_data[0]
44+
y_spt = batch_data[1]
45+
46+
x_qry = batch_data[2]
47+
y_qry = batch_data[3]
48+
return x_spt, y_spt, x_qry, y_qry
49+
50+
# define loss function by predicts and label
51+
def create_loss(self, pred, y_label):
52+
53+
loss_ctr = paddle.nn.functional.log_loss(
54+
input=pred, label=paddle.cast(
55+
y_label, dtype="float32"))
56+
return loss_ctr
57+
58+
# define optimizer
59+
def create_optimizer(self, dy_model, config, mode="train"):
60+
if mode == "train":
61+
lr = config.get("hyper_parameters.optimizer.global_learning_rate",
62+
0.001)
63+
optimizer = paddle.optimizer.Adam(
64+
learning_rate=lr, parameters=dy_model.parameters())
65+
else:
66+
lr = config.get(
67+
"hyper_parameters.optimizer.local_test_learning_rate", 0.001)
68+
optimizer = paddle.optimizer.Adam(
69+
learning_rate=lr, parameters=dy_model.parameters())
70+
return optimizer
71+
72+
# define metrics such as auc/acc
73+
# multi-task need to define multi metric
74+
def create_metrics(self):
75+
metrics_list_name = ["AUC"]
76+
auc_ctr_metric = paddle.metric.Auc("ROC")
77+
metrics_list = [auc_ctr_metric]
78+
return metrics_list, metrics_list_name
79+
80+
# construct train forward phase
81+
def train_forward(self, dy_model, metric_list, batch, config):
82+
# print(len(batch))
83+
# exit(0)
84+
x_spt, y_spt, x_qry, y_qry = self.create_feeds(batch, config)
85+
86+
task_count = config.get("hyper_parameters.task_count", 5)
87+
local_lr = config.get("hyper_parameters.local_lr", 0.0002)
88+
criterion = paddle.nn.BCELoss()
89+
90+
losses_q = []
91+
dy_model.clear_gradients()
92+
for i in range(task_count):
93+
## local update --------------
94+
fast_parameters = list(dy_model.parameters())
95+
for weight in fast_parameters:
96+
weight.fast = None
97+
98+
support_set_y_pred = dy_model(x_spt[i])
99+
label = paddle.squeeze(y_spt[i].astype('float32'))
100+
101+
loss = criterion(support_set_y_pred, label)
102+
dy_model.clear_gradients()
103+
loss.backward()
104+
105+
fast_parameters = list(dy_model.parameters())
106+
for weight in fast_parameters:
107+
if weight.grad is None:
108+
continue
109+
if weight.fast is None:
110+
weight.fast = weight - local_lr * weight.grad # create weight.fast
111+
else:
112+
weight.fast = weight.fast - local_lr * weight.grad
113+
dy_model.clear_gradients()
114+
## local update --------------
115+
116+
query_set_y_pred = dy_model(x_qry[i])
117+
label = paddle.squeeze(y_qry[i].astype('float32'))
118+
loss_q = criterion(query_set_y_pred, label)
119+
losses_q.append(loss_q)
120+
121+
loss_average = paddle.stack(losses_q).mean(0)
122+
print_dict = {'loss': loss_average}
123+
124+
return loss_average, metric_list, print_dict
125+
126+
def infer_train_forward(self, dy_model, batch, config):
127+
batch_x, batch_y = batch[0], batch[1]
128+
criterion = paddle.nn.BCELoss()
129+
130+
pred = dy_model.forward(batch_x)
131+
132+
label = paddle.squeeze(batch_y.astype('float32'))
133+
loss_q = criterion(pred, label)
134+
135+
return loss_q
136+
137+
def infer_forward(self, dy_model, metric_list, metric_list_local, batch,
138+
config):
139+
batch_x, batch_y = batch[0], batch[1]
140+
pred = dy_model.forward(batch_x)
141+
label = paddle.squeeze(batch_y.astype('float32'))
142+
143+
pred = paddle.unsqueeze(pred, 1)
144+
pred = paddle.concat([1 - pred, pred], 1)
145+
146+
metric_list[0].update(preds=pred.numpy(), labels=label.numpy())
147+
metric_list_local[0].update(preds=pred.numpy(), labels=label.numpy())
148+
149+
return metric_list, metric_list_local

0 commit comments

Comments
 (0)