Solved by albanD in post #2 
              
              
                The following should work (and be much faster as it only loops over the number of dimensions, not the whole output: 

def i_tensor(shape = [2,2,2]):
    ndim = len(shape)
    view_shape = [1,] * ndim
    res = 0.
    for d in range(ndim):
        dim_size = shape[d]
        t = torch.arange(dim_size…