写了一半,保存一下
import pickle
import numpy as np
import os
import matplotlib.pyplot as plt
#from knn import KNearestNeighbor
class KNearestNeighbor(object):
""" a kNN classifier with L2 distance """
def __init__(self):
pass
def train(self, X, y):
self.X_train = X
self.y_train = y
def predict(self, X, k=1, num_loops=0):
dists = self.compute_distances(X)
return self.predict_labels(dists, k=k)
def compute_distances(self, X):
num_test = X.shape[0]
num_train = self.X_train.shape[0]
dists = np.zeros((num_test, num_train))
return dists
def predict_labels(self, dists, k=1):
num_test = dists.shape[0]
y_pred = np.zeros(num_test)
for i in range(num_test):
closest_y = []
return y_pred
#class KNearestNeighbor(object):
# def __init__(self):
# pass
# def train(self,X,y):
# self.X_train=X
# self.y_train=y
# def predict(self,X,k=1,num_loops=0): #1
# if num_loops== 0:
# dists=self.compute_distances_no_loops(X)
# elif num_loops==1:
# dists=self.compute_distances_one_loop(X)
# elif num_loops==2:
# dists=self.compute_distances_two_loops(X)
# else:
# raise ValueError('Invalid value %d for num_loops' %num_loops)
# return self.predict_labels(dists,k=k)
# def cumpute_distances_two_loops(self,X):
# num_test=X.shape[0]
# num_train=self.X_train.shape[0]
# dists=np.zeros((num_test,num_train))
# print(X.shape,self.X_train.shape)
# for i in range(num_test):
# for j in range(num_train):
# dists[i,j]=np.sqrt(np.sum((X[i,:]‐self.X_train[j,:])**2))
# return dists
# def compute_distances_one_loop(self,X):
# num_test=X.shape[0]
# num_train=self.X_train.shape[0]
# dists=np.zeros((num_test,num_train))
# for i in range(num_test):
# dists[i,:]=np.sqrt(np.sum(np.square(self.X_train‐X[i,:]),axis=1))
# return dists
# def compute_distances_no_loops(self,X):
#
# num_test = X.shape[0]
# num_train = self.X_train.shape[0]
# dists = np.zeros((num_test, num_train))
# test_sum=np.sum(np.square(X),axis=1)
# train_sum=np.sum(np.square(self.X_train),axis=1)
# inner_product=np.dot(X,self.X_train.T)
# dists=np.sqrt(‐2*inner_product+test_sum.reshape(‐1,1)+train_sum)
# return dists
# def predict_labels(self,dists,k=1): #2
# num_test=dists.shape[0]
# y_pred=np.zeros(num_test)
# for i in range(num_test):
# closest_y=[]
# y_indicies=np.argsort(dists[i,:],axis=0) #2.1
# closest_y=self.y_train[y_indicies[: k]] #2.2
# y_pred[i]=np.argmax(np.bincount(closest_y)) #2.3
# return y_pred
def load_cifar_batch(filename):
with open(filename,'rb') as f :
datadict=pickle.load(f,encoding='bytes')
x=datadict[b'data']
y=datadict[b'labels']
x=x.reshape(10000,3,32,32).transpose(0,2,3,1).astype('float')
y=np.array(y)
return x,y
def load_cifar10(root):
xs=[]
ys=[]
for b in range(1,6):
f=os.path.join(root,'data_batch_%d' % (b,))
x,y=load_cifar_batch(f)
xs.append(x)
ys.append(y)
Xtrain=np.concatenate(xs) #1
Ytrain=np.concatenate(ys)
del x ,y
Xtest,Ytest=load_cifar_batch(os.path.join(root,'test_batch')) #2
return Xtrain,Ytrain,Xtest,Ytest
x_train,y_train,x_test,y_test=load_cifar10(r'E:\cs231n\DL_assignment\assignment1\assignment1_\cs231n\datasets\cifar-10-batches-py')
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)
#选取一些图片并进行展示
classes=['plane','car','bird','cat','deer','dog','frog','horse','ship','truck']
num_claesses=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_claesses+y+1
plt.subplot(samples_per_class,num_claesses,plt_idx)
plt.imshow(x_train[idx].astype('uint8'))
plt.axis('off')
if i ==0:
plt.title(cls)
plt.show()
#我们只选取5000张训练集,500张测试集
num_training=5000
mask=range(num_training)
x_train=x_train[mask]
y_train=y_train[mask]
num_test=500
mask=range(num_test)
x_test=x_test[mask]
y_test=y_test[mask]
#为了欧氏距离的计算,我们把得到的图像数据拉长成行向量
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[0].shape,x_test.shape)
#classifier=KNearestNeighbor()
#classifier.train(x_train,y_train)
#dists=classifier.compute_distances(x_test)
#print(dists.shape)