数学方程的 Numpy 向量化

Numpy vectorization of mathematical equation

我用 for 循环写了等式

A = np.random.rand(10, 100)
B = np.random.rand(100, 100)
c = np.random.rand(100)

@timeit
def operate_loops(X, W, b):
  y1 = 0
  y2 = 0
  y3 = 0
  l1 = 0
  l2 = 0
  for j in range(np.shape(X)[0]):
      for n in range(np.shape(W)[1]):
          for m in range(np.shape(W)[0]):
              y1=y1+X[j][m] * W[m][n]
          y2=y2+y1+b[n]
      y3=y3+y2

  return y3

现在我想将这个等式写成 numpy 向量化代码,而不使用循环。我先做求和 像:

np.sum(x[0,:]*w[:,0])

但我不知道如何在不循环的情况下进行其他 sigma 求和

你的等式转化为以下

y = np.sum(X@W) + X.shape[0]*np.sum(b)

Murali's 是正确的,但有点过于复杂(请大家相信广播)

我比较简单的提议是

y = np.sum(X@W+b)

这里是其应用的一个小例子

In [20]: import numpy as np
    ...: X, W, b = np.ones((3,7)), np.ones((7,4)), np.arange(1,5)
    ...: print(b)
    ...: print(X@W)
    ...: print(X@W+b)
    ...: print((8+9+10+11)*3)
    ...: print(np.sum(X@W) + X.shape[0]*np.sum(b)) # Murali's proposal
[1 2 3 4]
[[7. 7. 7. 7.]
 [7. 7. 7. 7.]
 [7. 7. 7. 7.]]
[[ 8.  9. 10. 11.]
 [ 8.  9. 10. 11.]
 [ 8.  9. 10. 11.]]
114
114.0

In [21]: print(y:=np.sum(X@W+b))
114.0

正确答案是:y = np.sum(X@W) + X.shape[0]*np.sum(b)(对我来说更清楚) 或:y = np.sum(X@W+b)

我的 operate_loops 函数很糟糕,因为我误解了等式的含义。 gboffi 的评论帮助我理解了它:

the meaning of A_mn + B_n is a matrix where each row, of length N, is summed term by term with the vector B also of length N.

我的新 operate_loops 函数是:

 def operate_loops(X, W, b):
    y1 = 0
    for j in range(np.shape(X)[0]):
        for n in range(np.shape(W)[1]):
            for m in range(np.shape(W)[0]):
                y1 = y1 + X[j][m] * W[m][n]
            y1=y1+b[n]
    return y1