import torch


def indices(shape):
    """An implementation of `numpy.indices <https://numpy.org/doc/stable/reference/generated/numpy.indices.html>`_
    for torch.

    Always returns the "sparse" version.

    :param tuple shape:
    :return: A tuple of tensors, each of dimension (1,)*n + (shape[n],) + (1,)*(len(shape) - n - 1), where n is the position in
        the tuple.
    """
    l = len(shape)
    return tuple(torch.arange(shape[a]).reshape((1,)*a + (shape[a],) + (1,)*(l-a-1)) for a in range(l))