在CS231n中的k最近方法中,one_loop慢于two_loop

时间:2018-12-03 11:48:54

标签: python

我正在做assignment in CS231n

我想知道为什么one_looptwo_loop慢。

我的结果是这样的:

enter image description here

我的代码是这样的:

def compute_distances_one_loop(self, X):
  num_test = X.shape[0]
  num_train = self.X_train.shape[0]
  dists = np.zeros((num_test, num_train))
  for i in range(num_test):
    dists[i] = np.sqrt(np.sum(np.square(X[i] - self.X_train), axis=1))

  return dists

def compute_distances_two_loops(self, X):
   num_test = X.shape[0]
   num_train = self.X_train.shape[0]
   dists = np.zeros((num_test, num_train))
   for i in range(num_test):
      for j in range(num_train):
         dists[i, j] = np.sqrt(np.sum(np.square(X[i] - self.X_train[j])))
   return dists

我正在Jupyter笔记本中执行代码。

谢谢。

0 个答案:

没有答案