gcn icon indicating copy to clipboard operation
gcn copied to clipboard

How to deal with arbitrary share of adjacency matrix for each sample?

Open Abhinav43 opened this issue 6 years ago • 6 comments

So I have one dataset which contains two molecules formulas and their prediction value, dataset looks like this:

alt text

Where the graph structure between the atoms for a single molecule looks like this:

[{'aid1': 1, 'aid2': 9, 'order': 1, 'style': 3},
 {'aid1': 1, 'aid2': 10, 'order': 1},
 {'aid1': 2, 'aid2': 13, 'order': 2},
 {'aid1': 3, 'aid2': 16, 'order': 1},
 {'aid1': 4, 'aid2': 16, 'order': 2},
 {'aid1': 5, 'aid2': 17, 'order': 2},
 {'aid1': 6, 'aid2': 9, 'order': 1},
 {'aid1': 6, 'aid2': 11, 'order': 1},
 {'aid1': 6, 'aid2': 13, 'order': 1},
 {'aid1': 7, 'aid2': 12, 'order': 1, 'style': 3},
 {'aid1': 7, 'aid2': 17, 'order': 1},
 {'aid1': 7, 'aid2': 34, 'order': 1},
 {'aid1': 8, 'aid2': 18, 'order': 1, 'style': 3},
 {'aid1': 8, 'aid2': 36, 'order': 1},
 {'aid1': 8, 'aid2': 37, 'order': 1},
 {'aid1': 9, 'aid2': 12, 'order': 1},
 {'aid1': 9, 'aid2': 25, 'order': 1},
 {'aid1': 10, 'aid2': 11, 'order': 1},
 {'aid1': 10, 'aid2': 14, 'order': 1},
 {'aid1': 10, 'aid2': 15, 'order': 1},
 {'aid1': 11, 'aid2': 16, 'order': 1, 'style': 3},
 {'aid1': 11, 'aid2': 26, 'order': 1},
 {'aid1': 12, 'aid2': 13, 'order': 1},
 {'aid1': 12, 'aid2': 27, 'order': 1},
 {'aid1': 14, 'aid2': 28, 'order': 1},
 {'aid1': 14, 'aid2': 29, 'order': 1},
 {'aid1': 14, 'aid2': 30, 'order': 1},
 {'aid1': 15, 'aid2': 31, 'order': 1},
 {'aid1': 15, 'aid2': 32, 'order': 1},
 {'aid1': 15, 'aid2': 33, 'order': 1},
 {'aid1': 17, 'aid2': 18, 'order': 1},
 {'aid1': 18, 'aid2': 19, 'order': 1},
 {'aid1': 18, 'aid2': 35, 'order': 1},
 {'aid1': 19, 'aid2': 20, 'order': 2, 'style': 8},
 {'aid1': 19, 'aid2': 21, 'order': 1, 'style': 8},
 {'aid1': 20, 'aid2': 22, 'order': 1, 'style': 8},
 {'aid1': 20, 'aid2': 38, 'order': 1},
 {'aid1': 21, 'aid2': 23, 'order': 2, 'style': 8},
 {'aid1': 21, 'aid2': 39, 'order': 1},
 {'aid1': 22, 'aid2': 24, 'order': 2, 'style': 8},
 {'aid1': 22, 'aid2': 40, 'order': 1},
 {'aid1': 23, 'aid2': 24, 'order': 1, 'style': 8},
 {'aid1': 23, 'aid2': 41, 'order': 1},
 {'aid1': 24, 'aid2': 42, 'order': 1}]

I am thinking to use GCN for this task and my plan is encode the formula name with medical embeddings and build the adjacency matrix to get feature vector for each molecule

gcn_model = f ( X, A )

where X will be embedded molecule name and A will be adjacency name

I want to send each sample to network like this but the problem is each sample contains different size of atoms and bonds so adjacency matrix will be different, second thing if I am tryin to solve this then training will be in batches and if I use your recommendation as you said in #4 but how to deal with arbitrary shape of adjacency matrix for each sample ?

Thank you in advance ! :)

Abhinav43 avatar Jul 22 '19 08:07 Abhinav43

The block-diagonal representation suggested in https://github.com/tkipf/gcn/issues/4 works with arbitrarily sized graphs (each instance can be of different size). There's a PyTorch framework that implemented this strategy for GCNs (they call it auto-batching): https://www.dgl.ai

On Mon, Jul 22, 2019 at 10:51 AM Tim as short [email protected] wrote:

So I have one dataset which contains two molecules formulas and their prediction value, dataset looks like this:

[image: alt text] https://camo.githubusercontent.com/1400474baf6f66cdfc2a58444dadbdf86bb5b0dc/68747470733a2f2f692e6962622e636f2f336b577368484e2f53637265656e2d53686f742d323031392d30372d32322d61742d322d31322d34372d504d2e706e67

Where the graph structure between the atoms for a single molecule looks like this:

[{'aid1': 1, 'aid2': 9, 'order': 1, 'style': 3}, {'aid1': 1, 'aid2': 10, 'order': 1}, {'aid1': 2, 'aid2': 13, 'order': 2}, {'aid1': 3, 'aid2': 16, 'order': 1}, {'aid1': 4, 'aid2': 16, 'order': 2}, {'aid1': 5, 'aid2': 17, 'order': 2}, {'aid1': 6, 'aid2': 9, 'order': 1}, {'aid1': 6, 'aid2': 11, 'order': 1}, {'aid1': 6, 'aid2': 13, 'order': 1}, {'aid1': 7, 'aid2': 12, 'order': 1, 'style': 3}, {'aid1': 7, 'aid2': 17, 'order': 1}, {'aid1': 7, 'aid2': 34, 'order': 1}, {'aid1': 8, 'aid2': 18, 'order': 1, 'style': 3}, {'aid1': 8, 'aid2': 36, 'order': 1}, {'aid1': 8, 'aid2': 37, 'order': 1}, {'aid1': 9, 'aid2': 12, 'order': 1}, {'aid1': 9, 'aid2': 25, 'order': 1}, {'aid1': 10, 'aid2': 11, 'order': 1}, {'aid1': 10, 'aid2': 14, 'order': 1}, {'aid1': 10, 'aid2': 15, 'order': 1}, {'aid1': 11, 'aid2': 16, 'order': 1, 'style': 3}, {'aid1': 11, 'aid2': 26, 'order': 1}, {'aid1': 12, 'aid2': 13, 'order': 1}, {'aid1': 12, 'aid2': 27, 'order': 1}, {'aid1': 14, 'aid2': 28, 'order': 1}, {'aid1': 14, 'aid2': 29, 'order': 1}, {'aid1': 14, 'aid2': 30, 'order': 1}, {'aid1': 15, 'aid2': 31, 'order': 1}, {'aid1': 15, 'aid2': 32, 'order': 1}, {'aid1': 15, 'aid2': 33, 'order': 1}, {'aid1': 17, 'aid2': 18, 'order': 1}, {'aid1': 18, 'aid2': 19, 'order': 1}, {'aid1': 18, 'aid2': 35, 'order': 1}, {'aid1': 19, 'aid2': 20, 'order': 2, 'style': 8}, {'aid1': 19, 'aid2': 21, 'order': 1, 'style': 8}, {'aid1': 20, 'aid2': 22, 'order': 1, 'style': 8}, {'aid1': 20, 'aid2': 38, 'order': 1}, {'aid1': 21, 'aid2': 23, 'order': 2, 'style': 8}, {'aid1': 21, 'aid2': 39, 'order': 1}, {'aid1': 22, 'aid2': 24, 'order': 2, 'style': 8}, {'aid1': 22, 'aid2': 40, 'order': 1}, {'aid1': 23, 'aid2': 24, 'order': 1, 'style': 8}, {'aid1': 23, 'aid2': 41, 'order': 1}, {'aid1': 24, 'aid2': 42, 'order': 1}]

I am thinking to use GCN for this task and my plan is encode the formula name with medical embeddings and build the adjacency matrix to get feature vector for each molecule

gcn_model = f ( X, A )

where X will be embedded molecule name and A will be adjacency name

I want to send each sample like this but the problem is each sample contains different size of atoms and bonds so ajcency matrix will be different, second thing if I am tryin to solve this then training will be in batches and if I use your recommmendation as you said in #4 https://github.com/tkipf/gcn/issues/4 but how to deal with arbitrary shape of adjacency matrix for each shapes ?

Thank you in advance ! :)

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/tkipf/gcn/issues/126?email_source=notifications&email_token=ABYBYYB33NRHHM5UICO2BXTQAVYHTA5CNFSM4IFWJNYKYY3PNVWWK3TUL52HS4DFUVEXG43VMWVGG33NNVSW45C7NFSM4HASOVRQ, or mute the thread https://github.com/notifications/unsubscribe-auth/ABYBYYGT34OKGRF4FZXAFBDQAVYHTANCNFSM4IFWJNYA .

tkipf avatar Jul 22 '19 09:07 tkipf

Actually my plan is to get the feature vectors from GCN and then combine with another subnet and concatenate both, but DGL is written in pytorch and my subnet is written in tensorflow.

data => gcn ( A,X ) ==> feature vector_a
data => another_net (sentences) ==> feature vectors_b

concatenate both ==> [ feature vector_a , feature_vector_b ]

So how I can do batch training with arbitrary adjacency matrix in tensorflow? Is there any code snippet in tensorflow?

Thank you very much for quick reply :)

Abhinav43 avatar Jul 24 '19 06:07 Abhinav43

The closest to this (in TensorFlow) is the Graph Nets library: https://github.com/deepmind/graph_nets -- but even this library doesn't provide an out-of-the-box solution for your case and will require some tinkering. In case you only need the frozen feature vectors from a GCN and don't need to train end to end, then you can run the GCN in DGL and port the features to your TensorFlow model. Or you re-implement your model in PyTorch. :)

On Wed, Jul 24, 2019 at 8:20 AM Tim as short [email protected] wrote:

Actually my plan is to get the feature vectors from GCN and then combine with another subnet and concatenate both, but DGL is written in pytorch and my subnet is written in tensorflow.

data => gcn ( A,X ) ==> feature vector_a data => another_net (sentences) ==> feature vectors_b

concatenate both ==> [ feature vector_a , feature_vector_b ]

So how I can do batch training with arbitrary adjacency matrix in tensorflow? Is there any code snippet in tensorflow?

Thank you very much for quick reply :)

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/tkipf/gcn/issues/126?email_source=notifications&email_token=ABYBYYGYA5ZJNJUFI4D56JDQA7YCJA5CNFSM4IFWJNYKYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOD2VJQEA#issuecomment-514496528, or mute the thread https://github.com/notifications/unsubscribe-auth/ABYBYYA7L2MU37TI35CUK23QA7YCJANCNFSM4IFWJNYA .

tkipf avatar Jul 24 '19 06:07 tkipf

Hey Thomas,

sorry that reopen the topic. But it seems there is not alot of people dealing with the issue of batched graph classification.

I understand that you can block_diag your adjacency and node-feature matrix. However when I pass it through the GCN layer or actually any subsequent layer, how odes it work with the weight matrix. How do I multiply the AX (a block matrix of my adjacency and features) with the W the weight matrix?

JanoschMenke avatar Nov 28 '19 13:11 JanoschMenke

No changes required w.r.t. multiplication with the weight matrix: the weight matrix acts independently on each node feature vector.

On Thu, Nov 28, 2019 at 2:05 PM JanoschMenke [email protected] wrote:

Hey Thomas,

sorry that reopen the topic. But it seems there is not alot of people dealing with the issue of batched graph classification.

I understand that you can block_diag your adjacency and node-feature matrix. However when I pass it through the GCN layer or actually any subsequent layer, how odes it work with the weight matrix. How do I multiply the AX (a block matrix of my adjacency and features) with the W the weight matrix?

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/tkipf/gcn/issues/126?email_source=notifications&email_token=ABYBYYHGFEBSG7PJ7UVU62DQV662LA5CNFSM4IFWJNYKYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOEFMRSQI#issuecomment-559487297, or unsubscribe https://github.com/notifications/unsubscribe-auth/ABYBYYBZSLFE5XOMOI5NBO3QV662LANCNFSM4IFWJNYA .

tkipf avatar Nov 29 '19 21:11 tkipf

Thanks for the response. I got it right after I asked the question. I got confused bc I thought I would have to diagonalize the feature matrix as well. But you dont

JanoschMenke avatar Dec 02 '19 12:12 JanoschMenke