pythonnumpynumpy-ndarraybackpropagationtensordot

Python: numpy.dot / numpy.tensordot for multidimensional arrays


I'm optimising my implementation of the back-propagation algorithm to train a neural network. One of the aspects I'm working on is performing the matrix operations on the set of datapoints (input/output vector) as a batch process optimised by the numpy library instead of looping through every datapoint.

In my original algorithm I did the following:

for datapoint in datapoints:
  A = ... (created out of datapoint info)
  B = ... (created out of datapoint info)

  C = np.dot(A,B.transpose())
____________________

A: (7,1) numpy array
B: (6,1) numpy array
C: (7,6) numpy array

I then expanded said matrices to tensors, where the first shape index would refer to the dataset. If I have 3 datasets (for simplicity purposes), the matrices would look like this:

A: (3,7,1) numpy array
B: (3,6,1) numpy array
C: (3,7,6) numpy array

Using ONLY np.tensordot or other numpy manipulations, how do I generate C?

I assume the answer would look something like this:

C = np.tensordot(A.[some manipulation], B.[some manipulation], axes = (...))

(This is a part of a much more complex application, and the way I'm structuring things is not flexible anymore. If I find no solution I will only loop through the datasets and perform the multiplication for each dataset)


Solution

  • We can use np.einsum -

    c = np.einsum('ijk,ilm->ijl',a,b)
    

    Since the last axes are singleton, you might be better off with sliced arrays -

    c = np.einsum('ij,il->ijl',a[...,0],b[...,0])
    

    With np.matmul/@-operator -

    c = a@b.swapaxes(1,2)