之前准备把所有的问题都写在一个博客里面,我发现实在太长了,所以就把作业一的问题都分开了!
part0:环境的安装和配置
part1:KNN,
part2:SVM
part3:softmax
part4:two_layer_net
part5:feature
part1:KNN
现在就part1进行解释与说明:
一、建议写之前先阅读下面的材料:http://cs231n.github.io/classification/
二、KNN的解释说明
KNN是一个简单的分类器,它包含下面的三个步骤:
step1:读取我们的训练数据,也就是简单的存储;
step2:测试,对于每一张测试的图片,我们将他和所有的训练集的每一个样本计算L2距离,找出离得最近的k张,然后我们将这k张里面出现次数最多的作为这个测试样本的标签;
step3:我们会通过交叉验证集来选择最佳的k值。
三、对于代码的解释和说明
knn.ipynb:
In[1]:
#预备代码
import random
import numpy as np
from cs231n.data_utils import load_CIFAR10
import matplotlib.pyplot as plt
from __future__ import print_function
# This is a bit of magic to make matplotlib figures appear inline in the notebook
# rather than in a new window.
%matplotlib inline#这句话会让matplotlib的画出现在notebook的页面上,而不是新建一个窗口
plt.rcParams['figure.figsize'] = (10.0, 8.0) # 设置默认的绘图窗口的大小
plt.rcParams['image.interpolation'] = 'nearest'
plt.rcParams['image.cmap'] = 'gray'
# 自动的重载Python的外部的模块
# see http://stackoverflow.com/questions/1907993/autoreload-of-modules-in-ipython
%load_ext autoreload
%autoreload 2
In[2]:
# 加载数据集
cifar10_dir = 'cs231n/datasets/cifar-10-batches-py'
X_train, y_train, X_test, y_test = load_CIFAR10(cifar10_dir)
# 打印出数据集的大小
print('Training data shape: ', X_train.shape)
print('Training labels shape: ', y_train.shape)
print('Test data shape: ', X_test.shape)
print('Test labels shape: ', y_test.shape)
输出:
Training data shape: (50000, 32, 32, 3) Training labels shape: (50000,) Test data shape: (10000, 32, 32, 3) Test labels shape: (10000,)
In[3]:
np.flatnonzero(a):返回的是flatten version of a中非零元素的下标;选出y与y_train里面元素相同的下标
np.randomchoice(a,size,replace=True,p=None):随机的选择属于y类别的七个图片进行显示
a:可以是int,那么就是在np.arange(a)里面选择出size个元素,要么是一个数组,从数组中选择出size个元素
size:选择出元素的个数
replace:如果为True,那么每次选出的元素再放回去,可以选出重复的元素;但是如果为False,不重复
p:以概率p选出元素
#可视化数据集,我们对每一个类别选择出其中的七个进行数据的展示
classes = ['plane', 'car', 'bird', 'cat', 'deer', 'dog', 'frog', 'horse', 'ship', 'truck']
num_classes = len(classes)
samples_per_class = 7
for y, cls in enumerate(classes):
idxs = np.flatnonzero(y_train == y)
idxs = np.random.choice(idxs, samples_per_class, replace=False)
for i, idx in enumerate(idxs):
plt_idx = i * num_classes + y + 1
plt.subplot(samples_per_class, num_classes, plt_idx)
plt.imshow(X_train[idx].astype('uint8'))
plt.axis('off')
if i == 0:
plt.title(cls)
plt.show()
结果显示:
In[4]:选出前5000个训练集样本和前500个测试集样本
# 为了跟高效的执行相应的代码,我们选择数据集的子集来进行训练和测试,这里选择的训练数是5000,测试数是500
num_training = 5000
mask = list(range(num_training))
X_train = X_train[mask]
y_train = y_train[mask]
num_test = 500
mask = list(range(num_test))
X_test = X_test[mask]
y_test = y_test[mask]
In[5]:
# 将数据集的每一个样本原本是32*32*3的图片,拉成一个1*3072维度的向量
X_train = np.reshape(X_train, (X_train.shape[0], -1))
X_test = np.reshape(X_test, (X_test.shape[0], -1))
print(X_train.shape, X_test.shape)
结果:
(5000, 3072) (500, 3072)这里的5000,500就是之前说的训练集和测试集的大小
In[6]:
from cs231n.classifiers import KNearestNeighbor
#创建一个KNN的实例,但是我们的训练过程仅仅单独的进行数据的存储,而不会进行计算
classifier = KNearestNeighbor()
classifier.train(X_train, y_train)
In[7]:K_nearest_neighbor.py文档里面的补全代码在之后会介绍!!!
# 在 cs231n/classifiers/k_nearest_neighbor.py 这个文件里,需要我们补全代码
# 实现L2距离的计算,这里我们使用两层循环
# 我们传入的参数是我们的测试集,但是需要注意的是,我们的测试集大小是Nte,训练集大小是Ntr,那我们输出的距离矩阵应该大小是Nte×Ntr。
dists = classifier.compute_distances_two_loops(X_test)
print(dists.shape)
</