-
Notifications
You must be signed in to change notification settings - Fork 646
/
Copy pathdygraph_model.py
168 lines (142 loc) · 6.2 KB
/
dygraph_model.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
# Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import paddle
import paddle.nn as nn
from net import DPINLayer
class DygraphModel():
# define model
def create_model(self, config):
K = config.get("hyper_parameters.K", 3)
emb_dim = config.get("hyper_parameters.embedding_dim", 8)
is_sparse = config.get("hyper_parameters.is_sparse", True)
max_item = config.get("hyper_parameters.max_item", 4975)
max_context = config.get("hyper_parameters.max_context", 1933)
d_model = config.get("hyper_parameters.d_model", 64)
h = config.get("hyper_parameters.h", 2)
model = DPINLayer(K, emb_dim, max_item, max_context, d_model, h,
is_sparse)
return model
# define feeds which convert numpy of batch data to paddle.tensor
def create_feeds(self, batch, config):
hist_item = batch[0]
hist_cat = batch[1]
target_item = batch[2]
target_cat = batch[3]
target_pos = batch[4]
position = batch[5]
label = batch[6]
return hist_item, hist_cat, target_item, target_cat, target_pos, position, label
# define loss function by predicts and label
def create_loss(self, pred, label):
loss = nn.functional.binary_cross_entropy(pred, label)
return loss
# define optimizer
def create_optimizer(self, dy_model, config):
lr = config.get("hyper_parameters.optimizer.learning_rate", 0.05)
optimizer = paddle.optimizer.SGD(learning_rate=lr,
parameters=dy_model.parameters())
return optimizer
# define metrics such as auc/acc
def create_metrics(self):
metrics_list_name = ["AUC", "PAUC"]
auc_metric = paddle.metric.Auc()
pauc_metric = PAUC()
metrics_list = [auc_metric, pauc_metric]
return metrics_list, metrics_list_name
# construct train forward phase
def train_forward(self, dy_model, metrics_list, batch_data, config):
hist_item, hist_cat, target_item, target_cat, target_pos, position, label = self.create_feeds(
batch_data, config)
output = dy_model.forward(hist_item, hist_cat, target_item, target_cat,
position)
pauc_position = target_pos
output = paddle.squeeze(output)
target_pos = paddle.unsqueeze(target_pos - 1, axis=1)
line = paddle.arange(
config.get("runner.train_batch_size", 32), dtype="int64")
line = paddle.unsqueeze(line, axis=1)
target_pos = paddle.concat([line, target_pos], axis=1)
output = paddle.gather_nd(output, target_pos)
## loss
loss = self.create_loss(output, label)
## AUC
output = paddle.unsqueeze(output, axis=1)
output = paddle.concat([1 - output, output], axis=1)
label = paddle.unsqueeze(label, axis=1)
metrics_list[0].update(preds=output, labels=label)
## PAUC
K = config.get("hyper_parameters.K", 3)
metrics_list[1].update(
preds=output, labels=label, position=pauc_position, K=K)
print_dict = {'loss': loss}
return loss, metrics_list, print_dict
# construct infer forward phase
def infer_forward(self, dy_model, metrics_list, batch_data, config):
hist_item, hist_cat, target_item, target_cat, target_pos, position, label = self.create_feeds(
batch_data, config)
output = dy_model.forward(hist_item, hist_cat, target_item, target_cat,
position)
pauc_position = target_pos
output = paddle.squeeze(output)
target_pos = paddle.unsqueeze(target_pos - 1, axis=1)
line = paddle.arange(
config.get("runner.train_batch_size", 32), dtype="int64")
line = paddle.unsqueeze(line, axis=1)
target_pos = paddle.concat([line, target_pos], axis=1)
output = paddle.gather_nd(output, target_pos)
## AUC
output = paddle.unsqueeze(output, axis=1)
output = paddle.concat([1 - output, output], axis=1)
label = paddle.unsqueeze(label, axis=1)
## PAUC
K = config.get("hyper_parameters.K", 3)
metrics_list[1].update(
preds=output, labels=label, position=pauc_position, K=K)
metrics_list[0].update(preds=output, labels=label)
return metrics_list, None
class PAUC(paddle.metric.Metric):
def __init__(self):
super(PAUC, self).__init__()
self.auc_list = []
self.num_list = []
self.m = paddle.metric.Auc()
def name(self):
return 'PAUC'
def update(self, preds, labels, position, K):
if isinstance(preds, paddle.Tensor):
preds = preds.numpy()
if isinstance(labels, paddle.Tensor):
labels = labels.numpy()
if isinstance(position, paddle.Tensor):
position = position.numpy()
# calculate the number of impression
# calculate the auc of each position
for i in range(K):
## the number of impression
self.num_list.append(len(position[position == i + 1]))
## the auc of each position
self.m.reset()
self.m.update(
preds=preds[position == i + 1],
labels=labels[position == i + 1])
self.auc_list.append(self.m.accumulate())
def accumulate(self):
sum = 0
_pauc = .0
for i in range(len(self.auc_list)):
sum += self.num_list[i]
_pauc += self.num_list[i] * self.auc_list[i]
return float(_pauc / sum) if sum != 0 else .0
def reset(self):
self.auc_list = []
self.num_list = []
self.m.reset()