CS231n assignment1 knn部分

assignment1

學(xué)習(xí)記錄(做的過程中寫個提綱,做完了再完善一下說不定是要理解深刻一點,總之寫下來沒壞處,就當(dāng)紀念啦(?>?<?))

環(huán)境Ubuntu

數(shù)據(jù)集cifar10

先完成knn代碼部分

第一部分

兩層循環(huán)求L2距離 compute_distances_two_loops

要求

#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((self.X_train[j,:]-X[i,:])**2))

一層循環(huán)求L2距離compute_distances_one_loops

# 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(self.X_train-X[i,:],axis=1))

無循環(huán) no_loop

# 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.? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? #

用基本數(shù)組操作、廣播機制

平方差展開

(dists=sqrt(X_train^2+X^2-2*X_train*X))

第二部分

predict_labels

# TODO:? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? #

# Use the distance matrix to find the k nearest neighbors of the ith? ? #

# testing point, and use self.y_train to find the labels of these? ? ? #

# neighbors. Store these labels in closest_y.? ? ? ? ? ? ? ? ? ? ? ? ? #

# Hint: Look up the function numpy.argsort.? ? ? ? ? ? ? ? ? ? ? ? ? ? #

# TODO:? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? #

# Now that you have found the labels of the k nearest neighbors, you? ? #

# need to find the most common label in the list closest_y of labels.? #

# Store this label in y_pred[i]. Break ties by choosing the smaller? ? #

# label.? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? #

使用 numpy.argsort


np.argsort
sort+slice
slice

將dists進行排序存入數(shù)組

再挑出最合適的label

使用np.bincount和np.argmax


argmax:返回沿軸axis最大值的索引 bincount:統(tǒng)計次數(shù),計算數(shù)據(jù)集的標簽列(y_train)的分布

np.argmax
np.bincount

closest_y=self.y_train[np.argsort(dists[i,:])[0:k]]

y_pred[i]=np.argmax(np.bincount(closest_y))

完成后

用jupyter notebook打開knn.ipynb,運行

result


這種作業(yè)做起來好棒啊(注釋說的清楚明白嘿嘿)。

發(fā)現(xiàn)很多感覺懂的真正寫下來很難,有種文字障礙的感覺。

numpy API一點都不熟 ,想著整理又覺得每次搜一下用法也可以,哎可能這就是懶吧。

就是個記錄并非教程所以詳細過程也沒說啦。

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

推薦閱讀更多精彩內(nèi)容

  • 一、前言 CS231n是斯坦福大學(xué)開設(shè)的一門深度學(xué)習(xí)與計算機視覺課程,是目前公認的該領(lǐng)域內(nèi)最好的公開課。目前,該課...
    金戈大王閱讀 4,812評論 3 7
  • import numpy as np class KNearestNeighbor(object): """ a ...
    withism閱讀 515評論 0 1
  • 初見你時,我還是一個留著學(xué)生頭齊劉海沒長開的小姑娘,我說我喜歡你,你問我為什么喜歡你。 再相見時,我們已經(jīng)聊的很熟...
    熊家小妞閱讀 130評論 0 0
  • 最近跟跑步較上了勁,因為不甘心每次自己預(yù)設(shè)的跑步訓(xùn)練計劃總是停留在5km初級階段,而且難以熬過第二周就胎死腹中,然...
    麥田有棵樹閱讀 420評論 0 0
  • 鰥居的經(jīng)濟學(xué)教授Walter二十年只教一門課,過著一潭死水般的日子。導(dǎo)演給主 角配的外觀線條方正的老款沃爾沃V90...
    蟹總有鉗閱讀 2,155評論 7 4