DREAM/train.py
def bpr_loss(uids, baskets, dynamic_user, item_embedding):
        """
        Bayesian personalized ranking loss for implicit feedback.

        Args:
            uids: batch of users' ID
            baskets: batch of users' baskets
            dynamic_user: batch of users' dynamic representations
            item_embedding: item_embedding matrix
        """
        loss = 0
        for uid, bks, du in zip(uids, baskets, dynamic_user):
            du_p_product = torch.mm(du, item_embedding.t())  # shape: [pad_len, num_item]
            loss_u = []  # loss for user
            for t, basket_t in enumerate(bks):
                if basket_t[0] != 0 and t != 0:
                    pos_idx = torch.LongTensor(basket_t)

                    # Sample negative products
                    neg = random.sample(list(neg_samples[uid]), len(basket_t))
                    neg_idx = torch.LongTensor(neg)

                    # Score p(u, t, v > v')
                    score = du_p_product[t - 1][pos_idx] - du_p_product[t - 1][neg_idx]

                    # Average Negative log likelihood for basket_t
                    loss_u.append(torch.mean(-torch.nn.LogSigmoid()(score)))
            for i in loss_u:
                loss = loss + i / len(loss_u)
        avg_loss = torch.div(loss, len(baskets))
        return avg_loss

        Bayesian personalized ranking loss for implicit feedback.

        Args:
            uids: batch of users' ID
            baskets: batch of users' baskets
            dynamic_user: batch of users' dynamic representations
            item_embedding: item_embedding matrix
        
Bayesian personalized ranking loss for implicit feedback.
Bayesian personalized ranking loss for implicit feedback .
def bpr_loss(uids, baskets, dynamic_user, item_embedding):
        loss = 0
        for uid, bks, du in zip(uids, baskets, dynamic_user):
            du_p_product = torch.mm(du, item_embedding.t())  
            loss_u = []  
            for t, basket_t in enumerate(bks):
                if basket_t[0] != 0 and t != 0:
                    pos_idx = torch.LongTensor(basket_t)
                    neg = random.sample(list(neg_samples[uid]), len(basket_t))
                    neg_idx = torch.LongTensor(neg)
                    score = du_p_product[t - 1][pos_idx] - du_p_product[t - 1][neg_idx]
                    loss_u.append(torch.mean(-torch.nn.LogSigmoid()(score)))
            for i in loss_u:
                loss = loss + i / len(loss_u)
        avg_loss = torch.div(loss, len(baskets))
        return avg_loss
def bpr_loss ( uids , baskets , dynamic_user , item_embedding ) : loss = 0 for uid , bks , du in zip ( uids , baskets , dynamic_user ) : du_p_product = torch . mm ( du , item_embedding . t ( ) ) loss_u = [ ] for t , basket_t in enumerate ( bks ) : if basket_t [ 0 ] != 0 and t != 0 : pos_idx = torch . LongTensor ( basket_t ) neg = random . sample ( list ( neg_samples [ uid ] ) , len ( basket_t ) ) neg_idx = torch . LongTensor ( neg ) score = du_p_product [ t - 1 ] [ pos_idx ] - du_p_product [ t - 1 ] [ neg_idx ] loss_u . append ( torch . mean ( - torch . nn . LogSigmoid ( ) ( score ) ) ) for i in loss_u : loss = loss + i / len ( loss_u ) avg_loss = torch . div ( loss , len ( baskets ) ) return avg_loss
Bayesian personalized ranking loss for implicit feedback.
Bayesian personalized ranking loss for implicit feedback .
"""
        Bayesian personalized ranking loss for implicit feedback.

        Args:
            uids: batch of users' ID
            baskets: batch of users' baskets
            dynamic_user: batch of users' dynamic representations
            item_embedding: item_embedding matrix
        """ # shape: [pad_len, num_item] # loss for user # Sample negative products # Score p(u, t, v > v') # Average Negative log likelihood for basket_t
uids
batch of users' ID
batch of users ' ID
baskets
batch of users' baskets
batch of users ' baskets
dynamic_user
batch of users' dynamic representations
batch of users ' dynamic representations
item_embedding
None