我有以下矩阵:
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)
这是我正在使用的场景。