Neural matrix factorization (from Murphy's book) combines a generalised matrix factorisation pathway with an MLP pathway, each with its own embeddings. Implement it in PyTorch.
class NeuMF(nn.Module) with __init__(self, n_users, n_items, k_gmf, k_mlp, layers). Use exactly these attribute names (the tests load weights into them):
self.P, self.Q: nn.Embedding(n_users, k_gmf) and nn.Embedding(n_items, k_gmf) for the GMF pathway, .self.U, self.V: nn.Embedding(n_users, k_mlp) and nn.Embedding(n_items, k_mlp) for the MLP pathway.self.mlp: an nn.Sequential of nn.Linear layers each followed by nn.ReLU(), with sizes 2 * k_mlp -> layers[0] -> layers[1] -> ..., applied to the concatenation to give .self.out: nn.Linear(k_gmf + layers[-1], 1) applied to .Methods: score(u, i) returns the pre-sigmoid logits as a 1-D tensor of length B for 1-D long tensors u, i; forward(u, i) returns their sigmoid.
bce_with_negatives(model, pos_u, pos_i, neg_u, neg_i): the mean binary cross-entropy over the positive pairs (label 1) and negative pairs (label 0) together, computed from the logits.
bpr_loss(model, u, i, j): the mean of .