# Translate a 1d convolution from Keras to Julia?

**URL:** https://discourse.julialang.org/t/translate-a-1d-convolution-from-keras-to-julia/88903
**Category:** New to Julia
**Tags:** question, package, flux, python, machine-learning
**Created:** [October 18, 2022, 10:30am UTC](https://discourse.julialang.org/t/translate-a-1d-convolution-from-keras-to-julia/88903 "2022-10-18T10:30:04Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![elii17](https://avatars.discourse-cdn.com/v4/letter/e/a88e4f/32.png) [@elii17](https://discourse.julialang.org/u/elii17)
#### Post date: [October 18, 2022, 10:30am UTC](https://discourse.julialang.org/t/translate-a-1d-convolution-from-keras-to-julia/88903/1 "2022-10-18T10:30:04Z")

</div>

Hey ! I am trying to reproduce this python code in julia : ([Supervised graph classification with Deep Graph CNN — StellarGraph 1.2.1 documentation](https://stellargraph.readthedocs.io/en/stable/demos/graph-classification/dgcnn-graph-classification.html#Supervised-graph-classification-with-Deep-Graph-CNN)).

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),  
> GlobalPool(mean),  
> Flux.Conv((97,1),1=\>16,relu,stride=97),  
> Flux.MaxPool((1,97),pad=2),  
> Flux.Conv((1,5),16=\>32,relu,stride=1),  
> Dense(32=\>1,relu)) |\> device

While debugging and implementing

> model[1:6]

the 6th layer

> Flux.Conv((97,1),1=\>16,relu,stride=97))  
> 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 (julialang.org)](https://discourse.julialang.org/t/flux-1d-convolutions-on-genomic-data/74874)) 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

---

<div class="post-metadata">

### Author: ![ToucheSir](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/touchesir/32/14411_2.png) [@ToucheSir](https://discourse.julialang.org/u/ToucheSir)
#### Post date: [October 19, 2022, 3:48pm UTC](https://discourse.julialang.org/t/translate-a-1d-convolution-from-keras-to-julia/88903/2 "2022-10-19T15:48:41Z")

</div>

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.

Also:

> [@elii17](#):
>
> Flux.Conv((97,1),1=\>16,relu,stride=97),

Per [the docs](https://fluxml.ai/Flux.jl/stable/models/layers/), 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 `MaxPool`ing layers.

---

<div class="post-metadata">

### Author: ![elii17](https://avatars.discourse-cdn.com/v4/letter/e/a88e4f/32.png) [@elii17](https://discourse.julialang.org/u/elii17)
#### Post date: [October 20, 2022, 10:25am UTC](https://discourse.julialang.org/t/translate-a-1d-convolution-from-keras-to-julia/88903/3 "2022-10-20T10:25:05Z")

</div>

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](https://stellargraph.readthedocs.io/en/stable/demos/graph-classification/dgcnn-graph-classification.html#Supervised-graph-classification-with-Deep-Graph-CNN))

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),  
> GlobalPool(mean))
> 
> model\_1 = Chain(Flux.Conv((30,),1=\>16,relu,stride=30),  
> Flux.MaxPool((1,),pad=2),  
> Flux.Conv((5,),16=\>32,relu,stride=2),  
> x → reshape(x, :, size(x, 4)),  
> Dense(32=\>1,relu))

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…

---

<div class="post-metadata">

### Author: ![ToucheSir](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/touchesir/32/14411_2.png) [@ToucheSir](https://discourse.julialang.org/u/ToucheSir)
#### Post date: [October 21, 2022, 2:18pm UTC](https://discourse.julialang.org/t/translate-a-1d-convolution-from-keras-to-julia/88903/4 "2022-10-21T14:18:15Z")

</div>

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.
