将2D矩阵的Numpy矩阵相乘得到3D矩阵 [英] Numpy matrix multiplication of 2d matrix to give 3d matrix

查看:82
本文介绍了将2D矩阵的Numpy矩阵相乘得到3D矩阵的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个numpy数组,例如

I have two numpy arrays, like

A: = array([[0, 1],  
            [2, 3],  
            [4, 5]])   

B = array([[ 6,  7],  
           [ 8,  9],  
           [10, 11]])

对于A和B的每一行,分别说Ra和Rb,我想计算转置(Ra)* Rb.因此,对于给定的A和B值,我想得到以下答案:

For each row of A and B, say Ra and Rb respectively, I want to calculate transpose(Ra)*Rb. So for given value of A and B, i want following answer:

array([[[ 0,  0],  
        [ 6,  7]],  

       [[ 16,  18],  
        [ 24,  27]],  

       [[ 40,  44],  
        [ 50,  55]]])

我已经编写了以下代码:

I have written the following code to do so:

x = np.outer(np.transpose(A[0]), B[0])
for i in range(1,len(A)):
    x = np.append(x,np.outer(np.transpose(A[i]), B[i]),axis=0)

有没有更好的方法来完成此任务.

Is there any better way to do this task.

推荐答案

您可以将AB的扩展尺寸与

You can use extend dimensions of A and B with np.newaxis/None to bring in broadcasting for a vectorized solution like so -

A[...,None]*B[:,None,:]

说明: np.outer(np.transpose(A[i]), B[i])基本上在版本的A[i]B[i]之间进行逐元素乘法.您要对A中的所有行重复此操作,以免在B中相应地更改行.请注意,np.transpose()似乎没有任何影响,因为np.outer负责预期的元素级乘法.

Explanation : np.outer(np.transpose(A[i]), B[i]) basically does elementwise multiplications between a columnar version of A[i] and B[i]. You are repeating this for all rows in A against corresoinding rows in B. Please note that the np.transpose() doesn't seem to make any impact as np.outer takes care of the intended elementwise multiplications.

我将用矢量化语言描述这些步骤,并像这样实现-

I would describe these steps in a vectorized language and thus implement, like so -

  1. 扩展AB的尺寸以为它们两个形成3D形状,这样我们在两个扩展版本中都保持axis=0对齐并保持为axis=0.因此,我们只能确定最后两个轴.
  2. 要引入元素乘法,请将原始2D版本中的A的 push axis=1转换为其3D版本的axis=1,从而在axis=2处创建一个单维度以进行扩展A的版本.
  3. A版本的3D版本的最后一个单例尺寸必须与B版本的原始2D版本中axis=1版本中的元素对齐,以使broadcasting发生.因此,扩展版本的B会将其2D版本中的axis=1中的元素推入其3D版本中的axis=2中,从而为axis=1创建一个单例尺寸.
  1. Extend dimensions of A and B to form 3D shapes for both of them such that we keep axis=0 aligned and keep as axis=0 in both of those extended versions too. Thus, we are left with deciding the last two axes.
  2. To bring in the elementwise multiplications, push axis=1 of A in its original 2D version to axis=1 in its 3D version, thus creating a singleton dimension at axis=2 for extended version of A.
  3. This last singleton dimension of 3D version of A has to align with the elements from axis=1 in original 2D version of B to let broadcasting happen. Thus, extended version of B would have the elements from axis=1 in its 2D version being pushed to axis=2 in its 3D version, thereby creating a singleton dimension for axis=1.

最后,扩展版本为:A[...,None]& B[:,None,:],乘以谁会给我们带来期望的输出.

Finally, the extended versions would be : A[...,None] & B[:,None,:], multiplying whom would give us the desired output.

这篇关于将2D矩阵的Numpy矩阵相乘得到3D矩阵的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆