model.py 4.7 KB
Newer Older
M
malin10 已提交
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
# 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.fluid as fluid

from paddlerec.core.utils import envs
from paddlerec.core.model import Model as ModelBase


class Model(ModelBase):
    def __init__(self, config):
        ModelBase.__init__(self, config)

    def _init_hyper_parameters(self):
        self.is_distributed = True if envs.get_trainer(
        ) == "CtrTrainer" else False
        self.sparse_feature_number = envs.get_global_env(
            "hyper_parameters.sparse_feature_number")
        self.sparse_feature_dim = envs.get_global_env(
            "hyper_parameters.sparse_feature_dim")
        self.learning_rate = envs.get_global_env(
            "hyper_parameters.optimizer.learning_rate")
        self.hidden_layers = envs.get_global_env("hyper_parameters.fc_sizes")

    def net(self, input, is_infer=False):
        self.user_sparse_inputs = self._sparse_data_var[2:6]
        self.mov_sparse_inputs = self._sparse_data_var[6:9]

        self.label_input = self._sparse_data_var[-1]

        def fc(input):
            fcs = [input]
            for size in self.hidden_layers:
                output = fluid.layers.fc(
                    input=fcs[-1],
                    size=size,
                    act='relu',
                    param_attr=fluid.ParamAttr(
                        initializer=fluid.initializer.Normal(
                            scale=1.0 / math.sqrt(fcs[-1].shape[1]))))
                fcs.append(output)
            return fcs[-1]

        def embedding_layer(input):
            emb = fluid.layers.embedding(
                input=input,
                is_sparse=True,
                is_distributed=self.is_distributed,
                size=[self.sparse_feature_number, self.sparse_feature_dim],
                param_attr=fluid.ParamAttr(
                    name="emb", initializer=fluid.initializer.Uniform()), )
            emb_sum = fluid.layers.sequence_pool(input=emb, pool_type='sum')
            return emb_sum

        user_sparse_embed_seq = list(
            map(embedding_layer, self.user_sparse_inputs))
        mov_sparse_embed_seq = list(
            map(embedding_layer, self.mov_sparse_inputs))
        concated_user = fluid.layers.concat(user_sparse_embed_seq, axis=1)
        concated_mov = fluid.layers.concat(mov_sparse_embed_seq, axis=1)

        usr_combined_features = fc(concated_user)
        mov_combined_features = fc(concated_mov)

        fc_input = fluid.layers.concat(
            [usr_combined_features, mov_combined_features], axis=1)
        sim = fluid.layers.fc(
            input=fc_input,
            size=1,
            act='sigmoid',
            param_attr=fluid.ParamAttr(initializer=fluid.initializer.Normal(
                scale=1.0 / math.sqrt(fc_input.shape[1]))))

        predict = fluid.layers.scale(sim, scale=5)
        self.predict = predict
        #auc, batch_auc, _ = fluid.layers.auc(input=self.predict,
        #                                     label=self.label_input,
        #                                     num_thresholds=10000,
        #                                     slide_steps=20)

        if is_infer:
            self._infer_results["user_feature"] = usr_combined_features
            self._infer_results["movie_feature"] = mov_combined_features
            self._infer_results["uid"] = self._sparse_data_var[2]
            self._infer_results["movieid"] = self._sparse_data_var[6]
            self._infer_results["label"] = self._sparse_data_var[-1]
            self._infer_results["predict"] = self.predict
            return

        #self._metrics["AUC"] = auc
        #self._metrics["BATCH_AUC"] = batch_auc
        #cost = fluid.layers.cross_entropy(
        #    input=self.predict, label=self.label_input)
        cost = fluid.layers.square_error_cost(
            self.predict,
            fluid.layers.cast(
                x=self.label_input, dtype='float32'))
        avg_cost = fluid.layers.reduce_mean(cost)
        self._cost = avg_cost
        self._metrics["LOSS"] = avg_cost

    def optimizer(self):
        optimizer = fluid.optimizer.Adam(self.learning_rate, lazy_mode=True)
        return optimizer

    def infer_net(self):
        pass