I get two 3d matrices A (32x3x3) and B (32x3x3), and I want to get a C matrix with a size of 32x3x3. The calculation can be performed using a loop, for example:
a = numpy.random.rand(32, 3, 3) b = numpy.random.rand(32, 3, 3) c = numpy.random.rand(32, 3, 3) for i in range(32): c[i] = numpy.dot(a[i], b[i])
I believe that to solve this problem there should be a more effective single-line solution. Can someone help, thanks.
You can do this using np.einsum:
np.einsum
In [142]: old = orig(a,b) In [143]: new = np.einsum('ijk,ikl->ijl', a, b) In [144]: np.allclose(old, new) Out[144]: True
einsum , , : (i) (jk,kl->jl)).
einsum
i
(jk,kl->jl)
Source: https://habr.com/ru/post/1656535/More articles:How to change items in a sublist? - pythonHow to add dynamic file to docker container - dockerCNContactStoreDidChangeNotification several times - iosBig O class for (1/2) ^ n - algorithmCommand line for installing / updating .NET Core - asp.net-coreProblem installing openstack via devstack script - openstackThe basic Hangman game in Java (mainly related to string manipulation) - javaFirebase notifications trigger the wrong delegate on iOS 10 - iosMatch Formless HList Types - typesAzure Management REST API - "Authentication failed. Authorization header provided in invalid format." - restAll Articles