0

I am trying to reshape the output of a keras Conv3D layer with 10 kernels (output_dim = (None, 14, 14, 3, 10)) to a desired outptut of (None, 14, 14, 30, 1) so I can perform another 3D convolution on all the kernels combined. I want to preserve the spatial relation / order of the previous 10 kernels in the reshaped tensor, like pasting them 'behind' each other.

Since keras.layers.reshape uses 'row-major c-style' for reshaping tensors, I would loose order of kernels here. There readily exists a comprehensive explanation on how to use numpy.reshape with numpy.permutate for numpy matrices, and assume keras will work similarly since I can also use keras.layers.permutate. The problem is, I can simply not get my head around what permutation I need in this case before reshaping with keras.layers.reshape to I preserve the order.

Intuition and idea behind reshaping 4D array to 2D array in NumPy

I could always slice and concatenate the tensor, but this would require more keras.layers and slows down my program. A 'fancy' combination of keras.layers.Permutate() --> keras.layers.Reshape() would be very much appreciated!

  • Try to be very specific when asking questions. Try to give a "Reproducible" example https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example By using a common data set and Showing Your Code it is easier for all. S.O. is best used for specific code questions not a place to have others write code. Try it first.... – mccurcio Mar 18 '20 at 17:02

1 Answers1

0

The reshapes should not change the order of anything (because changing the order is computationally expensive, while reshaping is simply telling how to divide)

The best way to test is to simply do it and look at the results, but you'd get these:

If you reshape this (2,5):

[
 [1,2,3,4,5],
 [6,7,8,9,10]
]

Into (10,), you will get the same order as before:

[1,2,3,4,5,6,7,8,9,10]

If for some reason you want [1,6,2,7,3,8,4,9,5,10], then you permute the last two dimensions.

That said, your (None, 14, 14, 3, 10) when reshaped to (None, 14, 14, 30) will keep the order of the last 10 together.

[[1,2,3,4,5,6,7,8,9,10],
 [11,12,13,14,15,16,17,18,19,20],
 [21,22,23,24,25,26,27,28,29,30]]

becomes

 [1,2,3,4,5,6,7,8,9,10,11,12...]

If you want to get the following then permute:

outs = Permute((1,2,4,3))(ins)
outs = Reshape((14,14,30))(outs)
---> [[1,11,21], [2,12,22], [3,13,23]...
Daniel Möller
  • 84,878
  • 18
  • 192
  • 214