【发布时间】:2023-08-02 17:26:01
【问题描述】:
我正在尝试自己完成Stanford CS231n 2017 CNN course 的作业。
我正在尝试仅使用矩阵乘法和 Numpy 广播求和来计算 L2 距离。 L2距离为:
如果我使用这个公式,我想我可以做到:
以下代码显示了计算 L2 距离的三种方法。如果我将compute_distances_two_loops 方法的输出与compute_distances_one_loop 方法的输出进行比较,两者都是相等的。但我将compute_distances_two_loops 方法的输出与compute_distances_no_loops 方法的输出进行比较,其中我只使用矩阵乘法和求和广播实现了L2 距离,它们是不同的。
def compute_distances_two_loops(self, X):
"""
Compute the distance between each test point in X and each training point
in self.X_train using a nested loop over both the training data and the
test data.
Inputs:
- X: A numpy array of shape (num_test, D) containing test data.
Returns:
- dists: A numpy array of shape (num_test, num_train) where dists[i, j]
is the Euclidean distance between the ith test point and the jth training
point.
"""
num_test = X.shape[0]
num_train = self.X_train.shape[0]
dists = np.zeros((num_test, num_train))
for i in xrange(num_test):
for j in xrange(num_train):
#####################################################################
# TODO: #
# Compute the l2 distance between the ith test point and the jth #
# training point, and store the result in dists[i, j]. You should #
# not use a loop over dimension. #
#####################################################################
#dists[i, j] = np.sqrt(np.sum((X[i, :] - self.X_train[j, :]) ** 2))
dists[i, j] = np.sqrt(np.sum(np.square(X[i, :] - self.X_train[j, :])))
#####################################################################
# END OF YOUR CODE #
#####################################################################
return dists
def compute_distances_one_loop(self, X):
"""
Compute the distance between each test point in X and each training point
in self.X_train using a single loop over the test data.
Input / Output: Same as compute_distances_two_loops
"""
num_test = X.shape[0]
num_train = self.X_train.shape[0]
dists = np.zeros((num_test, num_train))
for i in xrange(num_test):
#######################################################################
# TODO: #
# Compute the l2 distance between the ith test point and all training #
# points, and store the result in dists[i, :]. #
#######################################################################
dists[i, :] = np.sqrt(np.sum(np.square(self.X_train - X[i, :]), axis = 1))
#######################################################################
# END OF YOUR CODE #
#######################################################################
print(dists.shape)
return dists
def compute_distances_no_loops(self, X):
"""
Compute the distance between each test point in X and each training point
in self.X_train using no explicit loops.
Input / Output: Same as compute_distances_two_loops
"""
num_test = X.shape[0]
num_train = self.X_train.shape[0]
dists = np.zeros((num_test, num_train))
#########################################################################
# TODO: #
# Compute the l2 distance between all test points and all training #
# points without using any explicit loops, and store the result in #
# dists. #
# #
# You should implement this function using only basic array operations; #
# in particular you should not use functions from scipy. #
# #
# HINT: Try to formulate the l2 distance using matrix multiplication #
# and two broadcast sums. #
#########################################################################
dists = np.sqrt(-2 * np.dot(X, self.X_train.T) +
np.sum(np.square(self.X_train), axis=1) +
np.sum(np.square(X), axis=1)[:, np.newaxis])
print(dists.shape)
#########################################################################
# END OF YOUR CODE #
#########################################################################
return dists
您可以找到完整的工作可测试代码here。
你知道我在compute_distances_no_loops 做错了什么吗?
更新:
抛出错误信息的代码是:
dists_two = classifier.compute_distances_no_loops(X_test)
# check that the distance matrix agrees with the one we computed before:
difference = np.linalg.norm(dists - dists_two, ord='fro')
print('Difference was: %f' % (difference, ))
if difference < 0.001:
print('Good! The distance matrices are the same')
else:
print('Uh-oh! The distance matrices are different')
还有错误信息:
Difference was: 372100.327569
Uh-oh! The distance matrices are different
【问题讨论】:
-
你能仔细检查那里的点乘符号吗,我认为它应该像
X.dot(self.X_train.T)?会是这样吗? -
我无法重现您的错误,当我尝试
np.allclose(compute_distances_no_loops(Y, Z), compute_distances_one_loop(Y, Z))时,它返回True -
运行
compute_distances_no_loops方法后出现错误。 -
@DaniMesejo 我已经用引发错误的代码和错误消息更新了问题。
-
我认为你必须使用广播乘法,而不是点积
标签: python numpy euclidean-distance