天天看點

【cs231n筆記】assignment1之KNN

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代碼的完成和我的一些注釋

【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.

【cs231n筆記】assignment1之KNN

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.

交叉驗證的實作思路

【cs231n筆記】assignment1之KNN

下面是KNN算法實作的代碼

繼續閱讀