import numpy as np
import timeit
P = np.random.random((20, 15, 100, 30, 2))
A = np.random.random((20, 15, 100, 5))
H = np.random.random((30, 5))
C = np.random.random((2, 5))
def run():
return np.einsum('abfj,tj,cj->abftc', A, H, C)
times = timeit.Timer(run).timeit(number=10)
print times
Elapsed time: 1.44563603401 seconds
using Einsum
P = zeros(20,15,100,30,2);
A = randn(20,15,100,5);
H = randn(30,5);
C = randn(2,5);
tic()
for i = 1:10
@einsum P[a,b,f,t,c] = A[a,b,f,j]*H[t,j]*C[c,j]
end
toc()
Elapsed time: 85.405141333 seconds