

def pooling_matrix(groups, per_group, strides=None, dtype=None, sparse=None):
    "\n    Construct a pooling matrix, optionally with overlapping pools\n    arranged in a 1 or 2D topology.\n\n    Parameters\n    ----------\n    groups : int or tuple\n        The grid dimensions of a 1- or 2-dimensional pooling grid.\n    per_group : int or tuple\n        The grid dimensions of a single 1- or 2-dimensional feature\n        pool. Must be same length as `groups`.\n    strides : int or tuple, optional\n        The stride of the pools along each dimension. A value of `None`\n        is equivalent to setting equal to `per_group`, i.e. no overlap\n    dtype : dtype object or str, optional\n        The dtype of the resulting pooling matrix.\n    sparse : str, optional\n        If `None`, the function will return a dense matrix (a rank-2\n        `numpy.ndarray`). Specifying 'csc' or 'csr' in this argument will\n        cause the function to return a `scipy.sparse.csc_matrix` or a\n        `scipy.sparse.csr_matrix`, instead.\n\n    Returns\n    -------\n    pools : ndarray or sparse matrix\n        Either a dense 2-dimensional NumPy array or one of\n        `scipy.sparse.csc_matrix` or `scipy.sparse.csr_matrix`, depending\n        on the value of the `sparse` argument. In any case, the shape is\n        `(n_pools, n_filters)` and the value of `pools[i, j]` is 1 if\n        feature `j` is in pool `i`, and 0 otherwise.\n    "

    def _validate_shape(shape, param_name):
        try:
            shape = tuple(shape)
            [int(val) for val in shape]
        except (ValueError, TypeError):
            try:
                shape = (int(shape),)
            except TypeError:
                reraise_as(TypeError(('%s must be int or int tuple' % param_name)))
        return shape
    groups = _validate_shape(groups, 'groups')
    per_group = _validate_shape(per_group, 'per_group')
    if (strides is not None):
        strides = _validate_shape(strides, 'strides')
    else:
        strides = per_group
    if (len(groups) != len(per_group)):
        raise ValueError('groups and per_group must have the same length')
    elif (len(per_group) != len(strides)):
        raise ValueError('per_group and strides must have the same length')
    if ((len(groups) > 2) or (len(per_group) > 2)):
        raise ValueError('only <= 2-dimensional pooling grids are supported')
    if (not all(((stride <= dim) for (stride, dim) in izip(strides, per_group)))):
        raise ValueError('strides must each be <= per_group dimensions')
    try:
        (group_rows, group_cols) = groups
        (rows_per_group, cols_per_group) = per_group
        (row_stride, col_stride) = strides
    except ValueError:
        (group_rows, group_cols) = (groups[0], 1)
        (rows_per_group, cols_per_group) = (per_group[0], 1)
        (row_stride, col_stride) = (strides[0], 1)
    if ((sparse is not None) and (sparse not in ('csc', 'csr'))):
        raise ValueError("sparse must be one of (None, 'csr', 'csc')")
    filter_rows = ((group_rows * row_stride) + (rows_per_group - row_stride))
    filter_cols = ((group_cols * col_stride) + (cols_per_group - col_stride))
    if (dtype is None):
        dtype = theano.config.floatX
    shape = (group_rows, group_cols, filter_rows, filter_cols)
    matrix_shape = ((group_rows * group_cols), (filter_rows * filter_cols))
    if (sparse is not None):
        pools = scipy.sparse.dok_matrix(matrix_shape, dtype=dtype)
    else:
        pools = np.zeros(shape, dtype=dtype)
    for g_row in xrange(group_rows):
        for g_col in xrange(group_cols):
            row_start = (row_stride * g_row)
            row_end = (row_start + rows_per_group)
            col_start = (col_stride * g_col)
            col_end = (col_start + cols_per_group)
            if (sparse is not None):
                for f_row in xrange(row_start, row_end):
                    matrix_cols = slice(((f_row * shape[3]) + col_start), ((f_row * shape[3]) + col_end))
                    matrix_row = ((g_row * shape[1]) + g_col)
                    pools[(matrix_row, matrix_cols)] = 1.0
            else:
                pools[g_row, g_col, row_start:row_end, col_start:col_end] = 1
    if (sparse is not None):
        pools = getattr(pools, ('to' + sparse))()
    else:
        pools = pools.reshape(matrix_shape)
    return pools

dataset/ETHPy150Open lisa-lab/pylearn2/pylearn2/utils/pooling.py pooling_matrix/original