参考:http://cs231n.github.io/assignment1/

Q1: k-Nearest Neighbor classifier (30 points)

  1 import numpy as np
  2 from matplotlib.cbook import todate
  3
  4 class KNearestNeighbor:
  5   """ a kNN classifier with L2 distance """
  6
  7   def __init__(self):
  8     pass
  9
 10   def train(self, X, y):
 11     """
 12     Train the classifier. For k-nearest neighbors this is just
 13     memorizing the training data.
 14
 15     Input:
 16     X - A num_train x dimension array where each row is a training point.
 17     y - A vector of length num_train, where y[i] is the label for X[i, :]
 18     """
 19     self.X_train = X
 20     self.y_train = y
 21
 22   def predict(self, X, k=1, num_loops=0):
 23     """
 24     Predict labels for test data using this classifier.
 25
 26     Input:
 27     X - A num_test x dimension array where each row is a test point.
 28     k - The number of nearest neighbors that vote for predicted label
 29     num_loops - Determines which method to use to compute distances
 30                 between training points and test points.
 31
 32     Output:
 33     y - A vector of length num_test, where y[i] is the predicted label for the
 34         test point X[i, :].
 35     """
 36     if num_loops == 0:
 37       dists = self.compute_distances_no_loops(X)
 38     elif num_loops == 1:
 39       dists = self.compute_distances_one_loop(X)
 40     elif num_loops == 2:
 41       dists = self.compute_distances_two_loops(X)
 42     else:
 43       raise ValueError('Invalid value %d for num_loops' % num_loops)
 44
 45     return self.predict_labels(dists, k=k)
 46
 47   def compute_distances_two_loops(self, X):
 48     """
 49     Compute the distance between each test point in X and each training point
 50     in self.X_train using a nested loop over both the training data and the
 51     test data.
 52
 53     Input:
 54     X - An num_test x dimension array where each row is a test point.
 55
 56     Output:
 57     dists - A num_test x num_train array where dists[i, j] is the distance
 58             between the ith test point and the jth training point.
 59     """
 60     num_test = X.shape[0]
 61     num_train = self.X_train.shape[0]
 62     dists = np.zeros((num_test, num_train))
 63     for i in xrange(num_test):
 64       for j in xrange(num_train):
 65         #####################################################################
 66         # TODO:                                                             #
 67         # Compute the l2 distance between the ith test point and the jth    #
 68         # training point, and store the result in dists[i, j]               #
 69         #####################################################################
 70         dists[i,j] = np.sqrt(np.sum(np.square(X[i,:] - self.X_train[j,:])))
 71         #####################################################################
 72         #                       END OF YOUR CODE                            #
 73         #####################################################################
 74     return dists
 75
 76   def compute_distances_one_loop(self, X):
 77     """
 78     Compute the distance between each test point in X and each training point
 79     in self.X_train using a single loop over the test data.
 80
 81     Input / Output: Same as compute_distances_two_loops
 82     """
 83     num_test = X.shape[0]
 84     num_train = self.X_train.shape[0]
 85     dists = np.zeros((num_test, num_train))
 86     for i in xrange(num_test):
 87       #######################################################################
 88       # TODO:                                                               #
 89       # Compute the l2 distance between the ith test point and all training #
 90       # points, and store the result in dists[i, :].                        #
 91       #######################################################################
 92       dists[i, :] = np.sqrt(np.sum(np.square(self.X_train - X[i,:]), axis=1))
 93       #######################################################################
 94       #                         END OF YOUR CODE                            #
 95       #######################################################################
 96     return dists
 97
 98   def compute_distances_no_loops(self, X):
 99     """
100     Compute the distance between each test point in X and each training point
101     in self.X_train using no explicit loops.
102
103     Input / Output: Same as compute_distances_two_loops
104     """
105     num_test = X.shape[0]
106     num_train = self.X_train.shape[0]
107     dists = np.zeros((num_test, num_train))
108     #########################################################################
109     # TODO:                                                                 #
110     # Compute the l2 distance between all test points and all training      #
111     # points without using any explicit loops, and store the result in      #
112     # dists.                                                                #
113     # HINT: Try to formulate the l2 distance using matrix multiplication    #
114     #       and two broadcast sums.                                         #
115     #########################################################################
116     tDot = np.multiply(np.dot(X, self.X_train.T), -2)
117     t1 = np.sum(np.square(X), axis=1, keepdims=True)
118     t2 = np.sum(np.square(self.X_train), axis=1)
119     tDot = np.add(t1, tDot)
120     tDot = np.add(tDot, t2)
121     dists = np.sqrt(tDot)
122     #########################################################################
123     #                         END OF YOUR CODE                              #
124     #########################################################################
125     return dists
126
127   def predict_labels(self, dists, k=1):
128     """
129     Given a matrix of distances between test points and training points,
130     predict a label for each test point.
131
132     Input:
133     dists - A num_test x num_train array where dists[i, j] gives the distance
134             between the ith test point and the jth training point.
135
136     Output:
137     y - A vector of length num_test where y[i] is the predicted label for the
138         ith test point.
139     """
140     num_test = dists.shape[0]
141     y_pred = np.zeros(num_test)
142     for i in xrange(num_test):
143       # A list of length k storing the labels of the k nearest neighbors to
144       # the ith test point.
145       closest_y = []
146       #########################################################################
147       # TODO:                                                                 #
148       # Use the distance matrix to find the k nearest neighbors of the ith    #
149       # training point, and use self.y_train to find the labels of these      #
150       # neighbors. Store these labels in closest_y.                           #
151       # Hint: Look up the function numpy.argsort.                             #
152       #########################################################################
153       # pass
154       closest_y = self.y_train[np.argsort(dists[i, :])[:k]]
155       #########################################################################
156       # TODO:                                                                 #
157       # Now that you have found the labels of the k nearest neighbors, you    #
158       # need to find the most common label in the list closest_y of labels.   #
159       # Store this label in y_pred[i]. Break ties by choosing the smaller     #
160       # label.                                                                #
161       #########################################################################
162
163       y_pred[i] = np.argmax(np.bincount(closest_y))
164       #########################################################################
165       #                           END OF YOUR CODE                            #
166       #########################################################################
167
168     return y_pred

输出:

Two loop version took 55.817642 seconds
One loop version took 49.692089 seconds
No loop version took 1.267753 seconds

转载于:https://www.cnblogs.com/JackOne/p/4222320.html

CNN for Visual Recognition (assignment1_Q1)相关推荐

  1. 【CS231n_2017】1-Introduction to CNN for Visual Recognition

      本专栏根据斯坦福大学2017年公开课CS231n的视频教程整理学习资料,做学习笔记.   首先简单介绍CS231n,又称Convolutional Neural Networks for Visu ...

  2. 细粒度论文笔记:双线性模型 《Bilinear CNN Models for Fine-Grained Visual Recognition》

    双线性模型是2015年提出的一种细粒度图像分类模型.该模型使用的是两个并列的CNN模型,这种CNN模型使用的是AlexNet或VGGNet去掉最后的全连接层和softmax层,这个作为特征提取器,然后 ...

  3. [SPP-NET]Spatial Pyramid Pooling in Deep Convolutional Networks for Visual Recognition

    基于空间金字塔池化的卷积神经网络物体检测 原文地址:http://blog.csdn.net/hjimce/article/details/50187655 作者:hjimce 一.相关理论 本篇博文 ...

  4. 目标检测--Spatial pyramid pooling in deep convolutional networks for visual recognition(PAMI, 2015)

    Spatial pyramid pooling in deep convolutional networks for visual recognition 作者: Kaiming He, Xiangy ...

  5. Convolutional Neural Networks for Visual Recognition 1

    Introduction 这是斯坦福计算机视觉大牛李菲菲最新开设的一门关于deep learning在计算机视觉领域的相关应用的课程.这个课程重点介绍了deep learning里的一种比较流行的模型 ...

  6. 论文笔记:Bag of Tricks for Long-Tailed Visual Recognition with Deep Convolutional Neural Networks

    论文地址:http://www.lamda.nju.edu.cn/zhangys/papers/AAAI_tricks.pdf 代码地址:https://github.com/zhangyongshu ...

  7. 【读点论文】Conformer: Local Features Coupling Global Representations for Visual Recognition卷积提取局部,SA获取全局

    Conformer: Local Features Coupling Global Representations for Visual Recognition Abstract 在卷积神经网络(CN ...

  8. 《Long-term Recurrent Convolutional Networks for Visual Recognition and Description》论文翻译

    <Long-term Recurrent Convolutional Networks for Visual Recognition and Description>论文翻译 原文链接: ...

  9. VOLO: Vision Outlooker for Visual Recognition

    论文名称:VOLO: Vision Outlooker for Visual Recognition 作者:Li Yuan, Qibin Hou, Zihang Jiang, Jiashi Feng, ...

最新文章

  1. CVPR2020:基于自适应采样的非局部神经网络鲁棒点云处理(PointASNL)
  2. Flutter ListView封装,下拉刷新、上拉加载更多
  3. 解决htmlfile: 未知的运行时错误
  4. 【转】c#数字图像处理(一)Bitmap类、 Bitmapdata类和 Graphics类
  5. 深入解读Flink资源管理机制
  6. Python 列表推导式 - Python零基础入门教程
  7. javascript Nested functions
  8. 《游戏脚本高级编程》
  9. Threejs实现天空盒,全景场景,地面草地
  10. 中国移动飞信的研究 笔记二
  11. css 文字不规则排版,DIV CSS解决不规则文字排版
  12. 计算机if函数的作用,if函数的使用方法
  13. Windows下如何对声卡音频输出进行录音
  14. 【R语言】【可视化】 之 维恩图
  15. php面试常考函数,PHP面试常见算法、函数总结
  16. CTF-Crypto 密码原理及解密方法
  17. 美图秀秀自动化测试工程师笔试面试
  18. 未来可期与君远航--2021年终总结
  19. 2022年资料员-岗位技能(资料员)操作证考试题模拟考试平台操作
  20. 初识C语言#define、指针、结构体

热门文章

  1. html5与css3都要学吗,前端要学css3吗?
  2. yum mysql5.7位置_CentOS yum 安装 Mysql5.7
  3. python新建一个文件夹需要重新安装模块吗_解决pycharm每次新建项目都要重新安装一些第三方库的问题...
  4. linux ls 命令排序,如何在Linux中使用ls命令按大小对所有文件进行排序
  5. go mysql recover_golang用panic和recover做业务流程中断的尝试
  6. 后台系统可扩展性学习笔记(十四)异步机制与MQ
  7. Linux系统上的程序调优思路概要
  8. 抛硬币正面期望_如果抛硬币,正面的数量多于反面的可能性
  9. php中in array循环,在php中in_array的使用方法
  10. python3爬虫学习笔记