spektral icon indicating copy to clipboard operation
spektral copied to clipboard

Issue with GCN followed by Flatten & Dense layers

Open Fabien-Couthouis opened this issue 3 years ago • 3 comments

Hello,

I encounter an issue when I try to flatten the output node features of a GCN. The part of my architecture where I got the problem looks like something like that:

A = Input( (None, None), name="adjacency_matrix", sparse=True)
F = Input( (n_features, ), name="adjacency_matrix") 

 conv1= GCNConv(256, activation="relu")([F,A])
 conv2= GCNConv(256, activation="relu")([conv1,A])
flat = Flatten()(conv2)
dense = Dense(128)(flat)

Then I got the error ValueError: The last dimension of the inputs to Dense should be defined. Found None. Note that this code works fine with GATConv. It may be an issue with SparseTensors, as I can see there is a convert from sparse to dense in GATConv (l.169) that is not present in GCN. I would appreciate any clue about this error.

And thanks for your amazing work!

Fabien-Couthouis avatar Jun 14 '21 09:06 Fabien-Couthouis

Hi

A few things going on here. First, your input shapes are likely wrong since A is in batch mode and F is in single/disjoint mode. Second, the Flatten layer is not an ideal choice for flattening the node features because its output size will depend on the number of nodes in the graph (which should always be unspecified -- or None -- and it's the reason why you are getting the error).

My suggestion is:

  1. Fix the input shapes or, even better, switch to model subclassing and remove the input layers altogether
  2. Replace the Flatten layer with a global pooling layer from spektral.layers.pooling.global_pool

Cheers

danielegrattarola avatar Jun 14 '21 11:06 danielegrattarola

Hi, thanks for the answer!

Well my use case is very specific: I am trying some stuff on reinforcement learning with graphs. In fact I need this Flatten layer to make my agent converging, as I want my number of outputs to depend on the number of inputs. My input shape was working with GAT: I checked and the shape of my inputs for nodes features was (n_nodes, n_features) instead of what is in my first message, sorry for that.

My question was more "Why is it working with GAT but not with GCN?". By working, I mean I have a RL agent that is able to converge on my custom Reinforcement Learning environment, but now I want to try with a non-binary adjacency matrix but I cannot due to this error.

Do you have an idea?

Fabien-Couthouis avatar Jun 14 '21 15:06 Fabien-Couthouis

I am not entirely sure why it works with GAT and not GCN, can you give me a minimum working example to reproduce the crash? Anyway, one thing you can try is to either specify the shape of the adjacency matrix as (n_nodes, n_nodes), or to try the model subclassing approach to create your model

Cheers

danielegrattarola avatar Jun 22 '21 08:06 danielegrattarola