Docsity

Learning Theory and Unsupervised Learning - Assignment - Machine Learning - 3, Exercises of Machine Learning

Toggle sidebar
bg1
CS229 Problem Set #3 1
CS 229, Public Course
Problem Set #3: Learning Theory and Unsuper-
vised Learning
1. Uniform convergence and Model Selection
In this problem, we will prove a bound on the error of a simple model selection procedure.
Let there be a binary classification problem with labels y∈ {0,1}, and let H1⊆ H2⊆
... ⊆ Hkbe kdifferent finite hypothesis classes (|Hi|<∞). Given a dataset Sof miid
training examples, we will divide it into a training set Strain consisting of the first (1−β)m
examples, and a hold-out cross validation set Scv consisting of the remaining βm examples.
Here, β∈(0,1).
Let ˆ
hi= arg minh∈HiˆεStrain(h) b e the hypothesis in Hiwith the lowest training error
(on Strain). Thus, ˆ
hiwould be the hypothesis returned by training (with empirical risk
minimization) using hypothesis class Hiand dataset Strain. Also let h⋆
i= arg minh∈Hiε(h)
be the hypothesis in Hiwith the lowest generalization error.
Suppose that our algorithm first finds all the ˆ
hi’s using empirical risk minimization then
uses the hold-out cross validation set to select a hypothesis from this the {ˆ
h1,...,ˆ
hk}with
minimum training error. That is, the algorithm will output
ˆ
h= arg min
h∈{ˆ
h1,...,ˆ
hk}
ˆεScv (h).
For this question you will prove the following bound. Let any δ > 0 be fixed. Then with
probability at least 1 −δ, we have that
ε(ˆ
h)≤min
i=1,...,k ε(h∗
i) + s2
(1 −β)mlog 4|Hi|
δ!+s2
2βm log 4k
δ
(a) Prove that with probability at least 1 −δ
2, for all ˆ
hi,
|ε(ˆ
hi)−ˆεScv (ˆ
hi)| ≤ s1
2βm log 4k
δ.
(b) Use part (a) to show that with probability 1 −δ
2,
ε(ˆ
h)≤min
i=1,...,k ε(ˆ
hi) + s2
βm log 4k
δ.
(c) Let j= arg miniε(ˆ
hi). We know from class that for Hj, with probability 1 −δ
2
|ε(ˆ
hj)−ˆεStrain (h⋆
j)| ≤ s2
(1 −β)mlog 4|Hj|
δ,∀hj∈ Hj.
Use this to prove the final bound given at the beginning of this problem.
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download Learning Theory and Unsupervised Learning - Assignment - Machine Learning - 3 and more Exercises Machine Learning in PDF only on Docsity!

CS229 Problem Set #3 1

CS 229, Public Course

Problem Set #3: Learning Theory and Unsuper-

vised Learning

  1. Uniform convergence and Model Selection In this problem, we will prove a bound on the error of a simple model selection procedure. Let there be a binary classification problem with labels y ∈ { 0 , 1 }, and let H 1 ⊆ H 2 ⊆ ... ⊆ Hk be k different finite hypothesis classes (|Hi| < ∞). Given a dataset S of m iid training examples, we will divide it into a training set Strain consisting of the first (1 − β)m examples, and a hold-out cross validation set Scv consisting of the remaining βm examples. Here, β ∈ (0, 1). Let ˆhi = arg minh∈Hi εˆStrain (h) be the hypothesis in Hi with the lowest training error (on Strain). Thus, ˆhi would be the hypothesis returned by training (with empirical risk minimization) using hypothesis class Hi and dataset Strain. Also let h⋆i = arg minh∈Hi ε(h) be the hypothesis in Hi with the lowest generalization error. Suppose that our algorithm first finds all the ˆhi’s using empirical risk minimization then uses the hold-out cross validation set to select a hypothesis from this the {ˆh 1 ,... , ˆhk} with minimum training error. That is, the algorithm will output ˆh = arg min h∈{ˆh 1 ,...,ˆhk }

ε ˆScv (h).

For this question you will prove the following bound. Let any δ > 0 be fixed. Then with probability at least 1 − δ, we have that

ε(ˆh) ≤ min i=1,...,k

ε(h∗ i ) +

(1 − β)m

log

4 |Hi| δ

2 βm

log

4 k δ

(a) Prove that with probability at least 1 − δ 2 , for all ˆhi,

|ε(ˆhi) − εˆScv (ˆhi)| ≤

2 βm

log 4 k δ

(b) Use part (a) to show that with probability 1 − δ 2 ,

ε(ˆh) ≤ min i=1,...,k ε(ˆhi) +

βm log

4 k δ

(c) Let j = arg mini ε(ˆhi). We know from class that for Hj , with probability 1 − δ 2

|ε(ˆhj ) − ˆεStrain (h⋆j )| ≤

(1 − β)m

log

4 |Hj | δ

, ∀hj ∈ Hj.

Use this to prove the final bound given at the beginning of this problem.

CS229 Problem Set #3 2

  1. VC Dimension Let the input domain of a learning problem be X = R. Give the VC dimension for each of the following classes of hypotheses. In each case, if you claim that the VC dimension is d, then you need to show that the hypothesis class can shatter d points, and explain why there are no d + 1 points it can shatter. - h(x) = 1 {a < x}, with parameter a ∈ R. - h(x) = 1 {a < x < b}, with parameters a, b ∈ R. - h(x) = 1 {a sin x > 0 }, with parameter a ∈ R. - h(x) = 1 {sin(x + a) > 0 }, with parameter a ∈ R.
  2. ℓ 1 regularization for least squares In the previous problem set, we looked at the least squares problem where the objective function is augmented with an additional regularization term λ‖θ‖^22. In this problem we’ll consider a similar regularized objective but this time with a penalty on the ℓ 1 norm of the parameters λ‖θ‖ 1 , where ‖θ‖ 1 is defined as

i |θi|. That is, we want to minimize the objective

J(θ) =

∑^ m

i=

(θT^ x(i)^ − y(i))^2 + λ

∑^ n

i=

|θi|.

There has been a great deal of recent interest in ℓ 1 regularization, which, as we will see, has the benefit of outputting sparse solutions (i.e., many components of the resulting θ are equal to zero). The ℓ 1 regularized least squares problem is more difficult than the unregularized or ℓ 2 regularized case, because the ℓ 1 term is not differentiable. However, there have been many efficient algorithms developed for this problem that work very well in practive. One very straightforward approach, which we have already seen in class, is the coordinate descent method. In this problem you’ll derive and implement a coordinate descent algorithm for ℓ 1 regularized least squares, and apply it to test data.

(a) Here we’ll derive the coordinate descent update for a given θi. Given the X and ~y matrices, as defined in the class notes, as well a parameter vector θ, how can we adjust θi so as to minimize the optimization objective? To answer this question, we’ll rewrite the optimization objective above as

J(θ) =

‖Xθ − ~y‖^22 + λ‖θ‖ 1 =

‖X ¯θ + Xiθi − ~y‖^22 + λ‖θ¯‖ 1 + λ|θi|

where Xi ∈ Rm^ denotes the ith column of X, and θ¯ is equal to θ except with θ¯i = 0; all we have done in rewriting the above expression is to make the θi term explicit in the objective. However, this still contains the |θi| term, which is non-differentiable and therefore difficult to optimize. To get around this we make the observation that the sign of θi must either be non-negative or non-positive. But if we knew the sign of θi, then |θi| becomes just a linear term. That, is, we can rewrite the objective as

J(θ) =

‖X ¯θ + Xiθi − ~y‖^22 + λ‖θ¯‖ 1 + λsiθi

where si denotes the sign of θi, si ∈ {− 1 , 1 }. In order to update θi, we can just compute the optimal θi for both possible values of si (making sure that we restrict

CS229 Problem Set #3 4

(a) Prove that the GEM algorithm described above converges. To do this, you should show that the the likelihood is monotonically improving, as it does for the EM algo- rithm — i.e., show that ℓ(θ(t+1)) ≥ ℓ(θ(t)). (b) Instead of using the EM algorithm at all, suppose we just want to apply gradient ascent to maximize the log-likelihood directly. In other words, we are trying to maximize the (non-convex) function

ℓ(θ) =

i

log

z(i)

p(x(i), z(i); θ)

so we could simply use the update

θ := θ + α∇θ

i

log

z(i)

p(x(i), z(i); θ).

Show that this procedure in fact gives the same update as the GEM algorithm de- scribed above.

CS 229, Public Course

Problem Set #3 Solutions: Learning Theory and

Unsupervised Learning

  1. Uniform convergence and Model Selection In this problem, we will prove a bound on the error of a simple model selection procedure. Let there be a binary classification problem with labels y ∈ { 0 , 1 }, and let H 1 ⊆ H 2 ⊆ ... ⊆ Hk be k different finite hypothesis classes (|Hi| < ∞). Given a dataset S of m iid training examples, we will divide it into a training set Strain consisting of the first (1 − β)m examples, and a hold-out cross validation set Scv consisting of the remaining βm examples. Here, β ∈ (0, 1). Let ˆhi = arg minh∈Hi εˆStrain (h) be the hypothesis in Hi with the lowest training error (on Strain). Thus, ˆhi would be the hypothesis returned by training (with empirical risk minimization) using hypothesis class Hi and dataset Strain. Also let h⋆i = arg minh∈Hi ε(h) be the hypothesis in Hi with the lowest generalization error. Suppose that our algorithm first finds all the ˆhi’s using empirical risk minimization then uses the hold-out cross validation set to select a hypothesis from this the {ˆh 1 ,... , ˆhk} with minimum training error. That is, the algorithm will output

ˆh = arg min h∈{ˆh 1 ,...,ˆhk }

ε ˆScv (h).

For this question you will prove the following bound. Let any δ > 0 be fixed. Then with probability at least 1 − δ, we have that

ε(ˆh) ≤ min i=1,...,k

ε(h∗ i ) +

(1 − β)m

log

4 |Hi| δ

2 βm

log

4 k δ

(a) Prove that with probability at least 1 − δ 2 , for all ˆhi,

|ε(ˆhi) − εˆScv (ˆhi)| ≤

2 βm

log

4 k δ

Answer: For each hˆi, the empirical error on the cross-validation set, εˆ(ˆhi) represents the average of βm random variables with mean ε(ˆhi), so by the Hoeffding inequality for any hˆi, P (|ε(ˆhi) − εˆScv (ˆhi)| ≥ γ) ≤ 2 exp(− 2 γ^2 βm).

As in the class notes, to insure that this holds for all ˆhi, we need to take the union over all k of the ˆhi’s.

P (∃i, s.t.|ε(ˆhi) − εˆScv (hˆi)| ≥ γ) ≤ 2 k exp(− 2 γ^2 βm).

  • h(x) = 1 {a < x}, with parameter a ∈ R. Answer: VC-dimension = 1. (a) It can shatter point { 0 }, by choosing a to be 2 and − 2. (b) It cannot shatter any two points {x 1 , x 2 }, x 1 < x 2 , because the labelling x 1 = 1 and x 2 = 0 cannot be realized.
  • h(x) = 1 {a < x < b}, with parameters a, b ∈ R. Answer: VC-dimension = 2. (a) It can shatter points { 0 , 2 } by choosing (a, b) to be (3, 5), (− 1 , 1), (1, 3), (− 1 , 3). (b) It cannot shatter any three points {x 1 , x 2 , x 3 }, x 1 < x 2 < x 3 , because the labelling x 1 = x 3 = 1, x 2 = 0 cannot be realized.
  • h(x) = 1 {a sin x > 0 }, with parameter a ∈ R. Answer: VC-dimension = 1. a controls the amplitude of the sine curve. (a) It can shatter point { π 2 } by choosing a to be 1 and − 1. (b) It cannot shatter any two points {x 1 , x 2 }, since, the labellings of x 1 and x 2 will flip together. If x 1 = x 2 = 1 for some a, then we cannot achieve x 1 6 = x 2. If x 1 6 = x 2 for some a, then we cannot achieve x 1 = x 2 = 1 (x 1 = x 2 = 0 can be achieved by setting a = 0).
  • h(x) = 1 {sin(x + a) > 0 }, with parameter a ∈ R. Answer: VC-dimension = 2. a controls the phase of the sine curve. (a) It can shatter points { π 4 , 34 π }, by choosing a to be 0 , π 2 , π, and 32 π. (b) It cannot shatter any three points {x 1 , x 2 , x 3 }. Since sine has a preiod of 2 π, let’s define x′ i = xi mod 2π. W.l.o.g., assume x′ 1 < x′ 2 < x′ 3. If the labelling of x 1 = x 2 = x 3 = 1 can be realized, then the labelling of x 1 = x 3 = 1, x 2 = 0 will not be realizable. Notice the similarity to the second question.
  1. ℓ 1 regularization for least squares

In the previous problem set, we looked at the least squares problem where the objective function is augmented with an additional regularization term λ‖θ‖^22. In this problem we’ll consider a similar regularized objective but this time with a penalty on the ℓ 1 norm of the parameters λ‖θ‖ 1 , where ‖θ‖ 1 is defined as

i |θi|. That is, we want to minimize the objective

J(θ) =

∑^ m

i=

(θT^ x(i)^ − y(i))^2 + λ

∑^ n

i=

|θi|.

There has been a great deal of recent interest in ℓ 1 regularization, which, as we will see, has the benefit of outputting sparse solutions (i.e., many components of the resulting θ are equal to zero). The ℓ 1 regularized least squares problem is more difficult than the unregularized or ℓ 2 regularized case, because the ℓ 1 term is not differentiable. However, there have been many efficient algorithms developed for this problem that work very well in practive. One very straightforward approach, which we have already seen in class, is the coordinate descent method. In this problem you’ll derive and implement a coordinate descent algorithm for ℓ 1 regularized least squares, and apply it to test data.

(a) Here we’ll derive the coordinate descent update for a given θi. Given the X and ~y matrices, as defined in the class notes, as well a parameter vector θ, how can we adjust θi so as to minimize the optimization objective? To answer this question, we’ll rewrite the optimization objective above as

J(θ) =

‖Xθ − ~y‖^22 + λ‖θ‖ 1 =

‖X ¯θ + Xiθi − ~y‖^22 + λ‖θ¯‖ 1 + λ|θi|

where Xi ∈ Rm^ denotes the ith column of X, and θ¯ is equal to θ except with θ¯i = 0; all we have done in rewriting the above expression is to make the θi term explicit in the objective. However, this still contains the |θi| term, which is non-differentiable and therefore difficult to optimize. To get around this we make the observation that the sign of θi must either be non-negative or non-positive. But if we knew the sign of θi, then |θi| becomes just a linear term. That, is, we can rewrite the objective as

J(θ) =

‖X ¯θ + Xiθi − ~y‖^22 + λ‖θ¯‖ 1 + λsiθi

where si denotes the sign of θi, si ∈ {− 1 , 1 }. In order to update θi, we can just compute the optimal θi for both possible values of si (making sure that we restrict the optimal θi to obey the sign restriction we used to solve for it), then look to see which achieves the best objective value. For each of the possible values of si, compute the resulting optimal value of θi. [Hint: to do this, you can fix si in the above equation, then differentiate with respect to θi to find the best value. Finally, clip θi so that it lies in the allowable range — i.e., for si = 1, you need to clip θi such that θi ≥ 0.] Answer: For si = 1,

J(θ) =

tr(X θ¯ + Xiθi − ~y)T^ (X ¯θ + Xiθi − ~y) + λ‖¯θ‖ 1 + λθi

=

XiT Xiθ^2 i + 2XTi (X ¯θ − ~y)θi + ‖X θ¯ − ~y‖^22

  • λ‖θ¯‖ 1 + λθi,

so ∂J(θ) ∂θi

= XiT Xiθ + XiT (X θ¯ − ~y) + λ

which means the optimal θi is given by

θi = max

−XiT (X θ¯ − ~y) − λ XTi Xi

Similarly, for si = − 1 , the optimal θi is given by

θi = min

−XiT (X θ¯ − ~y) + λ XTi Xi

(b) Implement the above coordinate descent algorithm using the updates you found in the previous part. We have provided a skeleton theta = l1ls(X,y,lambda) function in the q3/ directory. To implement the coordinate descent algorithm, you should repeatedly iterate over all the θi’s, adjusting each as you found above. You can terminate the process when θ changes by less than 10−5 after all n of the updates. Answer: The following is our implementation of l1ls.m:

and k = 4. Plot the cluster assignments and centroids for each iteration of the algorithm using the draw clusters(X, clusters, centroids) function. For each k, be sure to run the algorithm several times using different initial centroids. Answer: The following is our implementation of k means.m:

function [clusters, centroids] = k_means(X, k)

m = size(X,1); n = size(X,2); oldcentroids = zeros(k,n); centroids = X(ceil(rand(k,1)*m),:);

while (norm(oldcentroids - centroids) > 1e-15) oldcentroids = centroids; % compute cluster assignments for i=1:m, dists = sum((repmat(X(i,:), k, 1) - centroids).^2, 2); [min_dist, clusters(i,1)] = min(dists); end

draw_clusters(X, clusters, centroids); pause(0.1);

% compute cluster centroids for i=1:k, centroids(i,:) = mean(X(clusters == i, :)); end end

Below we show the centroid evolution for two typical runs with k = 3. Note that the different starting positions of the clusters lead to do different final clusterings.

−0.8^ −0.6 −1 −0.8 −0.6 −0.4 −0.2 0 0.2 0.4 0.6 0.8 1

−0.4^ −0.

0.2 0 0.4^ 0.

0.8^1

−0.8^ −0.6 −1 −0.8 −0.6 −0.4 −0.2 0 0.2 0.4 0.6 0.8 1

−0.4^ −0.

0.2 0 0.4^ 0.

0.8^1

−0.8^ −0.6 −1 −0.8 −0.6 −0.4 −0.2 0 0.2 0.4 0.6 0.8 1

−0.4^ −0.

0.2 0 0.4^ 0.

0.8^1

−0.8^ −0.6 −1 −0.8 −0.6 −0.4 −0.2 0 0.2 0.4 0.6 0.8 1

−0.4^ −0.

0.2 0 0.4^ 0.

0.8^1

−0.8^ −0.6 −1 −0.8 −0.6 −0.4 −0.2 0 0.2 0.4 0.6 0.8 1

−0.4^ −0.

0.2 0 0.4^ 0.

0.8^1

−0.8^ −0.6 −1 −0.8 −0.6 −0.4 −0.2 0 0.2 0.4 0.6 0.8 1

−0.4^ −0.

0.2 0 0.4^ 0.

0.8^1

−0.8 −1 −0.8 −0.6 −0.4 −0.2 0 0.2 0.4 0.6 0.8 1 −0.6^ −0.

−0.2^0

0.2^ 0.

0.6^ 0.

1

−0.8 −1 −0.8 −0.6 −0.4 −0.2 0 0.2 0.4 0.6 0.8 1 −0.6^ −0.

−0.2^0

0.2^ 0.

0.6^ 0.

1

−0.8 −1 −0.8 −0.6 −0.4 −0.2 0 0.2 0.4 0.6 0.8 1 −0.6^ −0.

−0.2^0

0.2^ 0.

0.6^ 0.

1

−0.8 −1 −0.8 −0.6 −0.4 −0.2 0 0.2 0.4 0.6 0.8 1 −0.6^ −0.

−0.2^0

0.2^ 0.

0.6^ 0.

1

−0.8 −1 −0.8 −0.6 −0.4 −0.2 0 0.2 0.4 0.6 0.8 1 −0.6^ −0.

−0.2^0

0.2^ 0.

0.6^ 0.

1

−0.8 −1 −0.8 −0.6 −0.4 −0.2 0 0.2 0.4 0.6 0.8 1 −0.6^ −0.

−0.2^0

0.2^ 0.

0.6^ 0.

1

  1. The Generalized EM algorithm

When attempting to run the EM algorithm, it may sometimes be difficult to perform the M step exactly — recall that we often need to implement numerical optimization to perform the maximization, which can be costly. Therefore, instead of finding the global maximum of our lower bound on the log-likelihood, and alternative is to just increase this lower bound a little bit, by taking one step of gradient ascent, for example. This is commonly known as the Generalized EM (GEM) algorithm.

Put slightly more formally, recall that the M-step of the standard EM algorithm performs the maximization

θ := arg max θ

i

z(i)

Qi(z(i)) log

p(x(i), z(i); θ) Qi(z(i))

The GEM algorithm, in constrast, performs the following update in the M-step:

θ := θ + α∇θ

i

z(i)

Qi(z(i)) log

p(x(i), z(i); θ) Qi(z(i))

where α is a learning rate which we assume is choosen small enough such that we do not decrease the objective function when taking this gradient step.

(a) Prove that the GEM algorithm described above converges. To do this, you should show that the the likelihood is monotonically improving, as it does for the EM algo- rithm — i.e., show that ℓ(θ(t+1)) ≥ ℓ(θ(t)). Answer: We use the same logic as for the standard EM algorithm. Specifically, just as for EM, we have for the GEM algorithm that

ℓ(θ(t+1)) ≥

i

z(i)

Q

(t) i (z

(i)) log p(x (i), z(i); θ(t+1))

Q( it )(z(i))

≥

i

z(i)

Q( it )(z(i)) log

p(x(i), z(i); θ(t)) Q( it )(z(i)) = ℓ(θ(t))

where as in EM the first line holds due to Jensen’s equality, and the last line holds because we choose the Q distribution to make this hold with equality. The only difference between EM and GEM is the logic as to why the second line holds: for EM it held because θ(t+1) was chosen to maximize this quantity, but for GEM it holds by our assumption that we take a gradient step small enough so as not to decrease the objective function. (b) Instead of using the EM algorithm at all, suppose we just want to apply gradient ascent to maximize the log-likelihood directly. In other words, we are trying to maximize the (non-convex) function

ℓ(θ) =

i

log

z(i)

p(x(i), z(i); θ)

so we could simply use the update

θ := θ + α∇θ

i

log

z(i)

p(x(i), z(i); θ).

Show that this procedure in fact gives the same update as the GEM algorithm de- scribed above. Answer: Differentiating the log likelihood directly we get ∂ ∂θj

i

log

z(i)

p(x(i), z(i); θ) =

i

z(i)^ p(x (i), z(i); θ)

z(i)

∂θj

p(x(i), z(i); θ)

i

z(i)

p(x(i); θ)

∂θj

p(x(i), z(i); θ).