numpy:将矩阵与3d张量相乘-建议 [英] Numpy: Multiplying a matrix with a 3d tensor -- Suggestion

查看:92
本文介绍了numpy:将矩阵与3d张量相乘-建议的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个形状为MxN的矩阵P和一个形状为KxNxR的3d张量T.我想将PT中的每个NxR矩阵相乘,得到一个KxMxR 3d张量.

I have a matrix P with shape MxN and a 3d tensor T with shape KxNxR. I want to multiply P with every NxR matrix in T, resulting in a KxMxR 3d tensor.

P.dot(T).transpose(1,0,2)给出所需的结果.是否有 nicer 解决方案(即摆脱transpose)来解决此问题?这一定是很常见的操作,因此我认为其他人已经找到了不同的方法,例如使用tensordot(我尝试但未能获得所需的结果).意见/观点将不胜感激!

P.dot(T).transpose(1,0,2) gives the desired result. Is there a nicer solution (i.e. getting rid of transpose) to this problem? This must be quite a common operation, so I assume, others have found different approaches, e.g. using tensordot (which I tried but failed to get the desired result). Opinions/Views would be highly appreciated!

推荐答案

scipy.tensordot(P, T, axes=[1,1]).swapaxes(0,1)

这篇关于numpy:将矩阵与3d张量相乘-建议的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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