Here is the code without nditer, provided from stackoverflow 9.
#alternative for nditer, but for both tensor and ndarray
def deep_iter(data, ix=tuple()):
    try:
        for i, element in enumerate(data):
            yield from deep_iter(element, ix + (i,))
    except:
        yield ix, data

#do average calculation for a specific element
def calc_average(t, *ix):
    t_dim = len(t.shape)
    out = 0.
    for i in range(t_dim):
        #ipdb.set_trace()
        ix_ = list(ix)
        ix_[i] +=1
        out += t[tuple(ix_)]**2
        ix_[i] -=2
        out += t[tuple(ix_)]**2
    return out/(2*t_dim)    

#loop over entire tensor
def averaging(t):
    new_values = {}
    t_dim = len(t.shape)
    for ix, value in deep_iter(t):
        if all([0<ix[j]<t.shape[j]-1 for j in range(t_dim)]):
            new_values[ix] = calc_average(t, *ix) 

    for ix, new_value in new_values.items():
        t[ix] = new_value
        
    return t

#test
t1 = torch.arange(36, dtype= torch.float32).reshape(3,4,3)
t1
> tensor([[[ 0.,  1.,  2.],
         [ 3.,  4.,  5.],
         [ 6.,  7.,  8.],
         [ 9., 10., 11.]],

        [[12., 13., 14.],
         [15., 16., 17.],
         [18., 19., 20.],
         [21., 22., 23.]],

        [[24., 25., 26.],
         [27., 28., 29.],
         [30., 31., 32.],
         [33., 34., 35.]]])
> averaging(t1)
>  tensor([[[  0.0000,   1.0000,   2.0000],
         [  3.0000,   4.0000,   5.0000],
         [  6.0000,   7.0000,   8.0000],
         [  9.0000,  10.0000,  11.0000]],

        [[ 12.0000,  13.0000,  14.0000],
         [ 15.0000, 307.3333,  17.0000],
         [ 18.0000, 412.3333,  20.0000],
         [ 21.0000,  22.0000,  23.0000]],

        [[ 24.0000,  25.0000,  26.0000],
         [ 27.0000,  28.0000,  29.0000],
         [ 30.0000,  31.0000,  32.0000],
         [ 33.0000,  34.0000,  35.0000]]])