Skip to content
Advertisement

Accumulate sliding windows relative to origin

I have an array A with the shape (3,3) which can be thought of as the sliding window view of an unkown array with the shape (5,). I want to compute the inverse of windowing the array with the shape (5,). The adjoint operation of this will be summation. What I mean is that I want to accumulate the values in each corresponding window with the related position in the array with the shape (5,). Ofcourse, my expected output of this inverse function and the input A are not related and are just ordinary arrays. I have two examples which I hope explains this better.

JavaScript

I expect this output:

JavaScript

The other example:

JavaScript

I expect this output:

JavaScript

The solution I have which is quite slow (result stored in out)

JavaScript

Writing to a strided view feels a bit hacky and I am sure there is a better solution.

Is there any way to write this in a vectorized manner, without the for-loop? (which also generalizes for multiple dimensions)

EDIT

In terms of generalizing for higher dimensions, I have cases where the windows are taken from an image (2d array), instead of a 1d array like the example above. For the 2d case, A can for example be windows of size 3. This means that from an image (output) with the shape (4,4), The windows A will have the shape (2,2,3,3).

JavaScript

Using the solution given by Pablo, I get the following error

JavaScript

Using a slightly modified version of my stride solution:

JavaScript

Output:

JavaScript

To clarify, the window size and output shape is known beforehand, see inverse_sliding_windows.

Advertisement

Answer

As I mentioned in the comment, a vectorized solution doesn’t always guarantee a better running time. If your matrix is large, you might prefer more efficient methods. And there are several reasons why matrix rotation is slow (though, intuitive), see comment.

Performance comparison:

JavaScript

Code (tested in jupyter notebook)

JavaScript

In multidimensional situation:

JavaScript

Actually I don’t know the how the dimensions (or shapes) are calculated based on your description :(. But i think it could be generalized. The idea is to construct slices as you go. So you need to specify which dimensions correspond to h, w, which correspond to x, y. I think it’s not difficult to do that.

Reference: Numpy index array of unknown dimensions?


Regarding https://stackoverflow.com/a/67341994/14923227

JavaScript

Performance for large array:

JavaScript

enter image description here

It’s trivial to parallelize the for loop in fast. But fast is actually the most cache efficient (even for GPU cache and memory banks) and thus the fastest way to compute it. Ideally, you can parallelize the code with CUDA/OpenCL since there are way more cores in a GPU. If you do it correctly, the running time will be reduced to log(original_fast_time) with base k, where k is the number of cores you have.

However, there are only a few computations in the function. So the transportation of data between memory and GRAM might dominate. (I didn’t test it)

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement