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

蓝咒 提交于 2021-02-10 15:14:00

问题


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)


回答1:


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)


来源:https://stackoverflow.com/questions/62349905/python-numpy-dot-numpy-tensordot-for-multidimensional-arrays

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!