gmf.py 1.6 KB
Newer Older
O
overlordmax 已提交
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
import numpy as np
import paddle.fluid as fluid
import sys
import math
from time import time

class GMF(object):
    def net(self, inputs, num_users, num_items, latent_dim):
        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)
        
        predict_vector = fluid.layers.elementwise_mul(MF_Embedding_User, MF_Embedding_Item)
        
        prediction = fluid.layers.fc(input=predict_vector,
                                    size=1,
                                    act='sigmoid',
                                    param_attr=fluid.initializer.MSRAInitializer(uniform=True), 
                                    name='prediction')
                                    
        cost = fluid.layers.log_loss(input=prediction, label=fluid.layers.cast(x=inputs[2], dtype='float32'))
        avg_cost = fluid.layers.mean(cost)
        
        return avg_cost, prediction