Keras' convolutional and deconvolutional layers are designed for square grids. Is there was a way to adapt them for use in hexagonal grids?

For example, if we were using axial coordinates, the input of the kernel of radius 1 centered at (x,y) should be:

[(x-1,y), (x-1,y+1), (x,y-1), (x,y+1), (x+1,y-1), (x+1, y)]

One option is to fudge it with a 3 by 3 box, but then you are using cells at different distances.

Some ideas:

  • Modify Kera's convolutional layer code to use those inputs instead of the default inputs. The problem is that Kera calls its backend instead of implementing it itself, which means we need to modify the backend too.
  • Use a 3 by 3 box, but set the weights at (x-1,y-1) and (x+1,y+1) to zero. Unfortunately, I do not know how to permanently set weights to a given value in Kera.
  • Use cube coordinates instead of Axial coordinates. In this case, a 3 by 3 by 3 box will only contain the central hex's neighbors and inputs set to 0. The problem is that it makes the input array much bigger. Even more problematic, some coordinates that correspond to non-hexes (such as (1,0,0)) will be assigned non-zero outputs (since (0,0,0) falls within its 3 by 3 by 3 box).

Are there any better solutions?

  • $\begingroup$ Related $\endgroup$ Sep 15, 2018 at 20:27
  • $\begingroup$ for pytorch there is hexagdly with nice explanations $\endgroup$
    – maxy
    Dec 16, 2019 at 19:33

1 Answer 1


I had a similar problem with a 2D convolution on a hexagonal grid while working on a diffusion problem and stumbled upon this question. Rather than using cube coordinates, you could use doubled coordinates, which I found much easier to save in a 2D array.

An example kernel that only changes the direct neighbours and the cell itself would be this

kernel = np.array([[0,   0.1, 0,   0.1, 0  ],
                   [0.1, 0,   0.4, 0,   0.1],
                   [0,   0.1, 0,   0.1, 0  ]])

I don't know much about Keras, but I assume that this is possible there as well.


Not the answer you're looking for? Browse other questions tagged .