数组填充Numpy

时间:2017-11-23 13:33:49

标签: python numpy matrix multidimensional-array max-pooling

我有以下矩阵:

stdin

它的尺寸为:x = \ np.array([[[[0.99256822, 0.63019905], [0.77484078, 0.27471319]], [[0.94722451, 0.95948516], [0.81838252, 0.48979609]], [[0.81673764, 0.9388614], [0.57575844, 0.82265243]]], [[[0.95485566, 0.94870753], [0.92680463, 0.90044481]], [[0.90128127, 0.98683992], [0.9115591, 0.85900321]], [[0.949711, 0.85709163], [0.70392261, 0.91043368]]]]) 。我想要做的是将它与以下矩阵相乘:

2,3,2,2

尺码为y = \ np.array([[[[ 0., 0., 0.63019905, 0. ], [ 0., 0.99256822, 0., 0. ], [ 0.77484078, 0., 0., 0.27471319], [ 0., 0., 0., 0. ]], [[ 0., 0., 0., 0. ], [ 0.94722451, 0., 0., 0.95948516], [ 0.81838252, 0., 0., 0. ], [ 0., 0., 0.48979609, 0. ]], [[ 0., 0., 0., 0. ], [ 0., 0.81673764, 0., 0.9388614 ], [ 0., 0., 0., 0.82265243], [ 0.57575844, 0., 0., 0. ]]], [[[ 0., 0.95485566, 0., 0. ], [ 0., 0., 0., 0.94870753], [ 0., 0.92680463, 0., 0. ], [ 0., 0., 0., 0.90044481]], [[ 0., 0.90128127, 0., 0. ], [ 0., 0., 0., 0.98683992], [ 0., 0.9115591, 0., 0. ], [ 0., 0., 0., 0.85900321]], [[ 0., 0., 0., 0.85709163], [ 0., 0.949711, 0., 0. ], [ 0., 0.70392261, 0., 0.91043368], [ 0., 0., 0., 0. ]]]]) 。所以我需要做的是以这样的方式填充第一个矩阵,即我们将每个条目复制4次,以便可以进行乘法(结果中的3个将详细说明为0,最终结果将是乘法我想要)。因此,我需要将第一个矩阵转换为如下所示:

2,3,4,4

依旧......

更新:

    [[[[ 0.99256822          0.99256822          0.63019905  0.63019905        ]
       [ 0.99256822          0.99256822          0.63019905  0.63019905        ]
        [ 0.77484078         0.77484078          0.27471319  0.27471319]
        [ 0.77484078         0.77484078          0.27471319  0.27471319        ]]

这是我在使用它的场景。

2 个答案:

答案 0 :(得分:1)

您正在寻找的功能是np.repeat。我在最后两个维度上重复了x矩阵两次,如下所示:

>>> x = np.repeat(x, 2, axis=(2))
>>> x = np.repeat(x, 2, axis=(3))
>>> x
  [[[[ 0.99256822  0.99256822  0.63019905  0.63019905]
   [ 0.99256822  0.99256822  0.63019905  0.63019905]
   [ 0.77484078  0.77484078  0.27471319  0.27471319]
   [ 0.77484078  0.77484078  0.27471319  0.27471319]]

  [[ 0.94722451  0.94722451  0.95948516  0.95948516]
   [ 0.94722451  0.94722451  0.95948516  0.95948516]
   [ 0.81838252  0.81838252  0.48979609  0.48979609]
   [ 0.81838252  0.81838252  0.48979609  0.48979609]]
   ...
>>> x.shape
(2, 3, 4, 4)
>>> y.shape
(2, 3, 4, 4)

既然两个矩阵具有相似的形状,它们可以成倍增加:

>>> x * y
[[[[ 0.          0.          0.39715084  0.        ]
   [ 0.          0.98519167  0.          0.        ]
   [ 0.60037823  0.          0.          0.07546734]
   [ 0.          0.          0.          0.        ]]

  [[ 0.          0.          0.          0.        ]
   [ 0.89723427  0.          0.          0.92061177]
   [ 0.66974995  0.          0.          0.        ]
   [ 0.          0.          0.23990021  0.        ]]

  [[ 0.          0.          0.          0.        ]
   [ 0.          0.66706037  0.          0.88146073]
   [ 0.          0.          0.          0.67675702]
   [ 0.33149778  0.          0.          0.        ]]]


 [[[ 0.          0.91174933  0.          0.        ]
   [ 0.          0.          0.          0.90004598]
   [ 0.          0.85896682  0.          0.        ]
   [ 0.          0.          0.          0.81080086]]

  [[ 0.          0.81230793  0.          0.        ]
   [ 0.          0.          0.          0.97385303]
   [ 0.          0.83093999  0.          0.        ]
   [ 0.          0.          0.          0.73788651]]

  [[ 0.          0.          0.          0.73460606]
   [ 0.          0.90195098  0.          0.        ]
   [ 0.          0.49550704  0.          0.82888949]
   [ 0.          0.          0.          0.        ]]]]

答案 1 :(得分:1)

我假设ab分别为两个数组。

方法#1

要获得重复版本,我们可以使用a6D扩展为np.broadcast_to,然后重新转换为4D -

a6D = a[:,:,:,None,:,None]
m,n,p,q = a.shape
r,s = b.shape[-2:]
a_repeated = np.broadcast_to(a6D, (m,n,p,r//p,q,s//q)).reshape(b.shape)

然后,使用a_repeated进行元素乘法与b

方法#2(记忆效率高)

您可以通过添加新轴将a扩展到6D,从而避免任何实际的重复或平铺以提高内存效率,使用6D重新整形{{1}执行逐元素乘法最后重塑回b输出。因此,对于4D数组4Da,我们会有 -

b