您可以使用np.tensordot http://docs.scipy.org/doc/numpy/reference/generated/numpy.tensordot.html -
np.tensordot(As,Ms,axes=(0,0))
Or np.einsum http://docs.scipy.org/doc/numpy/reference/generated/numpy.einsum.html -
np.einsum('i,ijk->jk',As,Ms)
样本运行 -
In [41]: As = [2,5,6]
In [42]: Ms = [np.random.rand(2,3),np.random.rand(2,3),np.random.rand(2,3)]
In [43]: sum(a*M for a, M in zip(As, Ms))
Out[43]:
array([[ 6.79630284, 5.04212877, 10.76217631],
[ 4.91927651, 1.98115548, 6.13705742]])
In [44]: np.tensordot(As,Ms,axes=(0,0))
Out[44]:
array([[ 6.79630284, 5.04212877, 10.76217631],
[ 4.91927651, 1.98115548, 6.13705742]])
In [45]: np.einsum('i,ijk->jk',As,Ms)
Out[45]:
array([[ 6.79630284, 5.04212877, 10.76217631],
[ 4.91927651, 1.98115548, 6.13705742]])