Skip to main content
We’ve updated our Terms of Service. A new AI Addendum clarifies how Stack Overflow utilizes AI interactions.

Timeline for Strided convolution of 2D in numpy

Current License: CC BY-SA 3.0

18 events
when toggle format what by license comment
May 9, 2020 at 16:57 comment added Nihar Karve This code is wrong. See Jason's comment above if you want this to work in the general case
Mar 2, 2018 at 11:54 comment added Jason @Divakar But your code doesn't work on shapes other than arr=7x7 (this happen to give 1+(m1-m2)//s = 3 = kernel size), sometimes arr4D would even have nans in it. Changing to (1+(m1-m2)//s, 1+(n1-n2)//s, m2, n2) gives the correct match of tensordot axes ((2,3),(0,1)). I think @NaN also meant to ask about this because his np.sum() approach also works now.
Feb 26, 2018 at 14:30 comment added Divakar @Jason The windows would be obtained in a different format. That's what I meant when I mentioned - and these two depend on how we would like to get the sliding windows. With those new changes, to get the windows as listed in the given approach, we would need to swap axes - .swapaxes(1,2) or specify corresponding different axes in the tensordot later on.
Feb 26, 2018 at 14:10 comment added Jason @Divakar, so if I understand you correctly, (1+(m1-m2)//s, m2, 1+(n1-n2)//s, n2) should be paired with strides=(s*s0,s0,s*s1,s1), and (1+(m1-m2)//s, 1+(n1-n2)//s, m2, n2) be paired with strides=(s*s0,s*s1,s0,s1)? I tested on a matrix and the 1st top-left windows doesn't match according to your code.
Feb 26, 2018 at 13:58 comment added Divakar @Jason From what I remember, that output shape depends on how we are striding, i.e. the input to strides argument there and these two depend on how we would like to get the sliding windows. So, that's the only rule in it.
Feb 26, 2018 at 13:52 comment added Jason Is it a convention to use (1+(m1-m2)//s, m2, 1+(n1-n2)//s, n2), instead of (1+(m1-m2)//s, 1+(n1-n2)//s, m2, n2)?
Jan 4, 2018 at 19:31 comment added Divakar @NaN Yeah tensordot leverages BLAS, which should be better than any sum based method.
Jan 4, 2018 at 17:35 comment added NaN @Divakar is there a reason for using np.tensordot rather than c = (np.sum(arr4d * arr2, axis=(2, 3))) is it because your step is 2x2?
Jan 4, 2018 at 15:53 comment added Bharath M Shetty Sir I chose numpy so I can understand the core concepts. I will use scikit once I completely understood the topic.
Jan 4, 2018 at 15:52 comment added Divakar @Dark That scikit version does everything for you, so that could be the easiest way.
Jan 4, 2018 at 15:52 vote accept Bharath M Shetty
Jan 4, 2018 at 15:51 comment added Bharath M Shetty There's so much to understand, I have to analyse every variable and every operation. Thank you
Jan 4, 2018 at 15:49 comment added Divakar @Dark Yeah it's usually 1+(m1-m2). Incorporating the stepsize, we need that division. Think of it, like we are going m2 backwards along the length of m1 and then seeing how many windows can fit in from the start given that stepsize.
Jan 4, 2018 at 15:41 history edited Divakar CC BY-SA 3.0
edited body
Jan 4, 2018 at 15:41 comment added Bharath M Shetty Sir the out_shp of strided4D, is that a formula of some kind?
Jan 4, 2018 at 15:37 history edited Divakar CC BY-SA 3.0
added 102 characters in body
Jan 4, 2018 at 15:37 comment added Bharath M Shetty Strided4D is so beautiful, sir a small explanation of your approach would be really helpful.
Jan 4, 2018 at 15:33 history answered Divakar CC BY-SA 3.0