-4

我有以下矩阵:

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

这有尺寸2,3,4,4。所以我需要做的是以这样一种方式填充第一个矩阵,即我们将每个条目复制 4 次,以便可以进行乘法(其中 3 个结果将详细说明为 0,最终结果将是乘法我想)。因此,我需要将第一个矩阵转换为如下所示:

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

等等...

更新:

 def bprop(self, inputs, outputs, grads_wrt_outputs):

        m,n = grads_wrt_outputs.shape[:2]
        o = inputs.shape[2]
        p = inputs.shape[3]
        return (self.mask.reshape(m,n,2,2,2,2)*grads_wrt_outputs[:,:,:,None,:,None]).reshape(m,n,o,p) 

这是我正在使用的场景。

4

2 回答 2

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.        ]]]]
于 2017-11-23T13:45:13.233 回答
1

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

方法#1

为了得到那个重复的版本,我们可以扩展a6Dwithnp.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执行逐元素乘法,最后重新整形回输出。因此,对于数组和,我们将拥有 -6Db4D4Dab

m,n,p,q = a.shape
r,s = b.shape[-2:]
out = (b.reshape(m,n,p,r//p,q,s//q)*a[:,:,:,None,:,None]).reshape(b.shape)
于 2017-11-23T13:57:06.323 回答