cs231n笔记(数据驱动与最近邻算法)

本文介绍了如何通过数据驱动方法训练和评估图像分类器,重点讲解了最近邻算法的工作原理,包括其训练和测试阶段的特点,以及如何利用L1距离进行图片相似性判断。同时探讨了最近邻算法的测试效率问题,并提供了一个Python实现示例。
摘要由CSDN通过智能技术生成

数据驱动方法(data-drive approach)

  1. Collect a dataset of images and labels
  2. Use dataset to train a classier
  3. Evaluate the classifier on new images
def train(images, labels):
    #使用图片和标签去训练模型
    model = []
    return model

def predict(model, test_images):
	#使用模型去预测新的图片
    test_labels = []
    return test_labels

最近邻算法(Nearest Neighbor):(用于图片分类)
during train:
模型记忆所有的图片和标签
during test:
用新的图片在训练集中找最相似(距离最小的图片),然后由此得出新图片的标签(predict the label of the most similar training image)

比较图片的相似性是用L1距离(或曼哈顿距离)表示:
像素间绝对值的总和
L1在这里插入图片描述
缺点:训练过程快,而测试过程慢(但我们想要的是:训练过程慢,测试过程快)

import numpy as np

class NearestNeighbor:
  def __init__(self):
    pass
  
  def train(self, X, y):
    ‘‘‘ X is N x D where each row is an example. Yis 1-d of size N ’’’
    # the nearest neighbor classifier simply remebers all the tarining data
    self.Xtr = X
    self.ytr = y
    
  def predict(self X):
    ’‘’ X is N x D where each row is an example wish to predict label for ‘’‘
    num_test = X.shape[0]
    # lets make sure that the output type matches the input type
    Ypred = np.zeros(num_test, dtype = self.ytr.dtype)
    
    # loop over all test rows
    for i in xrange(num_test):
      # find the nearest training image to the i*th test image
      # using the L1 distance (sum of absolute value differences)
      distances = np.sum(np.abs(self.Xtr - X[i,:]), axis = 1) # using broadcast
      min_index = np.argmin(distances) # get the index with smallest distance
      Ypred[i] = self.ytr[min_index] # predict the label of the nearest example
      
    return Ypred

代码部分转载自:https://github.com/zhuole1025/cs231n/blob/master/CS231n-notes.md

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值