pyg-team / pytorch_geometric

Graph Neural Network Library for PyTorch
https://pyg.org
MIT License
20.58k stars 3.57k forks source link

Question about Graph SAGE implementation #2060

Open jianchaoji opened 3 years ago

jianchaoji commented 3 years ago

❓ Questions & Help

Thank you for sharing! One quick question, for the graph sage implementation, you posted three examples. I'm a little bit confused, they are supervised learning or unsupervised learning ?

jianchaoji commented 3 years ago

As my understanding, all of these examples are supervised learning right. But we can use "examples/reddit.py" and "examples/autoencoder.py" to implement the unsupervised, is that right?

rusty1s commented 3 years ago

examples/reddit.py is a fully supervised example, while examples/graph_sage_unsup.py implements the unsupervised GraphSAGE procedure. Learned embeddings are then evaluated on a down-stream task using logistic regression.

jianchaoji commented 3 years ago

Got it! Thank you for your reply!

jianchaoji commented 3 years ago

I have some issues when I try to run examples/graph_sage_unsup.py:

Traceback (most recent call last):
  File "graph_sage_unsup.py", line 115, in <module>
    loss = train()
  File "graph_sage_unsup.py", line 81, in train
    for batch_size, n_id, adjs in train_loader:
  File "/home/jj635/anaconda3/lib/python3.7/site-packages/torch/utils/data/dataloader.py", line 435, in __next__
    data = self._next_data()
  File "/home/jj635/anaconda3/lib/python3.7/site-packages/torch/utils/data/dataloader.py", line 475, in _next_data
    data = self._dataset_fetcher.fetch(index)  # may raise StopIteration
  File "/home/jj635/anaconda3/lib/python3.7/site-packages/torch/utils/data/_utils/fetch.py", line 47, in fetch
    return self.collate_fn(data)
  File "graph_sage_unsup.py", line 25, in sample
    row, col, _ = self.adj.coo()
AttributeError: 'NeighborSampler' object has no attribute 'adj'
jianchaoji commented 3 years ago

I could run the example/gcn.py correctly. Did I miss something?

jianchaoji commented 3 years ago

Environment

rusty1s commented 3 years ago

Should be fixable by installing PyG from master:

pip install git+https://github.com/rusty1s/pytorch_geometric.git
jianchaoji commented 3 years ago

Thank you for your reply.I tried that, but it is still not working. And it is the same issue as the last one. try to run examples/graph_sage_unsup.py:

Traceback (most recent call last):
  File "graph_sage_unsup.py", line 115, in <module>
    loss = train()
  File "graph_sage_unsup.py", line 81, in train
    for batch_size, n_id, adjs in train_loader:
  File "/home/jj635/anaconda3/lib/python3.7/site-packages/torch/utils/data/dataloader.py", line 435, in __next__
    data = self._next_data()
  File "/home/jj635/anaconda3/lib/python3.7/site-packages/torch/utils/data/dataloader.py", line 475, in _next_data
    data = self._dataset_fetcher.fetch(index)  # may raise StopIteration
  File "/home/jj635/anaconda3/lib/python3.7/site-packages/torch/utils/data/_utils/fetch.py", line 47, in fetch
    return self.collate_fn(data)
  File "graph_sage_unsup.py", line 25, in sample
    row, col, _ = self.adj.coo()
AttributeError: 'NeighborSampler' object has no attribute 'adj'
jianchaoji commented 3 years ago

And I reviewed the class 'NeighborSampler' in examples/graph_sage_unsup.py:

class NeighborSampler(RawNeighborSampler):
    def sample(self, batch):
        batch = torch.tensor(batch)
        row, col, _ = self.adj.coo()
        # For each node in `batch`, we sample a direct neighbor (as positive
        # example) and a random node (as negative example):
        pos_batch = random_walk(row, col, batch, walk_length=1,
                                coalesced=False)[:, 1]
        neg_batch = torch.randint(0, self.adj.size(0), (batch.numel(), ),
                                  dtype=torch.long)

        batch = torch.cat([batch, pos_batch, neg_batch], dim=0)
        return super(NeighborSampler, self).sample(batch)`

I also checked class RawNeighborSampler. We didn't have an attribute 'adj' in RawNeighborSampler, but we had an attribute 'adj_t' in RawNeighborSampler. I exchanged 'adj' with 'adj_t' in NeighborSampler . It could be complied, but I'm not sure it was correctly or not.Here is what I did:

class NeighborSampler(RawNeighborSampler):
    def sample(self, batch):
        batch = torch.tensor(batch)
        row, col, _ = self.adj_t.coo()# change adj to adj_t
        # For each node in `batch`, we sample a direct neighbor (as positive
        # example) and a random node (as negative example):
        pos_batch = random_walk(row, col, batch, walk_length=1,
                                coalesced=False)[:, 1]
        neg_batch = torch.randint(0, self.adj_t.size(0), (batch.numel(), ),
                                  dtype=torch.long)# change adj to adj_t

        batch = torch.cat([batch, pos_batch, neg_batch], dim=0)
        return super(NeighborSampler, self).sample(batch)`

Looking forward your reply!

rusty1s commented 3 years ago

Indeed, you are right. Can you send a PR with the applied change? Would really appreciate it!

jianchaoji commented 3 years ago

Sure,no problem!