cs231n svm代码解析

from cs231n.classifiers.linear_svm import svm_loss_naive
import time

# generate a random SVM weight matrix of small numbers
W = np.random.randn(3073, 10) * 0.0001 
#W生成了一个随机的W权重矩阵是 3073 X 10的矩阵
#X_dev是一个500 X 3073的输入向量
#y_dev是500X1的答案
loss, grad = svm_loss_naive(W, X_dev, y_dev, 0.000005)
print('loss: %f' % (loss, ))
def svm_loss_naive(W, X, y, reg):
    """
    Structured SVM loss function, naive implementation (with loops).

    Inputs have dimension D, there are C classes, and we operate on minibatches
    of N examples.

    Inputs:
    - W: A numpy array of shape (D, C) containing weights.
    - X: A numpy array of shape (N, D) containing a minibatch of data.
    - y: A numpy array of shape (N,) containing training labels; y[i] = c means
      that X[i] has label c, where 0 <= c < C.
    - reg: (float) regularization strength

    Returns a tuple of:
    - loss as single float
    - gradient with respect to weights W; an array of same shape as W
    """
    #dW是3073X10的0矩阵
    dW = np.zeros(W.shape) # initialize the gradient as zero

    # compute the loss and the gradient
    num_classes = W.shape[1] #10
    num_train = X.shape[0] #500
    loss = 0.0
    for i in range(num_train):
      scores = X[i].dot(W)
      #用第i张图片的向量点乘矩阵W得到权重就是分数,y=xw,这个y是预测的分数向量,不用b了,开始通过换元转换为y=xw了
      correct_class_score = scores[y[i]]
      #y是标签,可以得到正确得分的标签
      for j in range(num_classes):
        if j == y[i]:
          continue
        margin = scores[j] - correct_class_score + 1 # note delta = 1
        #Li=max(0,w0*wi-w1*xi+1)+max(0,w2*xi-w1*xi+1)
        #S0-S1  S2-S1
        #对于正确的标签类求导 dLi/dwi = -(xi+xi+...+xi)所以对应 dW[:, y[i]] += -X[i]
		#对于不是正确的标签类求导 dLi/dwj(j!=i) = xi 对应  dW[:, j] += X[i]
        if margin > 0: #margin是指示函数成立的时候求导不是等于0
          dW[:, y[i]] += -X[i]
          dW[:, j] += X[i]
          loss += margin
	#上述一系列操作实际就是用的svm损失函数公式 
	#Li = 求和(max(0,预测的-正确的+1))
    # Right now the loss is a sum over all training examples, but we want it
    # to be an average instead so we divide by num_train.
    loss /= num_train
    dW /= num_train

    # Add regularization to the loss.
    loss += reg * np.sum(W * W)
	#正则化
    #############################################################################
    # TODO:                                                                     #
    # Compute the gradient of the loss function and store it dW.                #
    # Rather than first computing the loss and then computing the derivative,   #
    # it may be simpler to compute the derivative at the same time that the     #
    # loss is being computed. As a result you may need to modify some of the    #
    # code above to compute the gradient.                                       #
    #############################################################################
    # *****START OF YOUR CODE (DO NOT DELETE/MODIFY THIS LINE)*****
	# 正则化项 aW^2 求导2aW
    dW += 2*reg*W

    # *****END OF YOUR CODE (DO NOT DELETE/MODIFY THIS LINE)*****
    
    return loss, dW

下面是向量形式

def svm_loss_vectorized(W, X, y, reg):
    """
    Structured SVM loss function, vectorized implementation.

    Inputs and outputs are the same as svm_loss_naive.
    """
    #W生成了一个随机的W权重矩阵是 3073 X 10的矩阵
	#X_dev是一个500 X 3073的输入向量
	#y_dev是500X1的答案
    loss = 0.0
    dW = np.zeros(W.shape) # initialize the gradient as zero
	#dW 是 3073 X 10
    #############################################################################
    # TODO:                                                                     #
    # Implement a vectorized version of the structured SVM loss, storing the    #
    # result in loss.                                                           #
    #############################################################################
    # *****START OF YOUR CODE (DO NOT DELETE/MODIFY THIS LINE)*****

    num_train = X.shape[0] #500
    scores = X.dot(W)#500 X 10
    # scores[range(num_train), [y]]
    # 这个是取前num_train行 y是一个ndarry的向量 得到的是500 X 1 的矩阵
    # 这句代码的意思是,前num_train的每一行,第一行获取y的第一个位置的列
    #第二行的y的第二个位置的列,
    #可以推测出位500个测试集正确的得分 .T转置
    #scores - scores[range(num_train), [y]].T + 1
    #通过广播可以出来结果,如果相减后 结果小于0,就置为0 这个就是svm损失函数的计算
    loss_tmp = np.maximum(0, scores - scores[range(num_train), [y]].T + 1)
    loss_tmp[range(scores.shape[0]), y] = 0	
	#     loss_tmp[range(scores.shape[0]), y] = 0
	#     把每个测试集合 正确得分的位置 都变为0  这一点是对的,因为在计算svm损失函数
	#  	  把正确的类别 不参与计算,排除在外的,所以设置为0  这个是正确的。
    loss = np.sum(loss_tmp) / num_train + reg*np.sum(W**2)
    #上面加上了正则化 没什么好说的

    # *****END OF YOUR CODE (DO NOT DELETE/MODIFY THIS LINE)*****

    #############################################################################
    # TODO:                                                                     #
    # Implement a vectorized version of the gradient for the structured SVM     #
    # loss, storing the result in dW.                                           #
    #                                                                           #
    # Hint: Instead of computing the gradient from scratch, it may be easier    #
    # to reuse some of the intermediate values that you used to compute the     #
    # loss.                                                                     #
    #############################################################################
    # *****START OF YOUR CODE (DO NOT DELETE/MODIFY THIS LINE)*****
	#返回一个用1填充的跟输入 形状和类型 一致的数组。 loss_tmp
	#dloss_tmp 是500X10的矩阵 里面每个数据都除以numtrain500之后 都变成0.02填充
    dloss_tmp = np.ones_like(loss_tmp) / num_train
    dscores = np.zeros_like(scores)
    dscores[loss_tmp>0] = 1 #对于一般的 变为1
    dscores[range(num_train), y] = -np.sum(dscores, axis=1)#对于特殊的,先将每一列加起来 然后减去
    dW = X.T.dot(dscores)
    dW += 2*reg*W

    # *****END OF YOUR CODE (DO NOT DELETE/MODIFY THIS LINE)*****

    return loss, dW
```![在这里插入图片描述](https://img-blog.csdnimg.cn/b40baab79f4d4344b4853de2d83e3431.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBAd2VpeGluXzQyNjczNTgz,size_13,color_FFFFFF,t_70,g_se,x_16)

你可能感兴趣的:(支持向量机,机器学习,算法)