Input X in shape (n,n,m,m),
Output Y in shape (n,n), where Y[i,j]=∑_{k=1}^{n}{||X[i,j]-X[i,k]*X[k,j]||}, with * denoting point-wise multiplication.
The silly for loop version is like:
X = np.random.randint(1,10,size=(5,5,3,3))
n, _, m, _ = X.shape
Y = np.zeros((n, n))
for i in range(n):
for j in range(n):
cnt = 0.0
X_ij = X[i, j] # in shape m x m
for k in range(n):
X_ikj = X[i, k] * X[k, j] # point-wise, in shape m x m
cnt += np.sum(np.abs(X_ij - X_ikj))
Y[i, j] = cnt
However I'd like to use a numpy parallel matrix computation. Exactly Y[i,j]=∑_{k=1}^{n}{||X[i,j]-X[i,k]*X[k,j]||} has a similar form with matmul. So in my view there are basically two points:
- How to
matmulonly along the first two dimensions, while keeping point-wise multiplication for the last two dimensions? matmulalready summarize then-dimvector{X[i,k]*X[k,j]}_{k in [1,n]}. However there is a function applied to eachX[i,k]*X[k,j]before they're summarized.
Any possible idea is appreciated! Thanks.