Skip to content

Instantly share code, notes, and snippets.

@npinto
Created March 5, 2012 09:55
Show Gist options
  • Select an option

  • Save npinto/1977686 to your computer and use it in GitHub Desktop.

Select an option

Save npinto/1977686 to your computer and use it in GitHub Desktop.
import numpy as np
from numpy.lib.stride_tricks import as_strided
for dtype in ('float32', 'float16'):
print 'trying with', dtype
arr_in = np.random.randn(10, 10, 4).astype(dtype)
window_shape = (5, 5, 1)
arr_shape = np.array(arr_in.shape)
window_shape = np.array(window_shape, dtype=arr_shape.dtype)
arr_in = np.ascontiguousarray(arr_in)
new_shape = tuple(arr_shape - window_shape + 1) + tuple(window_shape)
new_strides = arr_in.strides + arr_in.strides
arr_out = as_strided(arr_in, shape=new_shape, strides=new_strides)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment