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 net
18+ import numpy as np
19+
20+
21+ class DygraphModel ():
22+ # define model
23+ def create_model (self , config ):
24+ sparse_input_slot = config .get ('hyper_parameters.sparse_inputs_slots' )
25+ dense_input_slot = config .get ('hyper_parameters.dense_inputs_slots' )
26+ sparse_feature_size = config .get ("hyper_parameters.sparse_feature_size" )
27+ feature_name = config .get ("hyper_parameters.feature_name" )
28+ feature_dim = config .get ("hyper_parameters.feature_dim" , 20 )
29+ conv_kernel_width = config .get ("hyper_parameters.conv_kernel_width" , (7 , 7 , 7 , 7 ))
30+ conv_filters = config .get ("hyper_parameters.conv_filters" , (14 , 16 , 18 , 20 ))
31+ new_maps = config .get ("hyper_parameters.new_maps" , (3 , 3 , 3 , 3 ))
32+ pooling_width = config .get ("hyper_parameters.pooling_width" , (2 , 2 , 2 , 2 ))
33+ stride = config .get ("hyper_parameters.stride" , (1 ,1 ))
34+ dnn_hidden_units = config .get ("hyper_parameters.dnn_hidden_units" , (128 ,))
35+ dnn_dropout = config .get ("hyper_parameters.dnn_dropout" , 0.0 )
36+ fgcnn_model = net .FGCNN (sparse_input_slot , sparse_feature_size ,
37+ feature_name , feature_dim ,dense_input_slot ,
38+ conv_kernel_width , conv_filters , new_maps ,
39+ pooling_width , stride , dnn_hidden_units , dnn_dropout )
40+
41+ return fgcnn_model
42+
43+ # define feeds which convert numpy of batch data to paddle.tensor
44+ def create_feeds (self , batch_data , config ):
45+ # print(len(batch_data))
46+ inputs = paddle .to_tensor (np .array (batch_data [0 ]).astype ('int64' ))
47+ inputs = batch_data [0 ]
48+ label = batch_data [1 ]
49+ return label , inputs
50+
51+
52+ # define loss function by predicts and label
53+ def create_loss (self , y_pred , label ):
54+ loss = nn .functional .log_loss (
55+ y_pred , label = paddle .cast (
56+ label , dtype = "float32" ))
57+ avg_cost = paddle .mean (x = loss )
58+ return avg_cost
59+
60+ # define optimizer
61+ def create_optimizer (self , dy_model , config ):
62+ lr = config .get ("hyper_parameters.optimizer.learning_rate" , 1e-3 )
63+ optimizer = paddle .optimizer .Adam (
64+ parameters = dy_model .parameters (),
65+ learning_rate = lr )
66+ return optimizer
67+
68+ def create_metrics (self ):
69+ metrics_list_name = ["auc" ]
70+ auc_metric = paddle .metric .Auc ("ROC" )
71+ metrics_list = [auc_metric ]
72+ return metrics_list , metrics_list_name
73+
74+ # construct train forward phase
75+ def train_forward (self , dy_model , metrics_list , batch_data , config ):
76+ # 稠密向量
77+ label , inputs = self .create_feeds (batch_data , config )
78+ pred = dy_model .forward (inputs )
79+ loss = self .create_loss (pred , label )
80+ # update metrics
81+ predict_2d = paddle .concat (x = [1 - pred , pred ], axis = 1 )
82+ metrics_list [0 ].update (preds = predict_2d .numpy (), labels = label .numpy ())
83+ # print_dict format :{'loss': loss}
84+ print_dict = {'loss' : loss }
85+ return loss , metrics_list , print_dict
86+
87+ def infer_forward (self , dy_model , metrics_list , batch_data , config ):
88+ # label, sparse_tensor = self.create_feeds(batch_data, config)
89+ label , inputs = self .create_feeds (batch_data , config )
90+ pred = dy_model .forward (inputs )
91+ # pred = dy_model.forward(sparse_tensor)
92+ loss = self .create_loss (pred , label )
93+ # update metrics
94+ predict_2d = paddle .concat (x = [1 - pred , pred ], axis = 1 )
95+ metrics_list [0 ].update (preds = predict_2d .numpy (), labels = label .numpy ())
96+ # print_dict format :{'loss': loss}
97+ print_dict = {'loss' : loss }
98+ return metrics_list , print_dict
0 commit comments