ML - hw2

1. A Walk Through Linear Models

(a) Perceptron

(a.i) Part 1: Preceptron & Part 2: Preceptron: Non-linearly separable case

Part 1: Preceptron

training set = 10
E_train is 0.00016666666666666666, E_test is 0.17675
Average number of iterations is 6.124.

In this condition, I set iteration threshold = 100.
As for E_train isn't equal to 0, I think there are 2 reasons:

  1. The generated data forms some Non-linearly separable cases.
  2. The iteration threshold is too small.

I lift the iters threshold to 1000, E_train = 0, the satisfied answer.
E_train is 0.0, E_test is 0.173
Average number of iterations is 5.992.

Part 2: Perceptron: Non-linearly separable case

training set = 100(noisy
At first, the iteration threshold is 1000
E_train is 0.2771714285714284, E_test is 0.28826666666666606
Average number of iterations is 1000.0.

Then, I lift the iters threshold to 10000.
After calculating for a very long time,
E_train is 0.27365714285714265, E_test is 0.2900999999999996
Average number of iterations is 10000.0.

Conclusions

  • Error on train set still exists and the number of average iterations is always the maximum iterations. It means the learning algorithm eventually repeats the same set of weights and enters an infinite loop.
  • So when the dataset is large, we can't just use a two-dimensional line to separate them.

(a.ii)

When the iteration threshold is 1000

  • train set = 10, the average number of iterations is 5.992.
  • train set = 100, the average number of iterations is 1000.

(a.iii)

If the training data is not linearly separable, we can't find a line to separate all the data no matter how many iterations we use. The training error can't be 0.

(b) Linear Regression

(b.i) Part 3: Linear Regression

training set = 100
E_train is 0.037885714285714696, E_test is 0.05416666666666684

(b.ii) Part 4: Linear Regression: noisy

training set = 100(noisy
E_train is 0.13465714285714314, E_test is 0.14853333333333318

Conclusions

  • With noise increase, the E_train and E_test also increase.
  • In the same condition, Linear Regression has less E_train and E_test than Perceptron, which may mean that LR can find more solutions in solution space.

(b.iii) Run Linear Regression on dataset poly_train.mat and poly_test.mat

w = [ -0.02413004, 0.23556134, -0.11510741 ]
E_train is 0.49, E_test is 0.5496

(b.iiii) Do the data transformation

w = [ -0.06872399, -0.02687646, 0.22464875, 0.13160111, -1.84827196, 2.00418171 ]
E_train is 0.05, E_test is 0.066

(c) Logistic Regression

(c.i)

training set = 100
E_train is 0.002428571428571429, E_test is 0.021333333333333343

(c.ii)

training set = 100, test set = 10000(noisy
E_train is 0.1159, E_test is 0.13491999999999998

(d) Support Vector Machine

(d.i)

training set = 30, test set = 10
E_train is 0.001400000000000001, E_test is 0.036600000000000146

(d.ii)

training set = 100, test set = 30
E_train is 8.999999999999999e-05, E_test is 0.009799999999999977

(d.iii)

When nTrain = 100, the average number of support vectors is 3.094.
As I use the following code to define the support vector margin, where 1e-5 is set as a threshold so that it's not minimal margin exactly.

num = np.where(y_ - np.min(y_) < 1e-5)[0].__len__()

(d.iiii)

training set = 30, test set = 10(noisy
E_train is 0.016366666666666637, E_test is 0.46869999999999973
Average number of support vectors is 29.381.

Conclusions

  • When training SVM with noisy data, the classifier becomes overfitting on training data, the optimal hyperplane may be pushed far away from its original/correct place and the margin will be smaller than before, as the picture below.
  • Adding slack variables can ease this problem.

2. Regularization and Cross-Validation

(a) Ridge Regression

(a.i)

  • λ = 100

(a.ii)

  • with regularization, sum(w2) = 0.13289524019362603
  • without regularization, sum(w2) = 1.0204766956977154

(a.iii)

  • with regularization, E_train = 0, E_test = 0.05976896032144651
  • without regularization, E_train = 0, E_test = 0.12606730286288298
with regularization
without regularization

(b) Logistic Regression with Regularization

At first, the learning rate is 0.03

  • λ = 1
  • with regularization
    • E_train = 0, E_test = 0.0562531391260673
    • sum(w2) = 4.046897936351152
  • without regularization
    • E_train = 0, E_test = 0.05725765946760422
    • sum(w2) = 158.64900269669087
with regularization, lr = 0.03
without regularization, lr = 0.03

Then I change the learning rate to 0.01

  • λ = 1
  • with regularization
    • E_train = 0, E_test = 0.050728277247614265
    • sum(w2) = 3.968900599887993
  • without regularization
    • E_train = 0, E_test = 0.048719236564540434
    • sum(w2) = 25.414055343730237
with regularization, lr = 0.01
without regularization, lr = 0.01

Conclusions

  • A suitable learning rate can also affect the model's complexity, in other words, little learning rate may get more little parameters in limited steps.
  • When the learning rate changes from 0.03 to 0.01, the training error and test error also drop down. So sometimes, a good learning rate can generate a good model with less bias and variance.

3. Bias Variance Trade-off

(a)

  1. False. High bias means the model is very simple, the estimated value is far away from the truth. In this condition, we should change the model to a more broad space by adding parameters or other measures. Just adding more training examples cannot solve this problem.
  2. False. High variance means the model is overfitting on the training set, which will result in large test error. But we want to find a model behaving well on test data with good generalization ability.
  3. True. By using more parameters we can create a quite complicated model. It can even have zero-error on the training set, which also results in overfitting problem with high variance.
  4. False. Introducing regularization will control overfitting on the training set, and the model will behave better on the test set. This is the key reason we use regularization. And the performance also depends on the λ we choose. Sometimes, the model doesn't behave quite well on the training set but behave well on the test set.
  5. False. Large λ means more control of parameters. It will make the parameters we learn so small that the eventual boundary is like a simple line, as the picture below. And the training set data will have little influence on the model training process. So the performance of your hypothesis will be severely hurt.
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 216,163评论 6 498
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 92,301评论 3 392
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 162,089评论 0 352
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,093评论 1 292
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,110评论 6 388
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,079评论 1 295
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,005评论 3 417
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 38,840评论 0 273
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,278评论 1 310
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,497评论 2 332
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,667评论 1 348
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,394评论 5 343
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 40,980评论 3 325
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,628评论 0 21
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,796评论 1 268
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 47,649评论 2 368
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,548评论 2 352

推荐阅读更多精彩内容