Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

mini-batch and negative sampling code #2

Open
zfjsail opened this issue Oct 11, 2019 · 4 comments
Open

mini-batch and negative sampling code #2

zfjsail opened this issue Oct 11, 2019 · 4 comments

Comments

@zfjsail
Copy link

zfjsail commented Oct 11, 2019

Hi Fan-Yun,

Thanks for sharing your code. Do you plan to release the code with mini-batch training and negative sampling for large graphs? Thank you.

@sunfanyunn
Copy link
Owner

sunfanyunn commented Oct 11, 2019

For mini-batch training, refer to the following snippet

train_edges = [(u,v) for u,v in G.edges()]
n_nodes = G.number_of_nodes()

for epoch in range(epochs):
    np.random.shuffle(train_edges)
    batch_edges = train_edges[:batch_size]
    batch = torch.LongTensor(batch_edges)

@sunfanyunn
Copy link
Owner

For negative sampling, you can refer to this repository: https://github.com/DMPierre/LINE

@zfjsail
Copy link
Author

zfjsail commented Oct 11, 2019

Thanks for your reply. I modify the nonoverlapping.py file and run on the cora dataset. The loss is decreasing but nmi and modularity are not increasing. I implement forward function as follows.

 def forward(self, w, c, neg, temp):

        w = self.node_embeddings(w).to(self.device)
        c_ = self.node_embeddings(c).to(self.device)

        c_context = self.contextnode_embeddings(c).to(self.device)
        c_context_community = self.community_embeddings(c_context)
        neg_context = self.contextnode_embeddings(neg).to(self.device)
        neg_context_community = - self.community_embeddings(neg_context)  # neg

        q = self.community_embeddings(w * c_)

        prior = self.community_embeddings(w)
        prior = F.softmax(prior, dim=-1)

        mulpositivebatch = torch.mul(q, c_context_community)
        positivebatch = F.logsigmoid(torch.sum(mulpositivebatch, dim=1))

        mulnegativebatch = torch.mul(q.view(len(q), 1, self.embedding_dim), neg_context_community)
        negativebatch = torch.sum(
            F.logsigmoid(
                torch.sum(mulnegativebatch, dim=2)
            ),
            dim=1)

        loss = positivebatch + negativebatch

        return -torch.mean(loss), F.softmax(q, dim=-1), prior

Is there any mistakes in this function? Thank you!

@zfjsail zfjsail closed this as completed Oct 11, 2019
@zfjsail zfjsail reopened this Oct 11, 2019
@sunfanyunn
Copy link
Owner

sunfanyunn commented Oct 19, 2019

Note that it will take a longer time to observe increase in nmi and modularity if you use negative sampling.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants