简体   繁体   中英

Keras: transposing kernel of Conv2D layer for reuse in another Conv2D layer

I am trying to implement a own layer with Keras functional API. The idea is to transpose the convolutional operator. So far I got:

        k1 = self.tied_to.kernel
        self.kernel = K.transpose(k1)

The code works if I just pass the kernel of the former layer without doing anything to it. The kernel is defined in the shape 3x3, so transposing it should work nicely. But the problem is that the kernel is in the form of a tensor with shape 3x3x1x1 and K.transpose transposes the complete tensor. How do I transpose only the kernel itself, so I get again a kernel of the form 3x3x1x1?

You want to use K.permute_dimensions .

I'm not sure about what you want to transpose, but I see two possibilities.

  • Transpose the spatial dimensions (the kernel identifies identical things as the original kernel, but in a different orientation): K.permute_dimensions(k1, (1,0,2,3))
  • Transpose the input and output channels (maybe to use the kernel in sort of a reverse layer - don't know if this exists, but....): K.permute_dimensions(k1, (0,1,3,2))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM