0

The problem:

I have two sets of data, A and B, of shapes k x n x m and k x m x p respectively, with k, m and p potentially very large, and hence I'd really like to vectorize any processes upon them.

I'd like to form the dot product between these two datasets such that each kth element in A is dotted with each kth element in B (to form a k x n x p matrix, dotting over the mth axis). At first glance, it looked to me like a combination of hadamard product and dot product (hence the title), but I can't figure out a way to get my desired result.

As reference, because I can't get the vectorization to work well, I'm currently running it like this:

        for b, w in zip(biasList, weightsList):
            z = np.asarray([np.dot(weightMatrix,activation) for weightMatrix, activation in zip(w,activation)])

Which obviously isn't vectorized, so has terrible performance.

Using either numpy dot or numpy einsum (as shown here: vectorized/broadcasted Dot product of numpy arrays with different dimensions) do not give me the desired results, as they seem to only bring matrices of shapes k x n x m and k x m x p together by summing over a pair of indices, yielding a matrix of size k x n x k x p, or over both indices, yielding n x p.

Is there a good way to effectively vectorize this?

0 Answers0