2024-04-26 01:35:41 发布
网友
import numpy as np mat1 = np.random.rand(2,3) mat2 = np.random.rand(2,5)
我希望得到一个2x3x5张量,其中每一层是3x5外积,通过将3x1转置行mat1乘以1x5行mat2得到。你知道吗
能用numpy matmul做吗?你知道吗
在用^{}扩展它们的维度之后,您可以简单地使用^{}-
mat1[...,None]*mat2[:,None]
这将是最有效的,因为这里不需要sum-reduction来保证来自np.einsum或np.matmul的服务。你知道吗
sum-reduction
np.einsum
np.matmul
如果您仍然想拖入^{},它基本上与broadcasting相同:
broadcasting
np.matmul(mat1[...,None],mat2[:,None])
使用^{},如果您熟悉它的字符串表示法,它可能会比其他的看起来更整洁一些-
np.einsum('ij,ik->ijk',mat1,mat2) # 23,25->235 (to explain einsum's string notation using axes lens)
在用^{} 扩展它们的维度之后,您可以简单地使用^{} -
这将是最有效的,因为这里不需要
sum-reduction
来保证来自np.einsum
或np.matmul
的服务。你知道吗如果您仍然想拖入^{} ,它基本上与
broadcasting
相同:使用^{} ,如果您熟悉它的字符串表示法,它可能会比其他的看起来更整洁一些-
相关问题 更多 >
编程相关推荐