Assignment1_SVM

本文详细介绍了多类别SVM损失函数的计算方法及梯度推导过程,并提供了向量化实现方式。通过实例说明了如何利用正则化项防止过拟合,以及如何计算损失值和权重梯度。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

The Multiclass SVM loss for the i-th example is then formalized as follows:

    

The most common regularization penalty is the L2 norm that discourages large weights through an elementwise quadratic penalty over all parameters:

    

The full Multiclass SVM loss becomes:

    

Gradient:

    

    

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

  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
  """

    loss = 0.0
    dw = np.zeros(W.shape)
    num_train = X.shape[0]       # num_train : N
    scores = X.dot(W)       # scores.shape = (N,C)
    scores_correct = scores[np.arange(num_train),y]     # 1 by N
    scores_correct = np.reshape(scores_correct,(-1,1))   # N by 1
    margins = scores - scores_correct + 1.0  # delta = 1.0
    margins[np.arange(num_train),y] = 0     # j != yi
    margins[margins < 0] = 0     #max( 0 , s_j - s_yi + delta)
    regular_loss = reg * np.sum(W**2)  
    loss = 1/num_train * np.sum(margins) + regular_loss

    margins[margins>0] = 1.0
    row_sum = margins.sum(axis = 1)  # 1 by N , sum in row
    margins[np.arange(num_train),y] = -row_sum
    dw = (X.T).dot(margins)/num_train + regular_loss    #D by C  : D*N * N*C 
    return loss,dw

 

reference : http://cs231n.github.io/optimization-1/

转载于:https://www.cnblogs.com/zhangli-ncu/p/7612780.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值