【问题标题】:Cannot unflat numpy arrays properly无法正确展开 numpy 数组
【发布时间】:2023-03-30 17:03:01
【问题描述】:

我正在尝试为 tensorflow 主题“不平整”numpy 数组。我需要取一个 NxN 矩阵,例如 27x27,然后逐行取所有行元素(每次 27)并将其重塑为 3x3x3 映射(使用 27 列我将得到 3x3x3 x 27maps),我做到了以下函数:

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 函数-- 最初的形状是:(27, 27) 输出形状为:(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.]]])

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

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

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

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

 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

【问题讨论】:

  • 你能提供utest必须看起来如何的例子吗,至少utest[:,:,:,:3]

标签: python arrays numpy tensorflow


【解决方案1】:

Reshape & permute axes-

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

示例运行 -

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]]])

【讨论】:

  • 我认为它很完美:D,我不需要做这么多事情......但我不明白一件事:为什么 utest[:,:,0,0] 返回这个数组( [[ 0, 3, 6], [ 9, 12, 15], [18, 21, 24]]) 而不是这个数组([[[ 0, 1, 2], [ 3, 4, 5], [ 6, 7, 8]])
  • @MarioParreño 您可能错误地解释了坐标轴。见这里-stackoverflow.com/a/41507480/3293881。基本上,使用utest[:,:,0,0] ,您将从预期的 3D 数组中提取每个 2D 切片的第一行,也就是您在问题中发布的那个。
  • 我看到了你的帖子,由于我的结果是一样的,我想问你最后一个。我的最终目的是对一个扁平数组进行 PCA 分析并获得一个 27*27 矩阵(这很好用),然后逐行获取(27 个元素),然后将它们展开以构建一个形状为 [filter_height, filter_width, in_channels, out_channels] (与tensorflow.org/api_docs/python/tf/nn/conv2d 中的参数 'filter' 相同),但我认为我在这个 unflat -> 你的 unflat flated_pa​​tches.reshape(-1,w_patch,h_patch,depth).transpose( 1,2,3,0)
  • @MarioParreño 不清楚格式。但我会考虑使用transpose(1,2,3,0) 中的前三个数字,即尝试(1,3,2,0) 等等,看看其中一个是否有效。
猜你喜欢
  • 2019-08-06
  • 2020-04-27
  • 1970-01-01
  • 2020-01-30
  • 1970-01-01
  • 2020-03-17
  • 1970-01-01
  • 2022-01-25
  • 2018-11-30
相关资源
最近更新 更多