【CS231n】SVM浅谈 + SVM代码实现

文章中谈论的SVM是Multiclass Support Vector MachineMulticlass Support Vector Loss The SVM loss is set up so that the SVM “wants” the correct class for each image to a have a score higher than the inc...
摘要由CSDN通过智能技术生成

文章中谈论的SVM是 Multiclass Support Vector Machine

Multiclass Support Vector Loss

     The SVM loss is set up so that the SVM “wants” the correct class for each image to a have a score higher than the incorrect classes by some fixed margin Δ. Notice that it’s sometimes helpful to anthropomorphise the loss functions as we did above: The SVM “wants” a certain outcome in the sense that the outcome would yield a lower loss (which is good).

   也就是说:SVM loss 是为了SVM对于分类的image在正确类别上面的得分要比不正确的类别得分高出margin Δ。

  公式1:    L_i = \sum_{j \neq y_i}max(0, s_j - s_{y_i} + \Delta)

 L_i:第 i 个样本的loss

s_j:在第 j 个类别上的得分

s_{y_i}:在正确类别上的得分

从公式中,我们中可以看出image在正确类别上的得分比不正确的类别的得分多Δ,那么当前不正确类别并不会对loss产生任何贡献。

公式2:    L_i = \sum_{j \neq y_i}max(0, w_j^{T}*x - w_{y_i}^T*x + \Delta)

Calculus

SVM Loss Fuction:      L_i = \sum_{j \neq y_i}max(0, w_j^{T}*x_i - w_{y_i}^T*x_i + \Delta)

 

\triangledown _{w_{y_i}}的梯度:   \triangledown _{w_{y_i}} L_i = -(\sum _{j \neq y_i} 1 (w_j^T*x_i-w_{y_i}^T*x_i+\Delta) > 0))x_i

 

\triangledown _{w_j}的梯度:   \triangledown _{w_j}L_i = 1(w_j^T*x_i-w_{y_i}^T*x_i+\Delta > 0)x_i

 

SVM代码

github:https://github.com/GIGpanda/CS231n

主要包括两个.py文件,svm.py和linear_svm.py

svm.py

数据加载、可视化

# Multiclass Support Vector Machine

from __future__ import print_function
import random
import numpy as np
from cs231n.data_utils import load_CIFAR10
import matplotlib.pyplot as plt
from cs231n.classifiers.linear_svm import svm_loss_naive
import time
from cs231n.gradient_check import grad_check_sparse
from cs231n.classifiers.linear_svm import svm_loss_vectorized
from cs231n.classifiers import LinearSVM
import math

plt.rcParams['figure.figsize'] = (10.0, 8.0) # set default size of plots
plt.rcParams['image.interpolation'] = 'nearest'
plt.rcParams['image.cmap'] = 'gray'

# Load the raw CIFAR-10 data.
cifar10_dir = 'cs231n/datasets/cifar-10-batches-py'

# Cleaning up variables to prevent loading data multiple times (which may cause memory issue)
try:
   del X_train, y_train
   del X_test, y_test
   print('Clear previously loaded data.')
except:
   pass

X_train, y_train, X_test, y_test = load_CIFAR10(cifar10_dir)

# As a sanity check, we print out the size of the training and test data.
print('Training data shape: ', X_train.shape)
print('Training labels shape: ', y_train.shape)
print('Test data shape: ', X_test.shape)
print('Test labels shape: ', y_test.shape)

# Visualize some examples from the dataset.
# We show a few examples of
  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值