多矩阵乘法 [英] Multiple matrix multiplication

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

问题描述

在numpy中,我有N个3x3矩阵的数组.这将是我如何存储它们的一个示例(我正在提取内容):

In numpy, I have an array of N 3x3 matrices. This would be an example of how I'm storing them (I'm abstracting away the contents):

N = 10
matrices = np.ones((N, 3, 3))

我也有一个由3个向量组成的数组,这将是一个示例:

I also have an array of 3-vectors, this would be an example:

vectors = np.ones((N, 3))

我似乎无法弄清楚如何通过numpy将它们相乘,从而实现以下目的:

I can't seem to figure out how to multiply those via numpy, so as to achieve something like this:

result_vectors = []
for matrix, vector in zip(matrices, vectors):
    result_vectors.append(matrix @ vector)

result_vector的形状(在转换为数组时)为(N, 3). 但是,由于速度原因,列表实现是不可能的.

with the result_vector's shape (upon casting to array) being (N, 3). However, a list implementation is out of the question due to speed.

我尝试了各种换位方式的np.dot,但是最终结果没有得到正确的形状.

I've tried np.dot with various transpositions, but the end result didn't get the shape right.

推荐答案

使用步骤:

1)保持第一个轴对齐.

1) Keep the first axes aligned.

2)求和,将输入数组中的最后一个轴彼此相减.

2) Sum-reduce the last axes from the input arrays against each other.

3)让其余的轴(matrices的第二轴)相乘.

3) Let the remainining axes(second axis from matrices) be element-wise multiplied.

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

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