So I have implemented in a framework lightgcn.
Now I made a second model based on lightgcn where I added some things. But the new model is going so much worse, can you identify if I committed some errors ? Especially with the returned embeddings, and losses wrt the framework ?
LightGCN
from abc import ABC
from torch_geometric.nn import LGConv
import torch
import torch_geometric
import numpy as np
import random
import torch_sparse
from torch_geometric.nn import LGConv
import torch
import torch_geometric
import numpy as np
import random
import torch.nn as nn
import torch.nn.functional as F
The problem lies in the computation of xu_neg. You're using gu[pos] instead of gu[user]. This means you're incorrectly pairing item embeddings with wrong user embeddings when computing the negative samples.
In the context of recommendation systems and especially in models like LightGCN, when you compute the negative score, you should use the same user embeddings paired with negative item embeddings. The correct negative sampling should involve the user interacting with a negative item (an item the user hasn't interacted with). Therefore, you should replace gu[pos] with gu[user] in the computation of xu_neg.
By making this correction, xu_neg now correctly represents the scores of the same users with negative items, which is essential for the proper training of the model using the contrastive loss.
Aside from this, another difference to be aware of is how you're aggregating the embeddings after propagation. In your propagate_embeddings method, you're using torch.mean to combine the embeddings from different layers:
In contrast, the original LightGCN model uses a weighted sum based on the alpha parameter:
python
all_embeddings = sum([all_embeddings[k] * self.alpha[k] for k in range(len(all_embeddings))])
While using the mean might not necessarily be wrong, it does change how the embeddings are combined and can affect model performance. You might want to experiment with implementing a similar weighted sum to see if it improves your model's results.
Additionally, ensure that your contrastive loss implementation in cal_ssl_loss is correct and follows best practices for contrastive learning. Make sure that the positive and negative pairs are correctly defined, and consider whether you need to adjust the loss calculation to prevent potential issues like including positive pairs in the denominator when they shouldn't be.
By correcting the computation of xu_neg and reviewing the other differences, your model's performance should improve.