当前位置:   article > 正文

cs231n笔记(数据驱动与最近邻算法)_距离最小化数据驱动本构模型

距离最小化数据驱动本构模型

数据驱动方法(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
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
'
运行

最近邻算法(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
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27

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

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/你好赵伟/article/detail/962350
推荐阅读
相关标签
  

闽ICP备14008679号