美文网首页
12.支持向量机SVM(Support Vector Machi

12.支持向量机SVM(Support Vector Machi

作者: justinwei | 来源:发表于2019-03-26 14:33 被阅读0次

第七周 支持向量机SVM - Lecture 12

  1. 逻辑回归(logistic regression)的优化:
    逻辑回归代价函数(Cost Function)
    -(y\ log\ h_\theta(x) + (1-y)log(1-h_\theta(x)))
    =-y\ log\ \frac{1}{1+e^{-\theta^Tx}} - (1-y) log(1- \frac{1}{1+e^{-\theta^Tx}})
    Alternative view of logistic regression
  2. 支持向量机SVM
  • 逻辑回归(Logistic regression)
    \substack{min \\ \theta} \frac1m[\sum_{i=1}^my^{(i)}(- log\ h_\theta(x^{(i)}) + (1-y^{(i)})(-log(1-h_\theta(x^{(i)})))] + \frac{\lambda}{2m}\sum_{j=1}^n\theta_j^2
    其中:h_{\theta}(x) = \frac{1}{1+e^{-\theta^Tx}}
  • 支持向量机(Support vector machine)
    \substack{min \\ \theta} C\sum_{i=1}^m[y^{(i)}cost_1(\theta^Tx^{(i)}) + (1-y^{(i)})cost_0(\theta^Tx^{(i)}) ] + \frac{1}{2}\sum_{j=1}^n\theta_j^2
    image.png
    if y = 1, we want \theta^Tx \geq 1(not\ just \geq 0) 逻辑回归是>0
    if y = 0, we want \theta^Tx \leq -1(not\ just <0)逻辑回归是<0
    决策边界(Decision Boundary)
    \sum_{i=1}^m[y^{(i)}cost_1(\theta^Tx^{(i)}) + (1-y^{(i)})cost_0(\theta^Tx^{(i)}) ] = 0
    whenever y^{(i)}=1:
    \theta^Tx^{(i)} \geq 1
    whenever y^{(i)}=0:
    \theta^Tx^{(i)} \leq -1
    Large margin classifier
    Large margin classifier
  1. SVM with kernels
    给定一组训练集:(x^{(1)},y^{(1)}),(x^{(2)},y^{(2)}),...,(x^{(m)},y^{(m)})
    选择:l^{(1)}=x^{(1)},l^{(2)}=x^{(2)},...,l^{(m)}=x^{(m)}
    根据给定的训练集x,计算f \in R^{m+1}
    f_1 = similarity(x, l^{(1)})
    f_2 = similarity(x, l^{(2)})
    ...
    f_m = similarity(x, l^{(m)})

f = [f_1, f_2, ... f_m]是一个m+1的向量;

算法描述:给定一个训练集x, 计算出f特征向量 f \in R^{m+1}
如果 \theta^T f \geq 0 预测"y=1"
训练以下:
\substack{min \\ \theta} C\sum_{i=1}^m[y^{(i)}cost_1(\theta^Tf^{(i)}) + (1-y^{(i)})cost_0(\theta^Tf^{(i)}) ] + \frac{1}{2}\sum_{j=1}^n\theta_j^2

5.支持向量机的参数(SVM parameters):

  • C(=\frac1{\lambda}).
    -- 更大的C: 低偏差(lower bias), 过拟合(hight variance).
    -- 更小的C: 高偏差(Higher bias),欠拟合 (low variance).
    -\sigma^2
    -- 更大的 \sigma^2: f_i 更平滑(vary more smoothly). 高偏差(Higher bias),欠拟合 (low variance)..
    -- 更小的 \sigma^2:f_i 更陡峭(vary less smoothly). 低偏差(lower bias), 过拟合(hight variance).
    如下图所示:
    image.png
  1. 如何使用支持向量机(Using an SVM)
    不需要要向前面说的自己实现算法,已经有成熟的软件包(例如liblinear, libsvm, ...)直接计算出 \theta.

软件包需要确定以下参数:

  • 选择参数C.
  • 选择核函数(Choice of kernel(similarity function))
    例如:无核函数l("linear kernel")
    \theta^Tx \geq 0时,预测"y=1"
    高斯核函数(Gaussian kernel):
    f_i=exp(-\frac{||x-l^{(i)}||^2}{2\sigma^2}), where \ l^{(i)}=x^{(i)}
    Need to choose \sigma^2

核函数Kernel(simliarity) functions:
function\ f = kernel(x1, x2)
f=exp(-\frac{||x1-x2||^2}{2\sigma^2})
return

Kernel(similarity) functions:
function f = kernel(x1, x2)
f = exp(-(||x1-x2||^2/(2*sigma^2))
return 
\\ Do perform feature scaling before using the Gaussian kernel
\\使用 Gaussian kernel前必须归一化
  1. 逻辑回归vs.支持向量机(Logistic regression vs. SVMs)
    n为特性个数(x \in R^{n+1}),m为训练集的数量
    n = number of features(x \in R^{n+1}) m = number of training examples
  • 如果n相对m来说很大: (例如. n>m . n = 10,000 m=10...,000)
    使用逻辑回归或支持无核的向量机
    Use logistic regression, or SVM without a kernel("linear kernel")
  • 如果n很小,m中等: (例如 n = 10,000 m=10-10,000)
    使用 SVM with Gaussian kernel
  • 如果n很小,m很大: (例如. n = 1,000 m=50,000)
    创建新的特性,再使用逻辑回归
    Create/add more features, then use logistic regression or SVM without a kernel
  • Neural network likely to work well for most of these settings, but may be slower to train.

相关文章

网友评论

      本文标题:12.支持向量机SVM(Support Vector Machi

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