cs231n作业1——SVM

参考文章:cs231n assignment1——SVM

SVM

训练阶段,我们的目的是为了得到合适的 𝑊 和 𝑏 ,为实现这一目的,我们需要引进损失函数,然后再通过梯度下降来训练模型。
在这里插入图片描述
在这里插入图片描述

def svm_loss_naive(W, X, y, reg):  
    #梯度矩阵初始化
    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):
        #W*Xi
        score = X[i].dot(W)
        correct_score = score[y[i]]
        for j in range(num_classes):
            #预测正确
            if j == y[i]:
                continue
            #W*Xi-Wyi*Xi+1
            margin = score[j] - correct_score + 1  # 拉格朗日
            if margin > 0:
                loss += margin
    #平均损失
    loss /= num_train
    #加上正则化λ||W||²
    # Add regularization to the loss.
    loss += reg * np.sum(W * W)		
    dW /= num_train
    dW += reg * W	
    
    return loss, dW

向量形式计算损失函数
在这里插入图片描述

def svm_loss_vectorized(W, X, y, reg):
     loss = 0.0
     dW = np.zeros(W.shape)
     num_train=X.shape[0]
     classes_num=X.shape[1]
     score = X.dot(W)
     #矩阵大小变化,大小不同的矩阵不可以加减
     correct_scores = score[range(num_train), list(y)].reshape(-1, 1) #[N, 1]
     margin = np.maximum(0, score - correct_scores + 1)
     margin[range(num_train), list(y)] = 0
     #正则化
     loss = np.sum(margin) / num_train
     loss += 0.5 * reg * np.sum(W * W)
      #大于0的置1,其余为0
     margin[margin>0] = 1
     margin[range(num_train),list(y)] = 0
     
     margin[range(num_train),y] -= np.sum(margin,1)
     
     dW=X.T.dot(margin)
     
     dW=dW/num_train
     dW=dW+reg*W
     
     return loss, dW

SGD优化损失函数
使用批量随机梯度下降法来更新参数,每次随机选取batchsize个样本用于更新参数 𝑊 和 𝑏 。

 for it in range(num_iters):
    X_batch = None
    y_batch = No
    idxs = np.random.choice(num_train, batch_size, replace=True)
    X_batch = X[idxs]
    y_batch = y[idx
    loss, grad = self.loss(X_batch, y_batch, reg)
    loss_history.append(los
    self.W -= learning_rate * gr
    if verbose and it % 100 == 0:
        print("iteration %d / %d: loss %f" % (it, num_iters, loss))

    return loss_history

交叉验证调整超参数
为了获取最优的超参数,我们可以将整个训练集划分为训练集和验证集,然后选取在验证集上准确率最高的一组超参数。

相关推荐

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-11 15:06:01       52 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-11 15:06:01       54 阅读
  3. 在Django里面运行非项目文件

    2024-07-11 15:06:01       45 阅读
  4. Python语言-面向对象

    2024-07-11 15:06:01       55 阅读

热门阅读

  1. 24/07/10数据结构(4.1209)单链表OJ

    2024-07-11 15:06:01       20 阅读
  2. python批量读取Excel数据写入word

    2024-07-11 15:06:01       22 阅读
  3. 富格林:正确击破暗箱稳健出金

    2024-07-11 15:06:01       16 阅读
  4. 云原生监控-Kubernetes-Promethues-Grafana

    2024-07-11 15:06:01       12 阅读
  5. cmake

    2024-07-11 15:06:01       18 阅读
  6. leetcode300:最长递增子序列

    2024-07-11 15:06:01       19 阅读
  7. [Linux][Shell][Shell数学运算]详细讲解

    2024-07-11 15:06:01       19 阅读