博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
linear_classifier.py
阅读量:6093 次
发布时间:2019-06-20

本文共 6119 字,大约阅读时间需要 20 分钟。

1 import numpy as np  2 from cs231n.classifiers.linear_svm import *  3 from cs231n.classifiers.softmax import *  4   5 class LinearClassifier(object):  6   7   def __init__(self):  8     self.W = None  9  10   def train(self, X, y, learning_rate=1e-3, reg=1e-5, num_iters=100, 11             batch_size=200, verbose=False): 12     """ 13     Train this linear classifier using stochastic gradient descent. 14  15     Inputs: 16     - X: A numpy array of shape (N, D) containing training data; there are N 17       training samples each of dimension D. 18     - y: A numpy array of shape (N,) containing training labels; y[i] = c 19       means that X[i] has label 0 <= c < C for C classes. 20     - learning_rate: (float) learning rate for optimization. 21     - reg: (float) regularization strength. 22     - num_iters: (integer) number of steps to take when optimizing 23     - batch_size: (integer) number of training examples to use at each step. 24     - verbose: (boolean) If true, print progress during optimization. 25  26     Outputs: 27     A list containing the value of the loss function at each training iteration. 28     """ 29     num_train, dim = X.shape 30     num_classes = np.max(y) + 1 # assume y takes values 0...K-1 where K is number of classes 31     if self.W is None: 32       # lazily initialize W 33       self.W = 0.001 * np.random.randn(dim, num_classes) 34  35     # Run stochastic gradient descent to optimize W 36     loss_history = [] 37     for it in xrange(num_iters): 38       X_batch = None 39       y_batch = None 40  41       ######################################################################### 42       # TODO:                                                                 # 43       # Sample batch_size elements from the training data and their           # 44       # corresponding labels to use in this round of gradient descent.        # 45       # Store the data in X_batch and their corresponding labels in           # 46       # y_batch; after sampling X_batch should have shape (dim, batch_size)   # 47       # and y_batch should have shape (batch_size,)                           # 48       #                                                                       # 49       # Hint: Use np.random.choice to generate indices. Sampling with         # 50       # replacement is faster than sampling without replacement.              # 51       ######################################################################### 52       # num_train = 49000 batch_size = 200  53       mask = np.random.choice(num_train, batch_size, replace=False) 54       X_batch = X[mask] 55       y_batch = y[mask]   56       ######################################################################### 57       #                       END OF YOUR CODE                                # 58       ######################################################################### 59  60       # evaluate loss and gradient 61       loss, grad = self.loss(X_batch, y_batch, reg) 62       loss_history.append(loss) 63  64       # perform parameter update 65       ######################################################################### 66       # TODO:                                                                 # 67       # Update the weights using the gradient and the learning rate.          # 68       ######################################################################### 69       self.W = self.W - learning_rate * grad 70       ######################################################################### 71       #                       END OF YOUR CODE                                # 72       ######################################################################### 73  74       if verbose and it % 100 == 0: 75         print 'iteration %d / %d: loss %f' % (it, num_iters, loss) 76  77     return loss_history 78  79   def predict(self, X): 80     """ 81     Use the trained weights of this linear classifier to predict labels for 82     data points. 83  84     Inputs: 85     - X: D x N array of training data. Each column is a D-dimensional point. 86  87     Returns: 88     - y_pred: Predicted labels for the data in X. y_pred is a 1-dimensional 89       array of length N, and each element is an integer giving the predicted 90       class. 91     """ 92     y_pred = np.zeros(X.shape[1]) 93     ########################################################################### 94     # TODO:                                                                   # 95     # Implement this method. Store the predicted labels in y_pred.            # 96     ########################################################################### 97     #49000*3073  *  3073 * 10 98     y_pred = np.argmax(np.dot(X,self.W), axis=1)   99     ###########################################################################100     #                           END OF YOUR CODE                              #101     ###########################################################################102     return y_pred103   104   def loss(self, X_batch, y_batch, reg):105     """106     Compute the loss function and its derivative. 107     Subclasses will override this.108 109     Inputs:110     - X_batch: A numpy array of shape (N, D) containing a minibatch of N111       data points; each point has dimension D.112     - y_batch: A numpy array of shape (N,) containing labels for the minibatch.113     - reg: (float) regularization strength.114 115     Returns: A tuple containing:116     - loss as a single float117     - gradient with respect to self.W; an array of the same shape as W118     """119     pass120 121 122 class LinearSVM(LinearClassifier):123   """ A subclass that uses the Multiclass SVM loss function """124 125   def loss(self, X_batch, y_batch, reg):126     return svm_loss_vectorized(self.W, X_batch, y_batch, reg)127 128 129 class Softmax(LinearClassifier):130   """ A subclass that uses the Softmax + Cross-entropy loss function """131 132   def loss(self, X_batch, y_batch, reg):133     return softmax_loss_vectorized(self.W, X_batch, y_batch, reg)

 

转载于:https://www.cnblogs.com/wangxiu/p/5673717.html

你可能感兴趣的文章
程序员最喜爱的12个Android应用开发框架二(转)
查看>>
vim学习与理解
查看>>
DIRECTSHOW在VS2005中PVOID64问题和配置问题
查看>>
MapReduce的模式,算法以及用例
查看>>
《Advanced Linux Programming》读书笔记(1)
查看>>
zabbix agent item
查看>>
一步一步学习SignalR进行实时通信_7_非代理
查看>>
字符设备与块设备的区别
查看>>
为什么我弃用GNOME转向KDE(2)
查看>>
Redis学习记录初篇
查看>>
爬虫案例若干-爬取CSDN博文,糗事百科段子以及淘宝的图片
查看>>
Web实时通信技术
查看>>
第三章 计算机及服务器硬件组成结合企业运维场景 总结
查看>>
IntelliJ IDEA解决Tomcal启动报错
查看>>
默认虚拟主机设置
查看>>
php中的短标签 太坑人了
查看>>
[译] 可维护的 ETL:使管道更容易支持和扩展的技巧
查看>>
### 继承 ###
查看>>
数组扩展方法之求和
查看>>
astah-professional-7_2_0安装
查看>>