Python
I would like to multiply multiple matrices simultaneously with numpy or pytorch without using a for loop.
For example, suppose you have five different matrices of 10x100 and one matrix of 100x100.
import numpy as np
a=np.random.rand(5,10,100)
b=np.random.rand(100,100)
Five different matrices where a is 10x100 and b is 100x100.
c=np.zeros(5,10,100))
for i in range (len(a)) :
c[i] = np.dot(a[i], b)
Or you can use pytorch to
import torch
c=torch.zero((5,10,100))
a=torch.randn(5,10,100)
b=torch.randn(100,100)
for i in range (len(a)) :
c[i]=torch.mm(a[i],b)
I would appreciate it if you could let me know if you have any ideas.
You can do it with a for statement like this, but if the number of a increases from 5, it will be delayed.
Matrix b is the same thing, so I would like to multiply the matrix in parallel.
I thought the actual procession would be around 40,000 x 40,000 and the calculation time would be a problem, but when I calculated using the GPU, it took about 0.01 seconds to run.
This post was edited based on @potiki's Comment and posted as Community Wiki.This post was edited based on @potiki's Comment and posted as Community Wiki.
617 Uncaught (inpromise) Error on Electron: An object could not be cloned
572 rails db:create error: Could not find mysql2-0.5.4 in any of the sources
581 PHP ssh2_scp_send fails to send files as intended
912 When building Fast API+Uvicorn environment with PyInstaller, console=False results in an error
© 2024 OneMinuteCode. All rights reserved.