Skip to content

Commit e33674a

Browse files
committed
metaheac提交
1 parent 2959f3b commit e33674a

File tree

11 files changed

+1008
-0
lines changed

11 files changed

+1008
-0
lines changed
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# Copyright (c) 2020 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: 4
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) 2020 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
3.78 MB
Binary file not shown.
3.78 MB
Binary file not shown.
3.78 MB
Binary file not shown.
Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
# Copyright (c) 2020 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, num_expert, num_output)
35+
# model_state_dict = paddle.load('paddle.pkl')
36+
# meta_model.set_dict(model_state_dict)
37+
38+
return meta_model
39+
40+
# define feeds which convert numpy of batch data to paddle.tensor
41+
def create_feeds(self, batch_data, config):
42+
x_spt = batch_data[0]
43+
y_spt = batch_data[1]
44+
45+
x_qry = batch_data[2]
46+
y_qry = batch_data[3]
47+
return x_spt, y_spt, x_qry, y_qry
48+
49+
# define loss function by predicts and label
50+
def create_loss(self, pred, y_label):
51+
52+
loss_ctr = paddle.nn.functional.log_loss(
53+
input=pred, label=paddle.cast(
54+
y_label, dtype="float32"))
55+
return loss_ctr
56+
57+
# define optimizer
58+
def create_optimizer(self, dy_model, config, mode="train"):
59+
if mode == "train":
60+
lr = config.get("hyper_parameters.optimizer.global_learning_rate", 0.001)
61+
optimizer = paddle.optimizer.Adam(learning_rate=lr, parameters=dy_model.parameters())
62+
else:
63+
lr = config.get("hyper_parameters.optimizer.local_test_learning_rate", 0.001)
64+
optimizer = paddle.optimizer.Adam(learning_rate=lr, parameters=dy_model.parameters())
65+
return optimizer
66+
67+
# define metrics such as auc/acc
68+
# multi-task need to define multi metric
69+
def create_metrics(self):
70+
metrics_list_name = ["AUC"]
71+
auc_ctr_metric = paddle.metric.Auc("ROC")
72+
metrics_list = [auc_ctr_metric]
73+
return metrics_list, metrics_list_name
74+
75+
# construct train forward phase
76+
def train_forward(self, dy_model, metric_list, batch, config):
77+
# print(len(batch))
78+
# exit(0)
79+
x_spt, y_spt, x_qry, y_qry = self.create_feeds(batch, config)
80+
81+
task_count = config.get("hyper_parameters.task_count",5)
82+
local_lr = config.get("hyper_parameters.local_lr",0.0002)
83+
criterion = paddle.nn.BCELoss()
84+
85+
losses_q = []
86+
dy_model.clear_gradients()
87+
for i in range(task_count):
88+
## local update --------------
89+
fast_parameters = list(dy_model.parameters())
90+
for weight in fast_parameters:
91+
weight.fast = None
92+
93+
support_set_y_pred = dy_model(x_spt[i])
94+
label = paddle.squeeze(y_spt[i].astype('float32'))
95+
96+
loss = criterion(support_set_y_pred, label)
97+
dy_model.clear_gradients()
98+
loss.backward()
99+
100+
fast_parameters = list(dy_model.parameters())
101+
for weight in fast_parameters:
102+
if weight.grad is None:
103+
continue
104+
if weight.fast is None:
105+
weight.fast = weight - local_lr * weight.grad # create weight.fast
106+
else:
107+
weight.fast = weight.fast - local_lr * weight.grad
108+
dy_model.clear_gradients()
109+
## local update --------------
110+
111+
query_set_y_pred = dy_model(x_qry[i])
112+
label = paddle.squeeze(y_qry[i].astype('float32'))
113+
loss_q = criterion(query_set_y_pred, label)
114+
losses_q.append(loss_q)
115+
116+
loss_average = paddle.stack(losses_q).mean(0)
117+
print_dict = {'loss': loss_average}
118+
119+
return loss_average, metric_list, print_dict
120+
121+
def infer_train_forward(self, dy_model, batch, config):
122+
batch_x, batch_y = batch[0], batch[1]
123+
criterion = paddle.nn.BCELoss()
124+
125+
pred = dy_model.forward(batch_x)
126+
127+
label = paddle.squeeze(batch_y.astype('float32'))
128+
loss_q = criterion(pred, label)
129+
130+
return loss_q
131+
132+
def infer_forward(self, dy_model, metric_list, metric_list_local, batch, config):
133+
batch_x, batch_y = batch[0], batch[1]
134+
pred = dy_model.forward(batch_x)
135+
label = paddle.squeeze(batch_y.astype('float32'))
136+
137+
pred = paddle.unsqueeze(pred,1)
138+
pred = paddle.concat([1-pred,pred],1)
139+
140+
metric_list[0].update(preds=pred.numpy(), labels=label.numpy())
141+
metric_list_local[0].update(preds=pred.numpy(), labels=label.numpy())
142+
143+
return metric_list, metric_list_local

0 commit comments

Comments
 (0)