python - 无法正确展开 numpy 数组

标签 python arrays numpy tensorflow

我正在尝试为一个 tensorflow 主题“unflat”numpy 数组。我需要一个 NxN 矩阵,例如 27x27,然后逐行获取所有行元素(每次 27 个)并将其 reshape 为 3x3x3 map (使用 27 列我将得到 3x3x3 x 27 map ),我做到了以下功能:

def unflat_pca(flated_patches, depth=3, verbose=False):
    # tensor with shape [components_width, components_height]
    p_width = flated_patches.shape[0]
    p_height = flated_patches.shape[1]

    # Utilizo 3x3 por la ventana de la convolucion
    res = np.empty((3,3, depth, p_width))

    for one_map in range(p_width):

        map_unflat = np.empty((3,3, depth))

        current_indx = 0
        for d in range(depth):
            # flated_patches matriz cuadrada de pca (PxP)
            map_unflat[:,:,d] = flated_patches[one_map, current_indx:(current_indx+(3*3))].reshape(3,3)
            current_indx += 3*3
            res[:,:, d, one_map] = map_unflat[:,:,d]

    if verbose:
        print("\n-- unflat_pca function --")
        print("The initial shape was: " + str(flated_patches.shape))
        print("The output shape is: " + str(res.shape) + "\n")

    return res #[width, height, depth, pca_maps]

然后当我尝试测试函数时,我传递了一个易于遵循的数组 (0,1,2...) 来尝试观察函数是否正常工作...

utest =  unflat_pca(np.arange(0, 27*27).reshape(27,27), verbose=True)

我明白了

-- unflat_pca function -- The initial shape was: (27, 27) The output shape is: (3, 3, 3, 27)

完美!但是现在,当我检查结果时,例如使用 utest[:,:,:,0],我希望同一个数组中的所有数字都为 1,2,3.... 但得到了

 array([[[  0.,   9.,  18.],
        [  1.,  10.,  19.],
        [  2.,  11.,  20.]],

       [[  3.,  12.,  21.],
        [  4.,  13.,  22.],
        [  5.,  14.,  23.]],

       [[  6.,  15.,  24.],
        [  7.,  16.,  25.],
        [  8.,  17.,  26.]]])

但如果我只检查第一个 channel ,我会得到我预期的结果。

> array([[ 0.,  1.,  2.],
       [ 3.,  4.,  5.],
       [ 6.,  7.,  8.]])

我很困惑,因为后来我使用了未展开的 map 并且得到了糟糕的结果,我认为这是由于第一个结果我没有正确获得数字(按列?!)。你可以帮帮我吗?对不起我的英语:P

PS:utest[:,:,:,0] 的期望值 -> 有序的 3x3x3 map (宽度、高度、深度):

 array([[[  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.]]])

PS2:纸上第一行示例:First row result

最佳答案

Reshape & permute axes -

a.reshape(-1,3,3,3).transpose(1,2,3,0)

sample 运行-

In [482]: a = np.arange(27*27).reshape(27,27)

In [483]: out = a.reshape(-1,3,3,3).transpose(1,2,3,0)

# Verify output shape
In [484]: out.shape
Out[484]: (3, 3, 3, 27)

# Verify output values for the first slice
In [485]: out[...,0]
Out[485]: 
array([[[ 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]]])

关于python - 无法正确展开 numpy 数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48174704/

相关文章:

Python。如何使用 libxml2 获取属性值

Java按第三列的数值对二维字符串数组进行排序

java - 尝试通过复制数组来计算出某个字符集在更大的字符集中出现了多少次。出现错误+这是最好的方法吗?

python - 在哪里可以找到 numpy 百分位数的源代码

python - 寻找 Pandas 的外貌

python - 在Python中将逗号分隔的序列从DataFrame读取为字符串

python - 如何创建 Django 图片库

ios - 为 UITableView 中的每个部分添加特定数据 - Swift

python - numpy网格网格操作问题

python - 对列表进行重复采样