I'm just starting with Python and I'm having trouble understanding how I'm supposed to achieve the following goal (I'm a Java programmer).
Here is the initial code:
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 range(num_test):
for j in range(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.sum(np.square(X[i] - self.X_train[j]))
#####################################################################
# END OF YOUR CODE #
#####################################################################
return dists
Here is the piece of code that is supposed to have one less nested loop while still outputing the same array:
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 range(num_test):
tmp = '%s %d' % ("\nfor i:", i)
print(tmp)
print(X[i])
print("end of X[i]")
print(self.X_train[:]) # all the thing [[ ... ... ]]
print(": before, i after")
print(self.X_train[i]) # just a row
print(self.X_train[i, :])
#######################################################################
# TODO: #
# Compute the l2 distance between the ith test point and all training #
# points, and store the result in dists[i, :]. #
#######################################################################
dists[i, :] = np.sum(np.square(X[i] - self.X_train[i, :]))
print(dists[i])
#######################################################################
# END OF YOUR CODE #
#######################################################################
return dists
It seems like this should have helped me, but I still can't figure it out.
You can see that my pitfall is, among other things, my poor understanding of how the ":" works exactly.
I've spent hours trying to figure this thing out, but it seems like I'm really lacking some core knowledge. Anyone can help me out? This exercise comes for a Stanford course on Visual Recognition: it's the first Assignment, but it isn't a real homework of mine since I'm only doing the course for pleasure, on my own.
Currently, my piece of code outputs the proper value of the diagonal of the two_loops, but for the whole row. I don't understand how I'm supposed to synchronize the : from dists[i, :] with the - self.X_train[i, :] part. How to compute X[i] minus an iteration that goes through the whole self.X_train ?
Note: num_test is 500x3072 and num_train is 5000x3072. The 3072 comes from 32x32x3 which are the RGB values of 32x32 pictures. dists[i,j] is a 500x5000 matrix that maps the L2 distance between the ith element of num_test and the jth element of num_train.