def tile(tensor, dim, repeat):
    """Repeat each element `repeat` times along dimension `dim`"""
    # We will insert a new dim in the tensor and torch.repeat it
    # First we get the repeating counts
    repeat_dims = [1] * len(tensor.size())
    repeat_dims.insert(dim + 1, repeat)
    # And the final dims
    new_dims = list(tensor.size())
    new_dims[dim] = 2 * tensor.size(dim)
    # Now unsqueeze, repeat and reshape
    return tensor.unsqueeze(dim + 1).repeat(*repeat_dims).view(*new_dims)