3 个矩阵的 numpy 点积



我希望以特定的方式组合 3 个矩阵,我相信这对于习惯使用 numpy 的人来说都很容易:

w = np.array([[-0.46733567,  0.38864732],
                 [-0.42436867, -1.08760098],
                 [-1.01118741,  0.99096466]])   
a = array([[ 0.63127368,  0.00167775,  0.97812284]])
d = [[-0.43252997]] # although d is length 1 in this example, its often >1 in length and code needs to be able to generalize, there will always be the same length of cols and and arrays between a and w, i.e in this example there are 3 arrays in w and 1 of length 3 in a (please excuse my terminology, I am new to linear algebra).

我正在尝试将它们组合起来以找到 dx,以便:

dx1 = [-0.46733567,  0.38864732] * 0.63127368 * -0.43252997
dx2 = [-0.42436867, -1.08760098] * 0.00167775 * -0.43252997
dx3 = [-1.01118741,  0.99096466] * 0.97812284 * -0.43252997

如果 d>长度为 1,例如 d= np.array([[-0.43252997],[0.87500009]]),则:

dx1_1 = [-0.46733567,  0.38864732] * 0.63127368 * -0.43252997
dx2_1 = [-0.42436867, -1.08760098] * 0.00167775 * -0.43252997
dx3_1 = [-1.01118741,  0.99096466] * 0.97812284 * -0.43252997
dx1_2 = [-0.46733567,  0.38864732] * 0.63127368 * 0.87500009
dx2_2 = [-0.42436867, -1.08760098] * 0.00167775 * 0.87500009
dx3_2 = [-1.01118741,  0.99096466] * 0.97812284 * 0.87500009

尝试了所有这些,但似乎没有成功,除非我错过了什么?

out = np.dot(d, w) * a 
out = np.dot(d, w) * a.T 
out = np.dot(d, w.T) * a 
out = np.dot(a, w) * d
out = np.dot(a, w.T) * d

在很多情况下我收到错误:

ValueError: shapes (3,1) and (3,2) not aligned: 1 (dim 1) != 3 (dim 0)

任何有助于解决此问题的指针将不胜感激

你不需要任何点积,因为你不想对任何东西求和。

我在第三个轴上重复你的数组(如果 d 长于 1,这很重要)。

然后乘以相应的轴...:

w = np.array([[-0.46733567,  0.38864732],
                 [-0.42436867, -1.08760098],
                 [-1.01118741,  0.99096466]])   
a = np.array([[ 0.63127368,  0.00167775,  0.97812284]])
d = np.array([[-0.43252997]])
(dim1, dim2), dim3 = w.shape, len(d[0])  
wnew = w.reshape(dim1, dim2, 1)
anew = a.reshape(dim1, 1, 1)
dnew = d.reshape(1, 1, dim3)
product = wnew * anew * dnew
i, j = 0, 0
print product[i, :, j]

最后一条语句打印您所说的dx_ij

按照输入数组的格式,特别是所有输入数组的形状都是2D的,假设你想将输出存储在3D数组中,这里有一种使用 broadcasting 的方法 -

(w.T)[:,:,None]*(a[0,:,None]*d[:,0])

示例运行 -

In [48]: # Input arrays
    ...: w = np.array([[-0.46733567,  0.38864732],
    ...:                  [-0.42436867, -1.08760098],
    ...:                  [-1.01118741,  0.99096466]])   
    ...: 
    ...: a = np.array([[ 0.63127368,  0.00167775,  0.97812284]])
    ...: 
    ...: d= np.array([[-0.43252997],[0.87500009]])
    ...: 
    ...: dx1_1 = np.array([-0.46733567,  0.38864732]) * 0.63127368 * -0.43252997
    ...: dx2_1 = np.array([-0.42436867, -1.08760098]) * 0.00167775 * -0.43252997
    ...: dx3_1 = np.array([-1.01118741,  0.99096466]) * 0.97812284 * -0.43252997
    ...: dx1_2 = np.array([-0.46733567,  0.38864732]) * 0.63127368 * 0.87500009
    ...: dx2_2 = np.array([-0.42436867, -1.08760098]) * 0.00167775 * 0.87500009
    ...: dx3_2 = np.array([-1.01118741,  0.99096466]) * 0.97812284 * 0.87500009
    ...: 
    ...: # Let's store these in a 3D array
    ...: p1 = np.column_stack((dx1_1,dx2_1,dx3_1))
    ...: p2 = np.column_stack((dx1_2,dx2_2,dx3_2))
    ...: out = np.dstack((p1,p2))
    ...: 
用于

验证的原始和拟议的基于广播的方法的打印输出 -

In [49]: out                                 # Output from original approach
Out[49]: 
array([[[  1.27603568e-01,  -2.58139646e-01],
        [  3.07954650e-04,  -6.22986533e-04],
        [  4.27800472e-01,  -8.65432403e-01]],
       [[ -1.06118124e-01,   2.14674993e-01],
        [  7.89247187e-04,  -1.59663239e-03],
        [ -4.19244884e-01,   8.48124609e-01]]])
In [50]: (w.T)[:,:,None]*(a[0,:,None]*d[:,0]) # Output from proposed solution
Out[50]: 
array([[[  1.27603568e-01,  -2.58139646e-01],
        [  3.07954650e-04,  -6.22986533e-04],
        [  4.27800472e-01,  -8.65432403e-01]],
       [[ -1.06118124e-01,   2.14674993e-01],
        [  7.89247187e-04,  -1.59663239e-03],
        [ -4.19244884e-01,   8.48124609e-01]]])

如果输入数组a并且d1D数组:

a = np.array([ 0.63127368,  0.00167775,  0.97812284])
d = np.array([-0.43252997,0.87500009])

,该解决方案将简化为 -

(w.T)[:,:,None]*(a[:,None]*d)

当你多个两个矩阵时,它们应该有一个共享的维度。在您的示例中:

>>> out = np.dot(a, w)
>>> print out
[[-1.28479419  1.21280327]]

欲了解更多信息: https://en.wikipedia.org/wiki/Matrix_multiplication如果 A 是 n × m 矩阵,B 是 m × p 矩阵,则矩阵乘积 AB(不带乘号或点表示)定义为 n × p 矩阵。

此致敬意亚龙

相关内容

  • 没有找到相关文章