且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

将3D矩阵与2D矩阵相乘

更新时间:2022-11-10 10:34:50

似乎您正在尝试通过矩阵乘法使第一个数组的最后两个轴相对于第二个权重数组的仅两个轴丢失.我们可以使用 np.tensordot 将其转换为NumPy代码>,并分别假设arr1arr2作为输入数组,就像这样-

It seems you are trying to lose the last two axes of the first array against the only two axes of the second weight array with that matrix-multiplication. We could translate that idea into NumPy code with np.tensordot and assuming arr1 and arr2 as the input arrays respectively, like so -

np.tensordot(arr1,arr2,axes=([1,2],[0,1]))

放入NumPy代码的另一种更简单的方法是使用 np.einsum ,就像这样-

Another simpler way to put into NumPy code would be with np.einsum, like so -

np.einsum('ijk,jk',arr1,arr2)