0

Code is taking too much time for computation and I want to reduce number of iterations. I tried to use method suggested by @Albuquerque How to optimise the following for loop code?, but for this code I have 3d array. Please suggest how to optimize following code.

K=2
D=3
N=3
sigma=np.asarray([[1,2,3],[4,5,6]])
F=np.asarray([[1,2,3],[4,5,6]])
X=np.asarray([[1,2,3],[4,5,6],[7,8,9]])

W=
[
 [
  [1,2,3],
  [3,2,1]
 ],
 [
  [1,1,19],
  [1,2,1]
 ],
 [
  [2,2,2],
  [1,3,5]
 ]
]

result2= np.ones([N, D])
for i in range(N):
   for l in range(D):
     result2[i][l]=np.sum(W[i][k][l]*(F[k][l]+sigma[k][l]*X[i][l]) for k in range(K))


output-
array([[ 26.,  42.,  60.],
      [ 25.,  72., 441.],
      [ 48., 171., 360.]])
3
  • 1
    It would help if you explain what you are trying to do (with an input and output) and not just dump your code. Commented Oct 22, 2019 at 17:20
  • What is this code supposed to do? Right now it looks like some kind of product Commented Oct 22, 2019 at 17:20
  • thanks guys, I solved it. Using numpy einsum, we can perform multi-dimensional array operations. (docs.scipy.org/doc/numpy/reference/generated/numpy.einsum.html) thanks again. Commented Oct 22, 2019 at 23:27

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.