CS231N assignment1

1# Visualize some examples from the dataset. 2# We show a few examples of training images from each class. 3classes = ['plane', 'car', 'bird', 'cat', 'deer', 'dog', 'frog', 'horse', 'ship', 'truck'] #类别列表 4num_classes = len(classes) #类别数目 5samples_per_class = 7 # 每个类别采样个数 6for y, cls in enumerate(classes): # 对列表的元素位置和元素进行循环,y表示元素位置(0,num_class),cls元素本身'plane'7 idxs = np.flatnonzero(y_train == y) #找出标签中y类的位置 8 idxs = np.random.choice(idxs, samples_per_class, replace=False) #从中选出我们所需的7个样本 9 for i, idx in enumerate(idxs): #对所选的样本的位置和样本所对应的图片在训练集中的位置进行循环 10 plt_idx = i * num_classes + y + 1 # 在子图中所占位置的计算 11 plt.subplot(samples_per_class, num_classes, plt_idx) # 说明要画的子图的编号 12 plt.imshow(X_train[idx].astype('uint8')) # 画图 13 plt.axis('off') 14 if i == 0: 15 plt.title(cls) # 写上标题,也就是类别名 16plt.show() # 显示

用矩阵运算取代两次循环运算,大大减少运算时间。

核心的公式:https://blog.csdn.net/zhyh1435589631/article/details/54236643

https://blog.csdn.net/geekmanong/article/details/51524402

 我自己的经验总结:先看最终目标矩阵的大小,可以确定前面位置。

 

交叉验证这里:

  在进行分类前,一定要通过reshape函数,来确定数据输入的形状是不是符合要求。

1num_folds = 5 2k_choices = [1, 3, 5, 8, 10, 12, 15, 20, 50, 100] 3 4X_train_folds = [] 5y_train_folds = [] 6################################################################################ 7# TODO: # 8# Split up the training data into folds. After splitting, X_train_folds and # 9# y_train_folds should each be lists of length num_folds, where # 10# y_train_folds[i] is the label vector for the points in X_train_folds[i]. # 11# Hint: Look up the numpy array_split function. # 12################################################################################ 13X_train_folds=np.array_split(X_train,num_folds) 14y_train_folds=np.array_split(y_train,num_folds) 15 16################################################################################ 17# END OF YOUR CODE # 18################################################################################ 19 20# A dictionary holding the accuracies for different values of k that we find 21# when running cross-validation. After running cross-validation, 22# k_to_accuracies[k] should be a list of length num_folds giving the different 23# accuracy values that we found when using that value of k. 24k_to_accuracies = {} 25 26 27################################################################################ 28# TODO: # 29# Perform k-fold cross validation to find the best value of k. For each # 30# possible value of k, run the k-nearest-neighbor algorithm num_folds times, # 31# where in each case you use all but one of the folds as training data and the # 32# last fold as a validation set. Store the accuracies for all fold and all # 33# values of k in the k_to_accuracies dictionary. # 34################################################################################ 35num_test = X_train_folds[0].shape[0] 36for j in range(len(k_choices)): 37 k = k_choices[j] 38 for i in range(1,num_folds+1): 39 X_train_temp = np.concatenate((X_train_folds[num_folds-i],X_train_folds[num_folds-i-1],X_train_folds[num_folds-i-2],X_train_folds[num_folds-i-3]),axis = 0) 40 y_train_temp = np.concatenate((y_train_folds[num_folds-i],y_train_folds[num_folds-i-1],y_train_folds[num_folds-i-2],y_train_folds[num_folds-i-3])) 41 X_test_temp = X_train_folds[num_folds-i-4] 42 y_test_temp = y_train_folds[num_folds-i-4] 43 classifier.train(X_train_temp, y_train_temp) 44 y_test_pred = classifier.predict(X_test_temp, k=k) 45 num_correct = np.sum(y_test_pred == y_test_temp) 46 accuracy = float(num_correct) / num_test 47 k_to_accuracies.setdefault(k,[]).append(accuracy) 48 49################################################################################ 50# END OF YOUR CODE # 51################################################################################ 52 53# Print out the computed accuracies 54for k in sorted(k_to_accuracies): 55 for accuracy in k_to_accuracies[k]: 56 print('k = %d, accuracy = %f' % (k, accuracy))

将所有数据分为train/val/test三组,使用train训练,用val调整超参数,在最后的最后,才可以使用test,并且test只允许使用这一次,并将这一次的结果作为最终结果上报。否则得到的classifier会overfitting,或者结果不准确,有cheat的嫌疑。

Evaluate on the test set only a single time, at the very end.

所谓5-fold cross validation就是将所有的train data均匀分成5份,每次取4份做train,另外一份做val,重复五次,将五次结果平均。这样做的话每个数据都做了四次train,一次val。这样做的缺点是太expensive,NN中通常不用。注意,在这个过程中,test是不参与其中的。一定先将test set拿出来放到一边,不到最后交结果的时候不要碰它。

 

1,2显然不正确;因为kNN是非线性分类器,所以边界也是非线性的;training set越大,在predict时需要计算test example与所有training的距离,所以在相同算力条件下,taining set越大,predict一个test sample所需时间越多,时间复杂度为O(N)。

点赞
收藏

评论区

加载中...

相关推荐

MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1

文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s

Oracle 分组与拼接字符串同时使用

SELECTT.,ROWNUMIDFROM(SELECTT.EMPLID,T.NAME,T.BU,T.REALDEPART,T.FORMATDATE,SUM(T.S0)S0,MAX(UPDATETIME)CREATETIME,LISTAGG(TOCHAR(

MySQL部分从库上面因为大量的临时表tmp_table造成慢查询

背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_

皕杰报表之UUID

​在我们用皕杰报表工具设计填报报表时,如何在新增行里自动增加id呢?能新增整数排序id吗?目前可以在新增行里自动增加id,但只能用uuid函数增加UUID编码,不能新增整数排序id。uuid函数说明:获取一个UUID,可以在填报表中用来创建数据ID语法:uuid()或uuid(sep)参数说明:sep布尔值,生成的uuid中是否包含分隔符'',缺省为

手写Java HashMap源码

HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22

2020年前端实用代码段,为你的工作保驾护航

有空的时候,自己总结了几个代码段,在开发中也经常使用,谢谢。1、使用解构获取json数据let jsonData  id: 1,status: "OK",data: 'a', 'b';let  id, status, data: number   jsonData;console.log(id, status, number )

CS231N assignment1 - HelloWorld