I have an huge 8D array view that i want to reduce to 2D by multiplying the elements together over 4 axes and summing them over 2 axes. I didn´t find any example in the numpy.einsum documentation where the elements of the same array are multiplied with each other. The following works, but requires a lot of additional memory during computation:
import numpy as np
a = np.zeros((2048,2048,64,64,dtype=float32)
a[...] = data
b = np.lib.stride_tricks.sliding_window_view(a,(3,3)+a.shape[2:],axis=(0,1,2,3))
c = np.sum(np.prod(b,axis=(2,3,4,5)),axis=(2,3))
Can this operation be performed by numpy.einsum to save memory?
numpy.einsumis not to save memory. Your code is already looks good. break it into small chucks and run if you cannot fit it into memory. The memory required to createaarray is around 64 gb. So it is not the issue of the function.