Graph Convolutional Networks in PyTorch

Related tags

Deep Learning pygcn
Overview

Graph Convolutional Networks in PyTorch

PyTorch implementation of Graph Convolutional Networks (GCNs) for semi-supervised classification [1].

For a high-level introduction to GCNs, see:

Thomas Kipf, Graph Convolutional Networks (2016)

Graph Convolutional Networks

Note: There are subtle differences between the TensorFlow implementation in https://github.com/tkipf/gcn and this PyTorch re-implementation. This re-implementation serves as a proof of concept and is not intended for reproduction of the results reported in [1].

This implementation makes use of the Cora dataset from [2].

Installation

python setup.py install

Requirements

  • PyTorch 0.4 or 0.5
  • Python 2.7 or 3.6

Usage

python train.py

References

[1] Kipf & Welling, Semi-Supervised Classification with Graph Convolutional Networks, 2016

[2] Sen et al., Collective Classification in Network Data, AI Magazine 2008

Cite

Please cite our paper if you use this code in your own work:

@article{kipf2016semi,
  title={Semi-Supervised Classification with Graph Convolutional Networks},
  author={Kipf, Thomas N and Welling, Max},
  journal={arXiv preprint arXiv:1609.02907},
  year={2016}
}
Comments
  • The cora sites graph is not connected?

    The cora sites graph is not connected?

    @tkipf

    When I displayed the graph I got a graph with many components. Is this correct? pdf attached. Maybe you included extra info in the edge list?

    cora.pdf

    opened by bapriddy 6
  • inconsistent tensor size

    inconsistent tensor size

    Hi, When I run train.py there is a error message says: Loading cora dataset... Traceback (most recent call last): File "train.py", line 104, in train(epoch) File "train.py", line 69, in train output = model(features, adj) File "/usr/local/lib/python2.7/dist-packages/torch/nn/modules/module.py", line 206, in call result = self.forward(*input, **kwargs) File "build/bdist.linux-x86_64/egg/pygcn/models.py", line 15, in forward x = F.relu(self.gc1(x, adj)) File "/usr/local/lib/python2.7/dist-packages/torch/nn/modules/module.py", line 206, in call result = self.forward(*input, **kwargs) File "build/bdist.linux-x86_64/egg/pygcn/layers.py", line 61, in forward return output + self.bias File "/usr/local/lib/python2.7/dist-packages/torch/autograd/variable.py", line 745, in add return self.add(other) File "/usr/local/lib/python2.7/dist-packages/torch/autograd/variable.py", line 283, in add return self._add(other, False) File "/usr/local/lib/python2.7/dist-packages/torch/autograd/variable.py", line 277, in _add return Add(inplace)(self, other) File "/usr/local/lib/python2.7/dist-packages/torch/autograd/_functions/basic_ops.py", line 20, in forward return a.add(b) RuntimeError: inconsistent tensor size at /b/wheel/pytorch-src/torch/lib/TH/generic/THTensorMath.c:831

    opened by wangxiaoyunanne 6
  • Why test sample number is much more than train / validation sample?

    Why test sample number is much more than train / validation sample?

    I saw you assign train/val/test follow this way: idx_train = range(140) idx_val = range(200, 500) idx_test = range(500, 1500)

    I don't understand why you assign test sample number much more than train and val?

    opened by zhiqiangzhongddu 5
  • Node Classification for Multiple Graphs

    Node Classification for Multiple Graphs

    I have multiple graphs for node classification task. All the examples I've seen so far was for graph classification(or there is just one graph for node classification task). Although I've seen building block diagonal adjacency matrix, I'm not sure if it is for graph classification or node. Also I didn't understand whether should I create a block diagonal matrix with feature matrix and labels or not.

    Let's suppose I have 20 different graphs(with different number of nodes, edges, features). And each node of every graph is labeled. All the nodes in first 10 graphs are for training, all the nodes of the next 5 graphs are for val, and all the nodes of last 5 graphs are for test, and what I try to do is predicting labels of the nodes for the graphs in test-set. How can I input multiple graphs into GCN with these conditions for node classification task(not for graph classification).? If the solution is diagonal adj. matrix, should I do the same for labels and feature matrix?

    opened by ghost 4
  • Scale up to million of nodes

    Scale up to million of nodes

    Hi @tkipf, thank you so much for providing the code.

    I'm wondering if it's possible to scale this implementation up to millions of nodes (obviously the number of edges must scale linearly), for example a grid. I'm not familiar with PyTorch's sparse matrix implementation, so I'm not sure if representing the adjacency matrix as a sparse matrix is enough to deal with large graphs?

    opened by jthsieh 4
  • build symmetric adjacency matrix

    build symmetric adjacency matrix

    Hi tkipf, thank you for your amazing work! in utils.py, starts from Line 36,

        # build symmetric adjacency matrix
        adj = adj + adj.T.multiply(adj.T > adj) - adj.multiply(adj.T > adj)
    

    As far as I understand, these lines are turning a directed adjacency matrix into an undirected adjacency matrix? Since adj is a 0-1 matrix, then for the positions adj[i,j] where adj.T > adj, we should have adj[i,j] = 0, so the - adj.multiply(adj.T > adj) part is always zero.

    Then what's the purpose of having that part, or am I understand it incorrectly?

    opened by laoreja 4
  • Still curious about the code of building symmetric adjacency matrix

    Still curious about the code of building symmetric adjacency matrix

    ` adj = sp.coo_matrix((np.ones(edges.shape[0]), (edges[:, 0], edges[:, 1])), shape=(labels.shape[0], labels.shape[0]), dtype=np.float32)

    # build symmetric adjacency matrix
    adj = adj + adj.T.multiply(adj.T > adj) - adj.multiply(adj.T > adj)`
    

    I don't understand how the last line code produces the symmetric matrix. And I think it is intuitive to build the symmetric like this adj = adj + adj.T

    Can anyone help to answer my questions? thanks a lot

    opened by BilyZ98 3
  •  determinism problem

    determinism problem

    Hi Kipf,

    Nice work. I am trying to run the model but failed to get the same result everytime even with the below lines.

    np.random.seed(args.seed) torch.manual_seed(args.seed) if args.cuda: torch.cuda.manual_seed(args.seed)

    opened by xuuuluuu 3
  • non-square adj matrix

    non-square adj matrix

    Hello, thank you for your great work.

    I want to extend gcn which involves message passing, but I'm new to GCN so I have a minor question.

    I have to types of node A, B. Basically I want to train different weights jointly. (Weight_AA, Weight_AB, Weight_BA, Weight_BB) During the node representation update, A(t+1) = Weight_AA*A(t)adj(AA) + Weight_ABB(t)adj(BA) B(t+1) = Weight_BBB(t)adj(BB) + Weight_BAA(t)*adj(AB) The first terms are simple graph convolution layer with adj(AA), adj(BB) are both square but for the adj(BA), adj(AB) it might not be square, (# of two types of nodes will differ)

    Can I use non-square adj matrix during the whole process? (normalize, forward, ...)

    opened by ahyunSeo 3
  • Processing a single graph in batch mode

    Processing a single graph in batch mode

    Hi @tkipf, thanks for your awesome work and providing this code!

    I have a bit of a novice question: I'm trying to process my graph's features through a GCN in mini-batches. I.e. let's say I have a 1000-node graph and I want to process it through the GCN in mini-batches of size 50.

    It doesn't seem like the code currently supports this because we have to multiply by the full adjacency matrix in the GCN layer's forward pass - do you have any sense of how I can support these "batch" operations? Better yet, do you have example code that does this?

    My code looks something like:

    z0 = F.tanh(self.gcn1(x, self.fancy_adj))

    where x is a sampled batch of size 50 x F (not the full batch of 1000 x F) and self.fancy_adj is the adjacency matrix transformed as suggested in your paper ( adjacency + identity + row normalized). The problem, of course, is that self.fancy_adj is a 1000 x 1000 matrix. Even if I take just the rows of self.fancy_adj corresponding to the 50 points in the batch, then the adjacency matrix becomes a 50 x 1000 matrix which can't be multiplied by the 50 x F sampled batch.

    opened by motiwari 3
  • question about the adjacency matrix

    question about the adjacency matrix

    When I read the papers, I saw a lot of formulas on how to calculate the adjacency matrix. We need matrix D and A in the formula as well. But when I read the code in this project, I found that you calculate the adj matrix without D. And I also don't understand how you calculate the adj. Could you explain it in the simple way? Thanks very much.

    opened by Sandydreamsmile 2
  • citeseer dataset

    citeseer dataset

    Hi,tkipf. when I use pygcn deal with citeseer dataset, there is a abnormal issue:dtype=np.int32).reshape(edges_unordered.shape),TypeError: int() argument must be a string, a bytes-like object or a number, not 'NoneType'. How I dealing with it? Looking forward to your reply,Thank you!

    opened by zhoulijun11 0
  • About the dataset split

    About the dataset split

    I see the code in utils.py idx_train = range(140) idx_val = range(200, 500) idx_test = range(500, 1500)

    I cannot understand it, because the ratio of they may be 6:2:2 if the dataset is smaller.

    Anyone can help me?

    opened by DM0815 2
  • Why do row normalization instead of column normalization?

    Why do row normalization instead of column normalization?

    When we normalized features, it seemed that the columns were features and the rows were samples, so why not normalize the columns?

    Could someone give me some suggestions?

    opened by Liying1996 2
  • transform to other scope dataset

    transform to other scope dataset

    other scope dataset is a file (one file) and shape is below column1, column2, column3, ...column9, label 1500, 45, 1, .... 0 933, 22, 0, .... 0 1234, 30, 1, .... 1 1112, 23, 0, .... 0 ...

    Let me know how to transform to above dataset with this code

    opened by kimsijin33 0
  • Error: 'pybind11' must be installed before running the build.

    Error: 'pybind11' must be installed before running the build.

    I try below command

    log-> python setup.py install

    running install error: can't create or remove files in install directory

    The following error occurred while trying to add or remove files in the installation directory:

    [Errno 13] Permission denied: '/usr/local/lib/python3.7/dist-packages/test-easy-install-1558.write-test
    

    ...

    and try below command $ sudo python setup.py install log -> ... Best match: scipy 1.7.0 Processing scipy-1.7.0.tar.gz Writing /tmp/easy_install-v4yo41y1/scipy-1.7.0/setup.cfg Running scipy-1.7.0/setup.py -q bdist_egg --dist-dir /tmp/easy_install-v4yo41y1/scipy-1.7.0/egg-dist-tmp-d7yw9bg_ /tmp/easy_install-v4yo41y1/scipy-1.7.0/setup.py:492: UserWarning: Unrecognized setuptools command ('-q bdist_egg --dist-dir /tmp/easy_install-v4yo41y1/scipy-1.7.0/egg-dist-tmp-d7yw9bg_'), proceeding with generating Cython sources and expanding templates ' '.join(sys.argv[1:]))) Error: 'pybind11' must be installed before running the build.

    please help me!!

    opened by kimsijin33 0
  • Question About fastmode

    Question About fastmode

    Hi, I'm learning GCN and read your code, Not sure whats the use of fast mode? Seems that dropout may make the eval set accuracy inaccurate? I'm a learner so probably just me not knowing enough.

    opened by zhangxingeng 0
Owner
Thomas Kipf
Thomas Kipf
PyTorch implementation of Graph Convolutional Networks in Feature Space for Image Deblurring and Super-resolution, IJCNN 2021.

GCResNet PyTorch implementation of Graph Convolutional Networks in Feature Space for Image Deblurring and Super-resolution, IJCNN 2021. The code will

null 11 May 19, 2022
A PyTorch implementation of "Cluster-GCN: An Efficient Algorithm for Training Deep and Large Graph Convolutional Networks" (KDD 2019).

ClusterGCN ⠀⠀ A PyTorch implementation of "Cluster-GCN: An Efficient Algorithm for Training Deep and Large Graph Convolutional Networks" (KDD 2019). A

Benedek Rozemberczki 697 Dec 27, 2022
Graph Convolutional Networks in PyTorch

Graph Convolutional Networks in PyTorch PyTorch implementation of Graph Convolutional Networks (GCNs) for semi-supervised classification [1]. For a hi

Thomas Kipf 4.5k Dec 31, 2022
Spatial Temporal Graph Convolutional Networks (ST-GCN) for Skeleton-Based Action Recognition in PyTorch

Reminder ST-GCN has transferred to MMSkeleton, and keep on developing as an flexible open source toolbox for skeleton-based human understanding. You a

sijie yan 1.1k Dec 25, 2022
This is the official Pytorch-version code of FlatGCN (Flattened Graph Convolutional Networks for Recommendation).

FlatGCN This is the official Pytorch-version code of FlatGCN (Flattened Graph Convolutional Networks for Recommendation, submitted to ICASSP2022). Req

Dreamer 2 Aug 9, 2022
Unofficial TensorFlow implementation of Protein Interface Prediction using Graph Convolutional Networks.

[TensorFlow] Protein Interface Prediction using Graph Convolutional Networks Unofficial TensorFlow implementation of Protein Interface Prediction usin

YeongHyeon Park 9 Oct 25, 2022
Danfeng Hong, Lianru Gao, Jing Yao, Bing Zhang, Antonio Plaza, Jocelyn Chanussot. Graph Convolutional Networks for Hyperspectral Image Classification, IEEE TGRS, 2021.

Graph Convolutional Networks for Hyperspectral Image Classification Danfeng Hong, Lianru Gao, Jing Yao, Bing Zhang, Antonio Plaza, Jocelyn Chanussot T

Danfeng Hong 154 Dec 13, 2022
MVGCN: a novel multi-view graph convolutional network (MVGCN) framework for link prediction in biomedical bipartite networks.

MVGCN MVGCN: a novel multi-view graph convolutional network (MVGCN) framework for link prediction in biomedical bipartite networks. Developer: Fu Hait

null 13 Dec 1, 2022
Official DGL implementation of "Rethinking High-order Graph Convolutional Networks"

SE Aggregation This is the implementation for Rethinking High-order Graph Convolutional Networks. Here we show the codes for citation networks as an e

Tianqi Zhang (张天启) 32 Jul 19, 2022
Implementing Graph Convolutional Networks and Information Retrieval Mechanisms using pure Python and NumPy

Implementing Graph Convolutional Networks and Information Retrieval Mechanisms using pure Python and NumPy

Noah Getz 3 Jun 22, 2022
Two-Stream Adaptive Graph Convolutional Networks for Skeleton-Based Action Recognition in CVPR19

2s-AGCN Two-Stream Adaptive Graph Convolutional Networks for Skeleton-Based Action Recognition in CVPR19 Note PyTorch version should be 0.3! For PyTor

LShi 547 Dec 26, 2022
CoSMA: Convolutional Semi-Regular Mesh Autoencoder. From Paper "Mesh Convolutional Autoencoder for Semi-Regular Meshes of Different Sizes"

Mesh Convolutional Autoencoder for Semi-Regular Meshes of Different Sizes Implementation of CoSMA: Convolutional Semi-Regular Mesh Autoencoder arXiv p

Fraunhofer SCAI 10 Oct 11, 2022
A PyTorch implementation of "Signed Graph Convolutional Network" (ICDM 2018).

SGCN ⠀ A PyTorch implementation of Signed Graph Convolutional Network (ICDM 2018). Abstract Due to the fact much of today's data can be represented as

Benedek Rozemberczki 251 Nov 30, 2022
A PyTorch implementation of the Relational Graph Convolutional Network (RGCN).

Torch-RGCN Torch-RGCN is a PyTorch implementation of the RGCN, originally proposed by Schlichtkrull et al. in Modeling Relational Data with Graph Conv

Thiviyan Singam 66 Nov 30, 2022
A static analysis library for computing graph representations of Python programs suitable for use with graph neural networks.

python_graphs This package is for computing graph representations of Python programs for machine learning applications. It includes the following modu

Google Research 258 Dec 29, 2022
The source code of the paper "Understanding Graph Neural Networks from Graph Signal Denoising Perspectives"

GSDN-F and GSDN-EF This repository provides a reference implementation of GSDN-F and GSDN-EF as described in the paper "Understanding Graph Neural Net

Guoji Fu 18 Nov 14, 2022
Some tentative models that incorporate label propagation to graph neural networks for graph representation learning in nodes, links or graphs.

Some tentative models that incorporate label propagation to graph neural networks for graph representation learning in nodes, links or graphs.

zshicode 1 Nov 18, 2021
On Size-Oriented Long-Tailed Graph Classification of Graph Neural Networks

On Size-Oriented Long-Tailed Graph Classification of Graph Neural Networks We provide the code (in PyTorch) and datasets for our paper "On Size-Orient

Zemin Liu 4 Jun 18, 2022