cs231n作业1之knn
这篇博文是对cs231n课程assignment1的第一个问题KNN算法的完成,参考了一些网上的博客,不具有什么创造性,以个人学习笔记为目的发布。
参考:
http://cs231n.github.io/assignments2017/assignment1/ https://blog.csdn.net/Sean_csy/article/details/89028970 https://www.cnblogs.com/daihengchen/p/5754383.html
KNN分类中K=1时,为最邻近分类,其中的K体现在算法中就是选择与测试样本距离最近的前K个训练样本中出现最多的标签即为测试样本的标签。训练KNN分类器需要记忆所有训练样本,速度很慢,实际应用的很少,但是对于理解机器学习以及深度学习中的一些基础概念还是很有帮助的
The kNN classifier consists of two stages:
During training, the classifier takes the training data and simply remembers it
During testing, kNN classifies every test image by comparing to all training images and transfering the labels of the k most similar training examples
The value of k is cross-validated
In this exercise you will implement these steps and understand the basic Image Classification pipeline, cross-validation, and gain proficiency in writing efficient, vectorized code.
下面是我对cs231n assignment1 KNN代码的完成和我的一些注释

We would now like to classify the test data with the kNN classifier. Recall that we can break down this process into two steps:
First we must compute the distances between all test examples and all train examples.
Given these distances, for each test example we find the k nearest examples and have them vote for the label
Lets begin with computing the distance matrix between all training and test examples. For example, if there are Ntr training examples and Nte test examples, this stage should result in a Nte x Ntr matrix where each element (i,j) is the distance between the i-th test and j-th train example.
First, open <code>cs231n/classifiers/k_nearest_neighbor.py</code> and implement the function <code>compute_distances_two_loops</code> that uses a (very inefficient) double loop over all pairs of (test, train) examples and computes the distance matrix one element at a time.
Inline Question #1: Notice the structured patterns in the distance matrix, where some rows or columns are visible brighter. (Note that with the default color scheme black indicates low distances while white indicates high distances.)
What in the data is the cause behind the distinctly bright rows?
What causes the columns?
Your Answer:
极其明亮的行表明这一个测试样本与所有的训练样本都不相似
及其明亮的列表明这一个训练样本与所有的测试样本都不相似
You should expect to see approximately <code>27%</code> accuracy. Now lets try out a larger <code>k</code>, say <code>k = 5</code>:
You should expect to see a slightly better performance than with <code>k = 1</code>.
We have implemented the k-Nearest Neighbor classifier but we set the value k = 5 arbitrarily. We will now determine the best value of this hyperparameter with cross-validation.
交叉验证的实现思路
下面是KNN算法实现的代码