python - dot product across multiple axes -


given 2 numpy arrays first d dimensions equal in size

import numpy  d = 3 = numpy.random.rand(2, 2, 2, 12, 3) b = numpy.random.rand(2, 2, 2, 5) 

i compute dot-product across first dimensions. this

a2 = a.reshape(-1, *a.shape[d:]) b2 = b.reshape(-1, *b.shape[d:]) out = numpy.dot(numpy.moveaxis(a2, 0, -1), numpy.moveaxis(b2, 0, -2)) 

works, if b not of shape (2, 2, 2). messing around reshape , moveaxis seems more complicated necessary.

are there more elegant solutions? (perhaps tensordot?)

again use np.einsum

np.einsum('ijklm,ijkn->lmn',a,b) 

Comments

Popular posts from this blog

php - Vagrant up error - Uncaught Reflection Exception: Class DOMDocument does not exist -

vue.js - Create hooks for automated testing -

Add new key value to json node in java -