A problem I'm working on states:

Computes the squared Euclidean distance between each element of the training set and each element of the test set. Images should be flattened and treated as vectors.

The training set is a tensor of dimensions: [500, 3, 32, 32]

The test set is a tensor of dimensions: [250, 3, 32, 32]

The dataset is a subsample from CIFAR-10, so these are images. I've flattened both making their dimensions [500, 3072] and [250, 3072] respectively, but I run into a couple questions.

  • Isn't the process of kNN to find the nearest "majority" neighbor of a particular example? Why is step 1 asking me to find a distance between the training and test set here, which makes no sense? (I currently see it as taking the pixel value difference between two random & unrelated pictures.)
  • Shouldn't I instead be finding a distance between the training set a "label", then classify the example to the label with the lowest distance?

I get that the label is a scalar value so taking the actual difference between it and an example is incorrect, I'm just trying to understand it more conceptually. I've tried to not add code to clear headspace, but can toss it in if needed.


1 Answer 1


Took a bit of time, but I understand what I was asking.

The process of kNN is to find the nearest "majority" major (determined by param k). My misunderstanding came from interpreting what the training set was being used for. The training set and the test set both are mapped in the same feature space. To classify the test set images, a distance (usually Euclidean) is taken between each test set example and each training set example (this is why the matrix of the output will be a 500x250).

Each example in the test set then classifies with it's "majority" neighbor. This is why you measure a difference between the two sets.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.