multiply multiple matrices simultaneously

Asked 2 years ago, Updated 2 years ago, 175 views

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.

python image numpy pytorch

2022-09-30 20:21

1 Answers

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.


2022-09-30 20:21

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.