cs231n assignment1 svm作业

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 range(num_train):
    scores = X[i].dot(W)
    correct_class_score = scores[y[i]]
    for j in range(num_classes):
      if j == y[i]:
        continue
      margin = scores[j] - correct_class_score + 1 
      if margin > 0:
        loss += margin
  ##############
        dW[:, y[i]] += -X[i, :]    
        dW[:, j] += X[i, :]  
  ############## 
  # 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)
  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  
  num_train= X.shape[0]   #500
  dW = np.zeros(W.shape) # initialize the gradient as zero  
  scores = np.dot(X,W)      # N C   (500,10)
  correct_class_scores = scores[np.arange(num_train),y]  
  #   print(correct_class_scores.shape)   #   (500,)

  correct_class_scores = np.reshape(correct_class_scores,(num_train,-1))  
  #   print(correct_class_scores.shape)   #   (500,1)
  margin = scores-correct_class_scores+1.0  

  #   print(margin.shape)                 #   (500,10)
  margin[np.arange(num_train),y]=0.0  
  margin[margin<=0]=0.0                     
  loss += np.sum(margin)/num_train  
  loss += 0.5*reg*np.sum(W*W)  
  
  margin[margin>0]=1.0                  
  row_sum = np.sum(margin,axis=1)        
  margin[np.arange(num_train),y] = -row_sum  
  dW = 1.0/num_train*np.dot(X.T,margin) + reg*W  



  return loss, dW

 

你可能感兴趣的:(cs231n assignment1 svm作业)