Translate a 1d convolution from Keras to Julia?

Hey ! I am trying to reproduce this python code in julia : (Supervised graph classification with Deep Graph CNN — StellarGraph 1.2.1 documentation).

While the GNN part is completely fine, I am finding myself to struggle with the implementation of a 1D Convolutional layer, and in general the CNN part.

So far my model is designed as follow:

model = GNNChain(GCNConv(4 => 32,tanh),
GCNConv(32 => 32,tanh),
GCNConv(32 => 32,tanh),
GCNConv(32 => 1,tanh),
Dense(32=>1,relu)) |> device

While debugging and implementing


the 6th layer

breaks and returns to me the error:
DimensionMismatch(“Rank of x and w must match! (2 vs. 4)”)

From what I understand and read online, ( Flux: 1D convolutions (on genomic data) - Specific Domains / Machine Learning - Julia Programming Language ( the dimension of the Conv layer expect 3 or 4d data however the dimension of the previous layer in my code is (1,30), therefore I believe this is where the mismatch comes from. However, I do not know what to do from here…

Here is a MWE data that you can use to pass through the model:

using GraphNeuralNetworks
using Flux
g = rand_graph(22080,175800)
graph = GNNGraph(g,ndata=rand(4,g.num_nodes))
model[1:6] (g,g.ndata.x)

Thanks for your help,
Your boi

1 Like

Where do GCNConv and GNNChain come from? Those are not standard Flux. The model here also seems to be very different from the one in the linked page? Looking through that page, they get a 3D array to pass through to the Conv1D layers whereas the first Conv layer here appears to be getting a 2D array.


Per the docs, you should be passing a tuple of length 1 for the kernel dimensions to run a 1D conv. So the above would be Flux.Conv((97,),1=>16,relu,stride=97). Likewise for the MaxPooling layers.

1 Like

Hey ! These GNCConv are from the GraphNeuralNetworks library in Julia. Where do you get that the array is 3d in their code ? Just to make sure: you are looking at this code right ( Supervised graph classification with Deep Graph CNN — StellarGraph 1.2.1 documentation)

I’m still a beginner with ML library and ML in general so could be that I am mistaken…

I have now changed the code to

model = GNNChain(GCNConv(5 => 32,tanh),
GCNConv(32 => 32,tanh),
GCNConv(32 => 32,tanh),
GCNConv(32 => 1,tanh),

model_1 = Chain(Flux.Conv((30,),1=>16,relu,stride=30),
x → reshape(x, :, size(x, 4)),

These two models are then put together with

ps = Flux.params(model,model_1)

I’m still very confused how in the python code they write

x_out = Conv1D(filters=16, kernel_size=sum(layer_sizes), strides=sum(layer_sizes))(x_out)

as the dimensions I get of the final layer of the GNNChain is of the order of the batchsize, therefore if using a standard batchsize of 32 this does fit as sum(layer_sizes)=97. I think i’m not understanding something here…

It would be illuminating for both yourself and this forum if you could print out the size of the output from the GNN part in both Python and Julia. I suspect they are not the same, which may be where your confusion is coming from.