svm

作者: withism | 来源:发表于2017-12-10 23:35 被阅读31次

    import numpy as np

    from random import shuffle

    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 = np.zeros(W.shape) # initialize the gradient as zero

    # compute the loss and the gradient

    num_classes = W.shape[1]

    num_train = X.shape[0]

    loss = 0.0

    for i in xrange(num_train):

    scores = X[i].dot(W)#X[i]=[2,5,4,8,9,9,4,5,5], W=D*C, Scores=[c1 c2 c3 c4 c5 c6 ...cc]

    correct_class_score = scores[y[i]]#correct_class_score=  >  scores[y[i]]=>scores[y[1]]=scores[6]=c6

    for j in xrange(num_classes):#num_classes=C  j=1,2,3,4,5,6,...,c

    if j == y[i]:#if 1 == y[1]=6, go------

    continue

    margin = scores[j] - correct_class_score + 1 # note delta = 1

    if margin > 0:

    loss += margin

    dW[:,j] += X[i].T

    dW[:,y[i]] += -X[i].T

    # 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 += 0.5 * reg * np.sum(W * W)

    dW += reg * W

    #############################################################################

    # TODO:                                                                    #

    # Compute the gradient of the loss function and store it dW.                #

    # Rather that 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.                                      #

    #############################################################################

    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.

    """

    loss = 0.0

    dW = np.zeros(W.shape) # initialize the gradient as zero

    #############################################################################

    # TODO:                                                                    #

    # Implement a vectorized version of the structured SVM loss, storing the    #

    # result in loss.                                                          #

    #############################################################################

    num_train = X.shape[0]

    num_classes = W.shape[1]

    scores = X.dot(W)

    correct_class_scores = scores[range(num_train), list(y)].reshape(-1,1) #(N, 1)

    margins = np.maximum(0, scores - correct_class_scores +1)

    margins[range(num_train), list(y)] = 0

    loss = np.sum(margins) / num_train + 0.5 * reg * np.sum(W * W)

    #pass

    #############################################################################

    #                            END OF YOUR CODE                              #

    #############################################################################

    #############################################################################

    # 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.                                                                    #

    #############################################################################

    coeff_mat = np.zeros((num_train, num_classes))

    coeff_mat[margins > 0] = 1

    coeff_mat[range(num_train), list(y)] = 0

    coeff_mat[range(num_train), list(y)] = -np.sum(coeff_mat, axis=1)

    dW = (X.T).dot(coeff_mat)

    dW = dW/num_train + reg*W

    #pass

    #############################################################################

    #                            END OF YOUR CODE                              #

    #############################################################################

    return loss, dW

    相关文章

      网友评论

          本文标题:svm

          本文链接:https://www.haomeiwen.com/subject/rdsbixtx.html