CS 129 Spring 2020 - Stanford UniversityProblem 2: Bias-Variance tradeo One of the key results in...

8
CS 129 - Winter 2021 Problem Set 2 Instructors: Andrew Ng, Younes Bensouda Mourri February 2021 Problem 1: Multi-class Classification Remember that the first method seen in class for multi-class classification is One vs. All. Assuming you have K classes, the One vs. All does the following: For each k [0,K], assign label 1 to samples from class k and label 0 to samples from all other classes. For each k [0,K], build a binary classifier using the dataset created in the previous step. The k-th classifier outputs the probability of belonging to class k versus all the other classes. (Hence the name One vs. All). Given a sample, predict the probability of belonging to class k versus all the other classes for every k. Choose the k that has the greatest probability. Let’s get into botany (No prerequisite in botany required ;)). (a) (b) (c) Figure 1: Iris Setosa (Top left), Iris Versicolour (Top right), Iris Virginica (Bottom) 1

Transcript of CS 129 Spring 2020 - Stanford UniversityProblem 2: Bias-Variance tradeo One of the key results in...

Page 1: CS 129 Spring 2020 - Stanford UniversityProblem 2: Bias-Variance tradeo One of the key results in Machine Learning is the Bias-Variance tradeo . We will see in class the intuition

CS 129 − Winter 2021Problem Set 2

Instructors: Andrew Ng, Younes Bensouda Mourri

February 2021

Problem 1: Multi-class Classification

Remember that the first method seen in class for multi-class classification is One vs. All. Assuming youhave K classes, the One vs. All does the following:

• For each k ∈ [0,K], assign label 1 to samples from class k and label 0 to samples from all other classes.

• For each k ∈ [0,K], build a binary classifier using the dataset created in the previous step. The k−thclassifier outputs the probability of belonging to class k versus all the other classes. (Hence the nameOne vs. All).

• Given a sample, predict the probability of belonging to class k versus all the other classes for every k.

• Choose the k that has the greatest probability.

Let’s get into botany (No prerequisite in botany required ;)).

(a)(b)

(c)

Figure 1: Iris Setosa (Top left), Iris Versicolour (Top right), Iris Virginica (Bottom)

1

Page 2: CS 129 Spring 2020 - Stanford UniversityProblem 2: Bias-Variance tradeo One of the key results in Machine Learning is the Bias-Variance tradeo . We will see in class the intuition

For this question, you will work with the iris dataset. This dataset is a Machine Learning standard formulti-class classification. Here is a description of the data. The features are:

• Sepal length (in cm)

• Sepal width (in cm)

• Petal length (in cm)

• Petal width (in cm)

The classes are:

• Iris Setosa

• Iris Versicolour

• Iris Virginica

Note: you do not need to import the data or code anything. If you are interested in the dataset, you canfind it here.

In this question, we are only using two features: petal length and sepal length.

1. (10 points) The following plot represents the flowers plotted against the two features. Using the Onevs. All method, we should build 3 classifiers. For a logistic regression classifier, what is the shape ofthe decision boundary? Note: no math required, just a general answer is enough.

Figure 2: Plot of the iris dataset using two features: petal length, sepal length

The shape of the decision boundary of a binary classifier built using Logistic Regression is a hyperplanein the feature space. In two dimensions, it will simply be a line.

2

Page 3: CS 129 Spring 2020 - Stanford UniversityProblem 2: Bias-Variance tradeo One of the key results in Machine Learning is the Bias-Variance tradeo . We will see in class the intuition

2. (10 points) Let’s assume that we trained three classifiers on the data:

• the Setosa vs. All classifier

• the Versicolour vs. All classifier

• the Virginica vs. All classifier

Given the plot, rank the classifiers by their accuracy. Hint: the answer should not consist of math.Think about classifiers visually.

Actual accuracies are given in parenthesis:

1. Setosa vs All (100%)

2. Virginica vs All (97%)

3. Versicolour vs All (66%)

Problem 2: Bias-Variance tradeoff

One of the key results in Machine Learning is the Bias-Variance tradeoff. We will see in class the intuition andthe implications of this important result but in this question, you will prove the result. In linear regression,the general assumptions are:

• Y = f(X) + ε (ε is a random variable that represents noise, f is deterministic and is the model we useto map X to Y )

• ε is independent of X hence of f̂(X)

• ε has mean 0 and variance σ2

Using data, we build f̂ ; an estimator of the model. Note: f̂ is a random variable. However, you can considerthat the data X is given: it’s deterministic, constant, not a random variable; so f(X) is also deterministic.The error of the model is defined as:

Err = E[(Y − f̂(X))2

]1. (5 points) Prove the following (for simplification, we note: f = f(X), f̂ = f̂(X)):

Err = E[(f − f̂)2

]+ E

[ε2]

+ 2E[(f − f̂)ε

]Err = E

[(Y − f̂)2

]= E

[(f − f̂ + ε)2

]= E

[(f − f̂)2

]+ E

[ε2]

+ 2E[(f − f̂)ε

]2. (5 points) Prove the following:

E[(f − f̂)ε

]= 0

ε is independent of X therefore of f̂(X). Furthermore, f is deterministic. Hence:

E[(f − f̂)ε

]= E

[(f − f̂)

]E[ε]

= 0

3

Page 4: CS 129 Spring 2020 - Stanford UniversityProblem 2: Bias-Variance tradeo One of the key results in Machine Learning is the Bias-Variance tradeo . We will see in class the intuition

3. (5 points) We define the bias of the model as the expected distance between the model and the

hypothesis: Bias = E[f − f̂

]. Prove the following:

E[(f − f̂)2

]= Var

[f̂]

+ E[f − f̂

]2E[(f − f̂)2

]= Var

[f − f̂

]+ E

[f − f̂

]2= Var

[f̂]

+ E[f − f̂

]2because f is deterministic

4. (5 points) Derive the expression of the error. Note: your result should only depend on Var[f̂], Bias,

and σ.Err = Var

[f̂]

+ Bias2 + σ2

Note: think about the implication of this equation. For instance, if your model is perfect, what isthe best error you can achieve? 0 right? Not really: σ2. Moreover, think about the tradeoff in termsof complexity. A simpler model will have lower variance but higher bias and vice-versa for a morecomplex model.

4

Page 5: CS 129 Spring 2020 - Stanford UniversityProblem 2: Bias-Variance tradeo One of the key results in Machine Learning is the Bias-Variance tradeo . We will see in class the intuition

Problem 3: Softmax Classification

In this question, you will see a new method for multi-class classification: the softmax method. Assume thereare K classes. We define a weight matrix θ such that θi represents a row vector of weights used to classifyclass i. The probability assumption of the softmax model for a given class k and datapoint x is the following:

P(Y = k|x, θ

)=

1

Zeθkx (Z is a constant)

Note 1 : in this case, x is a column vector. So in this problem, each column of X represents a trainingexample.Note 2 : θX represents a matrix whose coordinate (i, j) is the score of datapoint j belonging to class i.

1. (5 points) Compute Z, i.e. find an expression of P(Y = k|x, θ

)that only depends on θk and x.

Using the fact that the sum of probabilities must be equal to 1, we have:

P(Y = k|x, θ

)=

eθkx

K∑i=1

eθix

2. (5 points) After computing the probability of belonging to class k for all k, how do we assign a class?In other words, given the probabilities, what is the decision rule?

We choose the class that maximizes the probability, i.e. k = argk max

[P(Y = k|x, θ

)]3. (5 points) One of the problems of the softmax method, is that if θkx is large, its exponential will be

extremely large. Therefore, we will face overflow errors. One of the methods used to mitigate thiseffect is to replace θkx by θkx − α where α = max

j[θjx]. Show that this method does not change the

probability values and explain why overflow is mitigated.

P̃(Y = k|x, θ

)=

eθkx−α

K∑i=1

eθix−α

=e−αeθkx

K∑i=1

e−αeθix

= P(Y = k|x, θ

)

The overflow is mitigated because θkx− α < 0 ∀k

5

Page 6: CS 129 Spring 2020 - Stanford UniversityProblem 2: Bias-Variance tradeo One of the key results in Machine Learning is the Bias-Variance tradeo . We will see in class the intuition

Problem 4: Machine Translation

You are about to build a simple Machine Translation system. Specifically, you will make use of word vectorsto create your Machine Translation system. You will create an X matrix where each row corresponds to aword vector trained on the English corpus. You will also create a Y matrix where each row corresponds to aword vector trained on the French corpus. Concretely, Xi, the i-th row of X, is a vector representing wordi in English. Similarly, Yi is a vector representing the French equivalent of word i. You will now learn amapping from X to Y using gradient descent. Specifically, you will minimize the following:

F =1

m‖XR− Y ‖2F

Once you have that mapping, given an English word vector, you can multiply it by R and use the euclideandistance to find the closest French vector. The word for that vector will then be your translation. In thisproblem we will ask you to compute ∂F

∂R .Note: X is of dimension (m,n), R is of dimension (n, n), and Y is of dimension (m,n). m corresponds tothe number of training examples.

‖A‖F =

√√√√ m∑i=1

n∑j=1

|aij |2 denotes the Frobenius norm.

1. (5 points) Compute the gradient of F with respect to R. In other words compute ∂F∂R .

No justification needed, other than making sure your dimensions match.

δF

δR=

1

m

δ

δR‖XR− Y ‖2F =

1

m

δ

δRTrace((XR− Y )T (XR− Y )) =

2

mXT (XR− Y )

2. (5 points) Given the gradient you just computed, how do you update your R variable?

Using gradient descent, follow the update rule:

R := R− αδFδR

6

Page 7: CS 129 Spring 2020 - Stanford UniversityProblem 2: Bias-Variance tradeo One of the key results in Machine Learning is the Bias-Variance tradeo . We will see in class the intuition

Problem 5: Weighted Linear Regression

In class, we saw that the cost function for linear regression is:

J(θ) =1

2m

m∑i=1

(hθ(x(i))− y(i))2

Here, you can notice that all the samples are weighted equally. However, in certain contexts, some samplesmay be more relevant than others. For instance, suppose you could detect the outliers in the data - e.g. asensor reporting an incorrect measurement. Then, common sense would suggest to assign small weights tooutliers, because you do not want them to influence your model. To take into account the relative importanceof each example, you can use Weighted Linear Regression (WLR). The cost function for WLR is:

J(θ) =1

2m

m∑i=1

w(i)(hθ(x(i))− y(i))2

Each sample is assigned a weight w(i).

1. (5 points) Show that you can define a matrix W such that the cost function can be rewritten as:

J(θ) = (Xθ − Y )TW (Xθ − Y )

Note: to get credit, you need to explicitly specify W .

Set W = 12m ∗ diag(w(1), . . . , w(m)). Then:

(Xθ − Y )TW (Xθ − Y ) =

m∑i=1

[Xθ − Y ]iw(i)[Xθ − Y ]i =

m∑i=1

(hθ(x(i))− y(i))w(i)(hθ(x

(i))− y(i)) = J(θ)

Note: the normalization factor does not matter here as it is equivalent to minimize 12mJ and J .

2. (5 points) Assume that θ ∈ Rd, a ∈ Rd, A ∈ Rd2 , and A is symmetric. ∇θ is the derivative with respectto θ. Show the following properties:

∇θ[aT θ

]= a

∇θ[θTAθ

]= 2Aθ

∂aT θ

∂θi=

d∑j=1

ajθj

∂θi= ai Hence the result.

Using the same indicator notation, we have:

∂θiθj∂θk

= δikθj + δjkθi

7

Page 8: CS 129 Spring 2020 - Stanford UniversityProblem 2: Bias-Variance tradeo One of the key results in Machine Learning is the Bias-Variance tradeo . We will see in class the intuition

Therefore,

∂θTAθ

∂θk=

d∑j=1

d∑i=1

θiθjAij

∂θk

=

d∑j=1

d∑i=1

[δikθj + δjkθi

]Aij

=

d∑j=1

d∑i=1

δikθjAij +

d∑j=1

d∑i=1

δjkθiAij

=

d∑j=1

θjAkj +

d∑i=1

θiAik

=

d∑j=1

θjAkj +

d∑i=1

θiAki (A is symmetric) = [2Aθ]k

3. (5 points) In class, we saw that the normal equation for (unweighted) linear regression is:

XTXθ = XTY ⇒ θmin =(XTX

)−1XTY

Derive the value of θ such that it minimizes the WLR cost function. Hint : Compute ∇θJ(θ) and set∇θJ(θ) = 0 to find θmin.

The loss can be rewritten as:

J(θ) = (Xθ − Y )TW (Xθ − Y )

= θTXTWXθ − θTXTWY − Y TWXθ + Y TWY

= θTXTWXθ − 2[XTWTY

]Tθ + Y TWY because θTXTWY = Y TWXθ

Therefore, applying the formula seen before (all conditions apply), we have:

∇θJ(θ) = 2XTWXθ − 2XTWTY ⇒ θmin = (XTWX)−1XTWTY

4. (5 points) We also saw in section a particular example where we used Locally Weighted Linear Regres-sion. We defined w(i) as

w(i) = e

(−

(x(i) − x)2

2τ2)

How would increasing the value of τ affect your cost function?

The smaller the τ , the faster the exponential decreases around x therefore the more localised it isaround x. The converse applies for a large τ . The extreme cases are always good to have in mind: ifτ = 0, the weight is zero except in x. If τ = +∞ all weights are equal to 1.

8