我试着自己做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
5条答案
按热度按时间ergxz8rk1#
以下是如何计算X和Y行之间的成对距离,而无需创建任何三维矩阵:
7gyucuyw2#
这是一个迟来的回应,但我已经用不同的方式解决了这个问题,并想发布它。当我解决这个问题时,我不知道numpy的列-行向量从矩阵中减去。事实证明,我们可以从nxm中减去nx 1或1xm向量,当我们这样做时,从每个行列向量中减去。如果使用不支持这种行为的库,他/她可以用我的。对于这种情况,我已经计算出来了,结果是下面的一个:
这种方法的缺点是它使用更多的内存。
velaa5lx3#
这是我对OP要求的函数
compute_distances_no_loops()
的解决方案。出于性能原因,我没有使用sqrt()
函数:gmxoilav4#
我认为问题来自不一致的阵列形状。
jaxagkaj5#
我认为你在寻找两两之间的距离。
有一个惊人的技巧,在一个单一的行做到这一点。你必须巧妙地发挥广播: