Please tell me how to do the following calculations at high speed.

```
import numpy as np
# It's actually about 100,000 long.
a = np.array([1, 2, 3], [4, 5, 6]])
# It's about 100,000 feet long.
b=np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9], [1, 2, 3], [4, 5, 6], [7, 8, 9]] ])
res = [ ]
for i in range (a.shape[0]):
for jin range (b.shape[0]):
c=np.dot(a[i], b[i,j])
res.append(c)
```

Please let me know if there is a way to calculate the product of an array (matrix) containing vectors and an array (triple tensor) containing matrices at high speed without using the for statement above.

python python3 numpy

2022-09-30 14:13

The python for statement is slow, so I think you want to process it with a properly accelerated (should) numpy.

I have no experience in calculating tensors, but `numpy`

has `numpy.tensordot`

and `numpy.einsum`

which can calculate tensor product and tensor dot product, so if you use it well, it will be easy to write.

Looking at the code you wrote, it seems that you want to calculate the dot product, so I tried it.(The code for the question is `IndexError`

, so there is no proof, but does the following match the intent of the question?)

See if you can speed it up.

```
import numpy as np
# It's actually about 100,000 long.
a = np.array([1, 2, 3], [4, 5, 6]])
# It's about 100,000 feet long.
b=np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9], [2, 3, 4], [5, 6, 7], [8, 9, 10]] ])
c=np.tensordot(a,b.T,1)
x,y,z = c.shape
tmp = c.reshape (1, x*y*z)
res=tmp[0].tolist()
```

Also, I referred to the following.

- NumPy accelerates numerical calculations:Basic -Qiita
- Bulk processing of vector and matrix product operations in Python-numpy | teratail

Alternatively, using a library with parallel processing like `disk`

or using a GPU using `cupy`

might speed up.

2022-09-30 14:13

Using multiprocessing to parallelize the processing of the innermost for loop will increase the processing speed.

When parallelized, you don't know in which order the calculations will be returned, so

```
res.append(c)
```

Then I don't know if it's in order.

I think it is necessary to devise a code that specifies where to put the results in the array res.

```
res[i*N+j]=np.dot(a[i],b[j])
```

2022-09-30 14:13

Popular Tags

python x 4429
android x 1590
java x 1475
javascript x 1383
c x 903
c++ x 830
ruby-on-rails x 680
php x 678
python3 x 651
html x 631

© 2022 OneMinuteCode. All rights reserved.