def max_pool_forward_im2col(x, pool_param):
    "\n  An implementation of the forward pass for max pooling based on im2col.\n\n  This isn't much faster than the naive version, so it should be avoided if\n  possible.\n  "
    (N, C, H, W) = x.shape
    (pool_height, pool_width) = (pool_param['pool_height'], pool_param['pool_width'])
    stride = pool_param['stride']
    assert (((H - pool_height) % stride) == 0), 'Invalid height'
    assert (((W - pool_width) % stride) == 0), 'Invalid width'
    out_height = (((H - pool_height) / stride) + 1)
    out_width = (((W - pool_width) / stride) + 1)
    x_split = x.reshape((N * C), 1, H, W)
    x_cols = im2col(x_split, pool_height, pool_width, padding=0, stride=stride)
    x_cols_argmax = np.argmax(x_cols, axis=0)
    x_cols_max = x_cols[(x_cols_argmax, np.arange(x_cols.shape[1]))]
    out = x_cols_max.reshape(out_height, out_width, N, C).transpose(2, 3, 0, 1)
    cache = (x, x_cols, x_cols_argmax, pool_param)
    return (out, cache)
An implementation of the forward pass for max pooling based on im2col.

This isn't much faster than the naive version, so it should be avoided if
possible.
def max_pool_forward_im2col(x, pool_param):
    "\n  An implementation of the forward pass for max pooling based on im2col.\n\n  This isn't much faster than the naive version, so it should be avoided if\n  possible.\n  "
    (N, C, H, W) = x.shape
    (pool_height, pool_width) = (pool_param['pool_height'], pool_param['pool_width'])
    stride = pool_param['stride']
    assert (((H - pool_height) % stride) == 0), 'Invalid height'
    assert (((W - pool_width) % stride) == 0), 'Invalid width'
    out_height = (((H - pool_height) / stride) + 1)
    out_width = (((W - pool_width) / stride) + 1)
    x_split = x.reshape((N * C), 1, H, W)
    x_cols = im2col(x_split, pool_height, pool_width, padding=0, stride=stride)
    x_cols_argmax = np.argmax(x_cols, axis=0)
    x_cols_max = x_cols[(x_cols_argmax, np.arange(x_cols.shape[1]))]
    out = x_cols_max.reshape(out_height, out_width, N, C).transpose(2, 3, 0, 1)
    cache = (x, x_cols, x_cols_argmax, pool_param)
    return (out, cache)