0

Given array:

A = array([[[1, 2, 3, 1],
            [4, 5, 6, 2],
            [7, 8, 9, 3]]])

I obtain the following array in the forward pass with a downsampling factor of k-1:

k = 3
B = A[...,::k]

#output  
array([[[1, 1],
        [4, 2],
        [7, 3]]])

In the backward pass I want to be able to come back to my original shape, with an output of:

array([[[1, 0, 0, 1],
        [4, 0, 0, 2],
        [7, 0, 0, 3]]])
mozway
  • 194,879
  • 13
  • 39
  • 75

1 Answers1

1

You can use numpy.zeros to initialize the output and indexing:

shape = list(B.shape)
shape[-1] = k*(shape[-1]-1)+1
# [1, 3, 4]

A2 = np.zeros(shape, dtype=B.dtype)
A2[..., ::k] = B

print(A2)

output:

array([[[1, 0, 0, 1],
        [4, 0, 0, 2],
        [7, 0, 0, 3]]])

using A:

A2 = np.zeros_like(A)
A2[..., ::k] = B
# or directly
# A2[..., ::k] = A[..., ::k]
mozway
  • 194,879
  • 13
  • 39
  • 75
  • Thanks @mozway, this is working on k=3, whenever k is changed it won't work yet both B and A2 depends on that k. for example if k =2 output B = (array([[[1, 3], [4, 6], [7, 9]]]) and I want A2 to be (array([[[1, 0,3,0], [4,0, 6,0], [7,0, 9,0]]]). So in short, I want something whatever the number of k, it will work. Thanks – Average Leaner Oct 04 '22 at 21:34
  • @AverageLeaner there was a mistake, check again – mozway Oct 04 '22 at 21:37
  • However it's impossible to make trailing columns of 0 just by knowing B and k. You can force the output shape (like even, multiple of k, or whatever, but you can never predict back the exact original shape with trailing data) – mozway Oct 04 '22 at 21:41
  • I see what you mean, but you still have access to original array A, right? and the idea is to get to the original shape of A – Average Leaner Oct 04 '22 at 21:43
  • If you still have `A`, then no problem. I'll update – mozway Oct 04 '22 at 21:43
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/248551/discussion-between-average-leaner-and-mozway). – Average Leaner Oct 04 '22 at 21:53