12. Support Vector Machines

Support Vector Machines

Optimization objective

SVM hypothesis:
logistic regression:

y = \frac{1}{1+e^{-\theta^Tx}}

cost function:

\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_{i=1}^n\theta_j^2

Large Margin Intution

\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_{i=1}^n\theta_j^2

If y=1, we want \theta^Tx\ge1 (not just \ge0)
If y=0, we want \theta^Tx\le-1 (not just \le0)

If C is too large, the deasion boundary will be sensitive by outliers

The mathematics behind large margin classification (optional)

Vector Inner Product

SVM Decision Boundary

\min\limits_\theta\frac{1}{2}\sum\limits_{j=1}^n\theta_j^2=\frac{1}{2}||\theta||^2

Kernels I

Non-liner decision boundary:

Given x, compute new feature feature depending on proximity to landmarks defined manually.

Kernels and Similarity (Gaussian kernel):

f_1=similarity(x,l^{(1)})=\exp (-\frac{||x-l^{(1)}||^2}{2\sigma^2})=\exp(-\frac{\sum_{j=1}^n(x_j-l_j^{(1)})^2}{2\sigma^2})

If x\approx l^{(1)}\qquad f_1\approx 1
If x far from l^{(1)} f_1\approx0


Kernels II

Choosing the land marks:
Where to get l ?
Give (x^{(1)},y^{(i)}),(x^{(2)},y^{(2)}),...(x^{(n)},y^{(n)}
choose l^{(1)}=x^{(1)},l^{(2)}=x^{(2)},...,l^{(n)}=x^{(n)}

For training examples (x^{(i)},y^{(i)})

f_m^{(i)} = sim(x^{(i)},l^{(m)}) f_0 =1

SVM with Kernels

Hypothesis: Given x, compute features f\in R^{m+1}
Predict 'y=1' if \theta^Tf\ge0
Training:\min\limits_\theta C\sum\limits_{i=1}^my^{(i)}cost_1(\theta^Tf^{(i)})+(1-y^{(i)})cost_0(\theta^Tf^{(i)})+\frac{1}{2}\sum\limits_{j=1}^n\theta_j^2\quad (n=m)

Kernels ususally were used with SVM, although it can be used with logistic regressin, it runs slowly.

SVM parameters

C :

  • Large C: Lower bias, high variance.
  • Small C: Higher bias, low variance.

\sigma^2 :

  • Larger \sigma^2: Features f_i vary more smoothly. Higher bias, lower variance.(Underfit)
  • Small \sigma^2: Feaugers f_i vary less smoothly. Lower bias, higher variance. (Overfit)

Using an SVM

Need to specify:

  • Choice of parameter C
  • Choice of kernel (similarity function)

Note: Do perform feature scaling before using the Gaussian kernel.

Other choices of kernel

Not all similarity functions similarity(x,l) make valid kernels. (Need to satisfy technical condition called "Mercer's Theorem") to make sure SVM packages' optimizations run correctly, and do not diverge.

Many off-the-shelf kernels avaliable:

  • Polynomial kernel: k(x,l) = (x^Tl+constant)^degree
  • String kernel
  • chi-square kernel
  • histogram intersection kernel

Multi-class classification

Many SVM packages already have build-in multi-class classification functionality.

Logistic regression vs. SVM

n = number of features, m = number of training examples.

  • If n is large (relative m):
    Use logistc regression, or SVM without a kernel.
  • If n is small m is intermediate:
    Use SVM with Gaussian kernel
  • If n is small, m is large:
    Create/add more features, then use logistic regression or SVM without a kernel.
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末系草,一起剝皮案震驚了整個濱河市蜕该,隨后出現(xiàn)的幾起案子状蜗,更是在濱河造成了極大的恐慌,老刑警劉巖瘩欺,帶你破解...
    沈念sama閱讀 222,590評論 6 517
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異,居然都是意外死亡录别,警方通過查閱死者的電腦和手機耕渴,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 95,157評論 3 399
  • 文/潘曉璐 我一進店門拘悦,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人橱脸,你說我怎么就攤上這事础米。” “怎么了添诉?”我有些...
    開封第一講書人閱讀 169,301評論 0 362
  • 文/不壞的土叔 我叫張陵屁桑,是天一觀的道長。 經(jīng)常有香客問我栏赴,道長蘑斧,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 60,078評論 1 300
  • 正文 為了忘掉前任,我火速辦了婚禮乌叶,結(jié)果婚禮上盆偿,老公的妹妹穿的比我還像新娘。我一直安慰自己准浴,他們只是感情好事扭,可當(dāng)我...
    茶點故事閱讀 69,082評論 6 398
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著乐横,像睡著了一般求橄。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上葡公,一...
    開封第一講書人閱讀 52,682評論 1 312
  • 那天罐农,我揣著相機與錄音,去河邊找鬼催什。 笑死涵亏,一個胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的蒲凶。 我是一名探鬼主播气筋,決...
    沈念sama閱讀 41,155評論 3 422
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼旋圆!你這毒婦竟也來了宠默?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 40,098評論 0 277
  • 序言:老撾萬榮一對情侶失蹤灵巧,失蹤者是張志新(化名)和其女友劉穎搀矫,沒想到半個月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體刻肄,經(jīng)...
    沈念sama閱讀 46,638評論 1 319
  • 正文 獨居荒郊野嶺守林人離奇死亡瓤球,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 38,701評論 3 342
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了肄方。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片冰垄。...
    茶點故事閱讀 40,852評論 1 353
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖权她,靈堂內(nèi)的尸體忽然破棺而出虹茶,到底是詐尸還是另有隱情,我是刑警寧澤隅要,帶...
    沈念sama閱讀 36,520評論 5 351
  • 正文 年R本政府宣布蝴罪,位于F島的核電站,受9級特大地震影響步清,放射性物質(zhì)發(fā)生泄漏要门。R本人自食惡果不足惜虏肾,卻給世界環(huán)境...
    茶點故事閱讀 42,181評論 3 335
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望欢搜。 院中可真熱鬧封豪,春花似錦、人聲如沸炒瘟。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,674評論 0 25
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽疮装。三九已至缘琅,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間廓推,已是汗流浹背刷袍。 一陣腳步聲響...
    開封第一講書人閱讀 33,788評論 1 274
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留樊展,地道東北人呻纹。 一個月前我還...
    沈念sama閱讀 49,279評論 3 379
  • 正文 我出身青樓,卻偏偏與公主長得像滚局,于是被迫代替她去往敵國和親居暖。 傳聞我的和親對象是個殘疾皇子顽频,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 45,851評論 2 361