我正在做assignment in CS231n。
我想知道为什么one_loop
比two_loop
慢。
我的结果是这样的:
我的代码是这样的:
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笔记本中执行代码。
谢谢。