model.py 5.2 KB
Newer Older
F
frankwhzhang 已提交
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
# 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
import numpy as np


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

    def input_data(self, is_infer=False):
T
tangwei 已提交
28 29 30 31 32 33
        user_input = fluid.data(
            name="user_input", shape=[-1, 1], dtype="int64", lod_level=0)
        item_input = fluid.data(
            name="item_input", shape=[-1, 1], dtype="int64", lod_level=0)
        label = fluid.data(
            name="label", shape=[-1, 1], dtype="int64", lod_level=0)
F
frankwhzhang 已提交
34 35 36 37 38 39 40
        if is_infer:
            inputs = [user_input] + [item_input]
        else:
            inputs = [user_input] + [item_input] + [label]
            self._data_var = inputs

        return inputs
T
tangwei 已提交
41

F
frankwhzhang 已提交
42 43
    def net(self, inputs, is_infer=False):

T
tangwei 已提交
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
        num_users = envs.get_global_env("hyper_parameters.num_users", None,
                                        self._namespace)
        num_items = envs.get_global_env("hyper_parameters.num_items", None,
                                        self._namespace)
        latent_dim = envs.get_global_env("hyper_parameters.latent_dim", None,
                                         self._namespace)
        layers = envs.get_global_env("hyper_parameters.layers", None,
                                     self._namespace)

        num_layer = len(layers)  #Number of layers in the MLP

        MF_Embedding_User = fluid.embedding(
            input=inputs[0],
            size=[num_users, latent_dim],
            param_attr=fluid.initializer.Normal(
                loc=0.0, scale=0.01),
            is_sparse=True)
        MF_Embedding_Item = fluid.embedding(
            input=inputs[1],
            size=[num_items, latent_dim],
            param_attr=fluid.initializer.Normal(
                loc=0.0, scale=0.01),
            is_sparse=True)

        MLP_Embedding_User = fluid.embedding(
            input=inputs[0],
            size=[num_users, int(layers[0] / 2)],
            param_attr=fluid.initializer.Normal(
                loc=0.0, scale=0.01),
            is_sparse=True)
        MLP_Embedding_Item = fluid.embedding(
            input=inputs[1],
            size=[num_items, int(layers[0] / 2)],
            param_attr=fluid.initializer.Normal(
                loc=0.0, scale=0.01),
            is_sparse=True)

F
frankwhzhang 已提交
81 82 83
        # MF part
        mf_user_latent = fluid.layers.flatten(x=MF_Embedding_User, axis=1)
        mf_item_latent = fluid.layers.flatten(x=MF_Embedding_Item, axis=1)
T
tangwei 已提交
84 85 86
        mf_vector = fluid.layers.elementwise_mul(mf_user_latent,
                                                 mf_item_latent)

F
frankwhzhang 已提交
87 88 89 90
        # MLP part 
        # The 0-th layer is the concatenation of embedding layers
        mlp_user_latent = fluid.layers.flatten(x=MLP_Embedding_User, axis=1)
        mlp_item_latent = fluid.layers.flatten(x=MLP_Embedding_Item, axis=1)
T
tangwei 已提交
91 92 93
        mlp_vector = fluid.layers.concat(
            input=[mlp_user_latent, mlp_item_latent], axis=-1)

F
frankwhzhang 已提交
94
        for i in range(1, num_layer):
T
tangwei 已提交
95 96 97 98 99 100 101 102 103 104 105
            mlp_vector = fluid.layers.fc(
                input=mlp_vector,
                size=layers[i],
                act='relu',
                param_attr=fluid.ParamAttr(
                    initializer=fluid.initializer.TruncatedNormal(
                        loc=0.0, scale=1.0 / math.sqrt(mlp_vector.shape[1])),
                    regularizer=fluid.regularizer.L2DecayRegularizer(
                        regularization_coeff=1e-4)),
                name='layer_' + str(i))

F
frankwhzhang 已提交
106
        # Concatenate MF and MLP parts
T
tangwei 已提交
107 108
        predict_vector = fluid.layers.concat(
            input=[mf_vector, mlp_vector], axis=-1)
F
frankwhzhang 已提交
109 110

        # Final prediction layer
T
tangwei 已提交
111 112 113 114 115 116
        prediction = fluid.layers.fc(
            input=predict_vector,
            size=1,
            act='sigmoid',
            param_attr=fluid.initializer.MSRAInitializer(uniform=True),
            name='prediction')
F
frankwhzhang 已提交
117 118 119
        if is_infer:
            self._infer_results["prediction"] = prediction
            return
T
tangwei 已提交
120 121 122 123 124

        cost = fluid.layers.log_loss(
            input=prediction,
            label=fluid.layers.cast(
                x=inputs[2], dtype='float32'))
F
frankwhzhang 已提交
125 126 127 128 129 130 131 132 133 134 135 136
        avg_cost = fluid.layers.mean(cost)

        self._cost = avg_cost
        self._metrics["cost"] = avg_cost

    def train_net(self):
        input_data = self.input_data()
        self.net(input_data)

    def infer_net(self):
        self._infer_data_var = self.input_data(is_infer=True)
        self._infer_data_loader = fluid.io.DataLoader.from_generator(
T
tangwei 已提交
137 138 139 140
            feed_list=self._infer_data_var,
            capacity=64,
            use_double_buffer=False,
            iterable=False)
F
frankwhzhang 已提交
141
        self.net(self._infer_data_var, is_infer=True)