-
Notifications
You must be signed in to change notification settings - Fork 0
/
static_model.py
156 lines (127 loc) · 6.22 KB
/
static_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
# Copyright (c) 2020 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 math
import paddle
import numpy as np
#from net import DIENLayer, StaticDIENLayer
from net import StaticDIENLayer
class StaticModel():
def __init__(self, config):
self.cost = None
self.infer_target_var = None
self.config = config
self._init_hyper_parameters()
def _init_hyper_parameters(self):
self.is_distributed = False
self.distributed_embedding = False
if self.config.get("hyper_parameters.distributed_embedding", 0) == 1:
self.distributed_embedding = True
self.item_emb_size = self.config.get("hyper_parameters.item_emb_size",
64)
self.cat_emb_size = self.config.get("hyper_parameters.cat_emb_size",
64)
self.act = self.config.get("hyper_parameters.act", "sigmoid")
self.is_sparse = self.config.get("hyper_parameters.is_sparse", False)
self.use_DataLoader = self.config.get(
"hyper_parameters.use_DataLoader", False)
self.item_count = self.config.get("hyper_parameters.item_count", 63001)
self.cat_count = self.config.get("hyper_parameters.cat_count", 801)
self.learning_rate_base_lr = self.config.get(
"hyper_parameters.optimizer.learning_rate_base_lr")
def create_feeds(self, is_infer=False):
seq_len = -1
self.data_var = []
hist_item_seq = paddle.static.data(
name="hist_item_seq", shape=[None, seq_len], dtype="int64")
self.data_var.append(hist_item_seq)
hist_cat_seq = paddle.static.data(
name="hist_cat_seq", shape=[None, seq_len], dtype="int64")
self.data_var.append(hist_cat_seq)
target_item = paddle.static.data(
name="target_item", shape=[None], dtype="int64")
self.data_var.append(target_item)
target_cat = paddle.static.data(
name="target_cat", shape=[None], dtype="int64")
self.data_var.append(target_cat)
label = paddle.static.data(
name="label", shape=[-1, 1], dtype="float32")
self.data_var.append(label)
mask = paddle.static.data(
name="mask", shape=[None, seq_len, 1], dtype="float32")
self.data_var.append(mask)
target_item_seq = paddle.static.data(
name="target_item_seq", shape=[None, seq_len], dtype="int64")
self.data_var.append(target_item_seq)
target_cat_seq = paddle.static.data(
name="target_cat_seq", shape=[None, seq_len], dtype="int64")
self.data_var.append(target_cat_seq)
neg_hist_item_seq = paddle.static.data(
name="neg_hist_item_seq", shape=[None, seq_len], dtype="int64")
self.data_var.append(neg_hist_item_seq)
neg_hist_cat_seq = paddle.static.data(
name="neg_hist_cat_seq", shape=[None, seq_len], dtype="int64")
self.data_var.append(neg_hist_cat_seq)
train_inputs = [hist_item_seq] + [hist_cat_seq] + [target_item] + [
target_cat
] + [label] + [mask] + [target_item_seq] + [target_cat_seq] + [
neg_hist_item_seq
] + [neg_hist_cat_seq]
return train_inputs
def net(self, inputs, is_infer=False):
self.hist_item_seq = inputs[0]
self.hist_cat_seq = inputs[1]
self.target_item = inputs[2]
self.target_cat = inputs[3]
self.label = inputs[4].reshape([-1, 1])
self.mask = inputs[5]
self.target_item_seq = inputs[6]
self.target_cat_seq = inputs[7]
self.neg_hist_item_seq = inputs[8] # neg item sampling for aux loss
self.neg_hist_cat_seq = inputs[9] # neg cat sampling for aux loss
dien_model = StaticDIENLayer(
self.item_emb_size, self.cat_emb_size, self.act, self.is_sparse,
self.use_DataLoader, self.item_count, self.cat_count)
logit, aux_loss = dien_model.forward(
self.hist_item_seq, self.hist_cat_seq, self.target_item,
self.target_cat, self.label, self.mask, self.target_item_seq,
self.target_cat_seq, self.neg_hist_item_seq, self.neg_hist_cat_seq)
avg_loss = paddle.nn.functional.binary_cross_entropy_with_logits(
logit, self.label, reduction='mean')
self._cost = aux_loss + avg_loss
self.predict = paddle.nn.functional.sigmoid(logit)
predict_2d = paddle.concat([1 - self.predict, self.predict], 1)
label_int = paddle.cast(self.label, 'int64')
auc, batch_auc, _ = paddle.static.auc(input=predict_2d,
label=label_int,
slide_steps=0)
if is_infer:
fetch_dict = {'auc': auc}
return fetch_dict
fetch_dict = {'auc': auc, 'cost': self._cost}
return fetch_dict
def create_optimizer(self, strategy=None):
# optimizer = paddle.optimizer.Adam(learning_rate=self.learning_rate)
# if strategy != None:
# import paddle.distributed.fleet as fleet
# optimizer = fleet.distributed_optimizer(optimizer, strategy)
# optimizer.minimize(self._cost)
boundaries = [410000]
values = [self.learning_rate_base_lr, 0.2]
optimizer = paddle.optimizer.SGD(
learning_rate=paddle.optimizer.lr.PiecewiseDecay(
boundaries=boundaries, values=values))
if strategy != None:
import paddle.distributed.fleet as fleet
optimizer = fleet.distributed_optimizer(optimizer, strategy)
optimizer.minimize(self._cost)
return optimizer
def infer_net(self, input):
return self.net(input, is_infer=True)