Answers.Body: <p>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 <a href="https://www.redblobgames.com/grids/hexagons/#coordinates-doubled" rel="nofollow noreferrer">doubled coordinates</a>, which I found much easier to save in a 2D array. </p>
  
  <p>An example kernel that only changes the direct neighbours and the cell itself would be this</p>
  
  <pre><code>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  ]])
  </code></pre>
  
  <p>I don't know much about Keras, but I assume that this is possible there as well.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-04-16T07:44:43.477Z
Answers.LastActivityDate: 2019-04-16T07:44:43.477Z
Answers.UserDisplayName: Marvin van Aalst
Body: <p>Keras' convolutional and deconvolutional layers are designed for square grids. Is there was a way to adapt them for use in hexagonal grids?</p>
  
  <p>For example, if we were using <a href="https://www.redblobgames.com/grids/hexagons/#coordinates-axial" rel="nofollow noreferrer">axial coordinates</a>, the input of the kernel of radius 1 centered at <code>(x,y)</code> should be:</p>
  
  <p><code>[(x-1,y), (x-1,y+1), (x,y-1), (x,y+1), (x+1,y-1), (x+1, y)]</code></p>
  
  <p>One option is to fudge it with a 3 by 3 box, but then you are using cells at different distances.</p>
  
  <p>Some ideas:</p>
  
  <ul>
  <li>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.</li>
  <li>Use a 3 by 3 box, but set the weights at <code>(x-1,y-1)</code> and <code>(x+1,y+1)</code> to zero. Unfortunately, I do not know how to permanently set weights to a given value in Kera.</li>
  <li>Use <a href="https://www.redblobgames.com/grids/hexagons/#coordinates-cube" rel="nofollow noreferrer">cube coordinates</a> 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).</li>
  </ul>
  
  <p>Are there any better solutions?</p>
  
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2018-09-15T20:27:22.007Z
Comments.Text: [Related](https://stackoverflow.com/q/51265578/1172541)
Comments.UserDisplayName: Christopher King
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2019-12-16T19:33:54.227Z
Comments.Text: for pytorch there is [hexagdly](https://github.com/ai4iacts/hexagdly) with nice explanations
Comments.UserDisplayName: maxy
Tags: <neural-networks><machine-learning><convolutional-neural-networks><python><keras>
Title: Convolutional Layers on a hexagonal grid in Keras
UserDisplayName: Christopher King
ThreadText: # Convolutional Layers on a hexagonal grid in Keras
  
  (Asked by: username_0 on 2018-09-15)
  
  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](https://www.redblobgames.com/grids/hexagons/#coordinates-axial), 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](https://www.redblobgames.com/grids/hexagons/#coordinates-cube) 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?
  
  **Tags:** neural-networks, machine-learning, convolutional-neural-networks, python, keras
  > **Comments:**
  > **username_0**: [Related](https://stackoverflow.com/q/51265578/1172541)  
  > **username_1**: for pytorch there is [hexagdly](https://github.com/ai4iacts/hexagdly) with nice explanations  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_2 on 2019-04-16. Score: 2
  
  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.
  
  
  ---
  
  