Implementing a ratio layer in PyTorch


I have a very specific operation I’d like to implement in PyTorch, it has no trainable parameters but needs to be differentiable. The operation works as follows:

  • Take a fixed size window eg. 7x7
  • Take the mean of the left half of the window and the mean of the right half of the window (always ignoring the center column)
  • Set the center value to the ratio of the left side to the right side ( output(3,3) = MEANleft / MEANright)
  • Repeat over the entire image (No padding needed)

I have implemented this forward method using loops but it is really slow. Is there a simple way to do this with Tensor Comprehensions? Or a better way to apply such an operation using existing layers?


I have no idea how to make differentiable layer without any parameters… what exactly you differentiate?

This can easily be done faster than loops:
a) Use two 7x7 convolution kernels and a stride of (7,7) with no padding to get left mean and right mean.
Then compute your output as y[:, 0] / y[:, 1]
b) Without conv operations via

x_reshaped = x.view(-1, 7, 7)  # possibly need to crop or pad first
output_prelim = x_reshaped[:, :, :3].mean(-1) / x_reshaped[:, :, 4:].mean(-1)
output = output_prelim.view(expected_output_shape)