数组切片的累积和:两种方法

Cumulative sum over slices of array: Two approaches

我很难理解为什么以下两个代码示例会产生不同的结果:

代码 1:

for h in range(n_H):                       
    for w in range(n_W):               

        # Find indices
        vert_start = h * stride                  # Starting row-index for current slice 
        vert_end = vert_start + f                # Final row-index (+1) for current slice
        horiz_start = w * stride                 # Starting column-index for current slice 
        horiz_end = horiz_start + f              # Final column-index (+1) for current slice

        for c in range(n_C):
            Aux = (W[:, :, :, c] * Z[:, h, w, c, np.newaxis, np.newaxis, np.newaxis])
            A[:, vert_start:vert_end, horiz_start:horiz_end, :] += Aux

代码 2:

for h in range(n_H):                       
    for w in range(n_W):               

        # Find indices
        vert_start = h * stride                  # Starting row-index for current slice 
        vert_end = vert_start + f                # Final row-index (+1) for current slice
        horiz_start = w * stride                 # Starting column-index for current slice 
        horiz_end = horiz_start + f              # Final column-index (+1) for current slice

        Aux = np.zeros((m, f, f, n_CP))
        for c in range(n_C):
            Aux += (W[:, :, :, c] * Z[:, h, w, c, np.newaxis, np.newaxis, np.newaxis])
        A[:, vert_start:vert_end, horiz_start:horiz_end, :] += Aux

在这两种情况下

我注意到当 "index ranges"(vert_start:vert_end 和 horiz_start:horiz_end) 是标量时,这两种方法产生相同的结果,即 f=1。但是,我不明白为什么它也不适用于范围。

您可以在下面找到一个代码示例导致不同输出的示例:

np.random.seed(1)
m = 2
f = 2
stride = 1
n_C = 3
n_CP = 1
n_H = 2
n_W = 2
n_HP = 3
n_WP = 3

W = np.random.randn(f, f, n_CP, n_C)
Z = np.random.rand(m, n_H, n_W, n_C)
A = np.zeros((m, n_HP, n_WP, n_CP))
A2 = np.zeros((m, n_HP, n_WP, n_CP))

for h in range(n_H):                     
    for w in range(n_W):               

        # Find indices
        vert_start = h * stride                  # Starting row-index for current slice 
        vert_end = vert_start + f                # Final row-index (+1) for current slice
        horiz_start = w * stride                 # Starting column-index for current slice 
        horiz_end = horiz_start + f              # Final column-index (+1) for current slice

        for c in range(n_C):
            Aux = (W[:, :, :, c] * Z[:, h, w, c, np.newaxis, np.newaxis, np.newaxis])
            A[:, vert_start:vert_end, horiz_start:horiz_end, :] += Aux

        Aux = np.zeros((m, f, f, n_CP))
        for c in range(n_C):
            Aux += (W[:, :, :, c] * Z[:, h, w, c, np.newaxis, np.newaxis, np.newaxis])
        A2[:, vert_start:vert_end, horiz_start:horiz_end, :] += Aux

print(A == A2) 

虽然打印 A 和 A2 时看起来没有区别,但这只是 Python 显示结果的方式所致。输出(A - A2)可以看出在标为"False"的位置确实存在微小的差异。然而,不同之处在于维度e-16。所以这只是一个舍入误差。