DOT PRODUCT IN DEPTH DIRECTION FOR THREE-DIMENSIONAL ARRAY WITHOUT FOR SENTENCE

Asked 1 years ago, Updated 1 years ago, 49 views

I recently started using python for numerical calculations.
For example,

import numpy as np
A = np.zeros (500, 4, 4)    
B=np.identity(4)

Suppose you have a three-dimensional array A and a matrix B.
Suppose A is a zero matrix, but it is actually nonzero.

for i in range (0,500):
    B = np.dot (A[i, :, :], B)

Towards the depth of the elements of 3D array A,

A[499,:,:].dot(A[498,:,:]) ….dot(A[2,:,:]).dot(A[1,:,:]).dot(A[0,:,:])


in determining the specific dot product such as If you use the for statement like the one above, it will take a long time.
Is there any way to describe faster calculations with the same results?

Supplemental
I intend to repeat the process of calculating the 500*4*4 three-dimensional array A in order dozens of times above.
I want to use the results for fitting as well, so I want to speed up the processing speed as much as possible.
This loop was the most time-consuming part of the process, so the current situation is painful.

It depends on the machine you calculate, but in my environment, the three-dimensional array is 1,2,…,499,500 in the depth direction. The time required to calculate the dot product is approximately 0.4 seconds.
I wanted to reduce my goal to about 0.1 seconds.

python numpy

2022-09-30 19:23

2 Answers

The matrix product has a binding law, so use multiprocessing to

Divide 500 matrices (A1 to A500) (A1 to A100, A101 to A200, A401 to A500),
Calculate the matrix product of each group in parallel and
Find the product of each result

If so, the processing time will be reduced.
However, even in that case, the for statement is still required, and
It's better to use a GPU.


2022-09-30 19:23

When I found ufunc.reduce, I thought I could do it, but unfortunately np.dot is not ufunc, so I can't say np.dot.reduce.
https://docs.scipy.org/doc/numpy-1.13.0/reference/generated/numpy.ufunc.reduce.html

However, there seems to be something called np.frompyfunc where you can create ufunc from any function.
https://docs.scipy.org/doc/numpy-1.13.0/reference/generated/numpy.frompyfunc.html#numpy.frompyfunc

Use this to force the ufunc version of np.dot.

udot=np.frompyfunc(np.dot,2,1)

Then you can find the dot product of A in udot.reduce.

udot.reduce(A)

...but I'm using python function after all, so I think it's probably slow.

Similar discussions have been made here, but no good conclusion has been reached.
https://stackoverflow.com/questions/27993153/how-to-fold-accumulate-a-numpy-matrix-product-dot


2022-09-30 19:23

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.