Salta al contenuto
Note per Studenti Introduction to Machine Learning - Mock Exam 4 Report

Introduction to Machine Learning - Mock Exam 4

Questa pagina non ha ancora la versione ripasso: qui sotto c'è il testo completo.

In questa pagina 14

The written tests of this course are not public. This mock exam is therefore built only on the notes of the course (learning and data, perceptron and its convergence bound, ADALINE, logistic regression, nearest neighbour, linear regression, Bernoulli likelihood, classification metrics, model selection, and the Python notebooks). The questions and the data are new: it is not a copy of any real test. The official format, from the course description, is continuous activities on the course platform and a final closed-book written test. The format used here is a proposal of this site, not declared by the course: 10 multiple-choice questions worth 1.5 points each and 3 problems worth 5 points each, for a total of 30 points, in 90 minutes, with four options per question and exactly one correct option.

Conventions used in the whole test: every input vector used with a weight vector contains the constant coordinate x0=1x_0 = 1, so x=[1,x1,x2]T\mathbf{x} = [1, x_1, x_2]^T and w=[w0,w1,w2]T\mathbf{w} = [w_0, w_1, w_2]^T; labels are ±1\pm 1 for ADALINE and 0/10/1 for logistic regression; in the multi-class problem the classes are 0,1,20, 1, 2; log⁡\log and ln⁡\ln are the natural logarithm.

How to use it: work with a timer, write each formula in letters before putting the numbers in, and look at the "Result" only when a question is finished.

Question 1

The first lecture quotes the definition of learning as "an adaptive change in behaviour caused by experience". How do the notes read machine learning in the light of this definition?

  • A) The experience is the hypothesis set, and the change in behaviour is the collection of the historical data.
  • B) The lecture says that the best way to build intelligent systems at present seems to be writing the rules by hand, so learning is only a side tool.
  • C) The experience is a set of data, and the change in behaviour is the choice of a function (the hypothesis) that will be used on new inputs.
  • D) The lecture says that adaptive behaviour comes from evolution only, so learning from experience plays no role.
Show the result

C

Show the full solution

C is correct: in Introduction to Machine Learning - Learning, Data and TasksMachine learning builds a hypothesis g that approximates an unknown target function f using only a set of historical examples (input, output). The course lecture frames learning as adaptive behaviour, shows through the credit-approval example the components of learning (input x, output y, target f, data, hypothesis g, hypothesis set, learning algorithm) and warns that a model is only as unbiased as its data. Tasks named in the course are classification and regression, supervised and non-supervised problems.Introduction to Machine Learning - Learning, Data and Tasks → machine learning is "the second definition made concrete": the experience is a set of data, and the change in behaviour is the choice of the hypothesis that is used on new inputs. A swaps the two roles. B is false: the slide states that the best way to build intelligent systems at present seems to be learning and inference. D is false: the note says that biological systems developed adaptive behaviour through learning and evolution, and that the capstone of adaptive behaviour is learning and intelligence.

Question 2

Which statement about the tasks named in the course is correct?

  • A) Deciding whether credit is approved (good or bad customer) is a regression task, because the decision is stored as a number.
  • B) In classification the output yy is a class label and in regression it is a real number; every example of the course material comes with its output yy, so the material is supervised.
  • C) The perceptron notes use y∈{+1,−1}y \in \{+1, -1\}, so the perceptron solves a regression task.
  • D) The lectures of the course develop clustering and the other non-supervised methods in detail, with worked examples.
Show the result

B

Show the full solution

B is correct: in Introduction to Machine Learning - Learning, Data and TasksMachine learning builds a hypothesis g that approximates an unknown target function f using only a set of historical examples (input, output). The course lecture frames learning as adaptive behaviour, shows through the credit-approval example the components of learning (input x, output y, target f, data, hypothesis g, hypothesis set, learning algorithm) and warns that a model is only as unbiased as its data. Tasks named in the course are classification and regression, supervised and non-supervised problems.Introduction to Machine Learning - Learning, Data and Tasks → classification has a class label as output (in the perceptron notes y∈{+1,−1}y \in \{+1, -1\}) and regression a real number, and all the material of the course source is supervised, since every example comes with its output yy. A is false: the output of the credit example is a good or bad customer, a class. C is false: labels +1+1 and −1-1 are class labels, so the perceptron is a classifier. D is false: clustering and the other non-supervised methods are in the official program, but the notes state that there is no lecture material on them.

Question 3

All the points of a data set satisfy ∥xi∥≤R\|\mathbf{x}_i\| \le R with R=3R = 3. There is a unit vector w∗\mathbf{w}^* with yi w∗Txi≥γy_i\,\mathbf{w}^{*T}\mathbf{x}_i \ge \gamma for every point, with γ=0.1\gamma = 0.1. The Perceptron Learning Algorithm starts from w1=0\mathbf{w}_1 = \mathbf{0}. Which statement is guaranteed by the theorem of the course?

  • A) The algorithm makes at most 900900 updates.
  • B) The algorithm makes at most 3030 updates.
  • C) The algorithm makes exactly 900900 updates.
  • D) The algorithm makes at most 900900 passes over the data.
Show the result

A

Show the full solution

The theorem of Perceptron Convergence and the Margin BoundThe distance of a point x from the hyperplane w^T x + b = 0 is |w^T x + b| / sqrt(w^T w), obtained by writing x = x_p + gamma with gamma parallel to w. If the data are linearly separable with margin gamma (a unit vector w* with y_i w*^T x_i >= gamma for all i) and every point lies in a ball of radius R, the Perceptron Learning Algorithm makes at most K <= R^2 / gamma^2 updates. Proof: after K updates w_{K+1}^T w* >= K gamma, hence norm of w_{K+1} >= K gamma; and norm of w_{K+1} squared <= K R^2; combine.Perceptron Convergence and the Margin Bound → says K≤R2/γ2K \le R^2/\gamma^2, with KK the number of updates. Here R2/γ2=9/0.01=900R^2/\gamma^2 = 9/0.01 = 900, so A is correct. B is R/γ=30R/\gamma = 30: the squares are missing. C is false: the bound is only an upper limit, the algorithm may stop much earlier. D is false: the bound counts updates, not passes over the data (a pass may contain many updates or none).

Question 4

In the first notebook, NumPy arrays are defined as x = np.array([ [2, 1, 0], [-1, 3, 2] ]) and y = np.array([1, 2, -1]). Which statement is correct?

  • A) np.dot(x, y) returns [4, 7].
  • B) np.dot(x, y) returns [4, 3], but np.inner(x, y) signals an error, because x is not square.
  • C) np.dot(x, y) returns the single number 7, the sum of all the products.
  • D) np.dot(x, y) returns [4, 3], and np.inner(x, y) returns the same vector.
Show the result

D

Show the full solution

In Python Notebooks - Random Samples, Plots and Linear Algebra with NumPyThe first course notebook draws random samples with NumPy (uniform with np.random.rand, normal and exponential), plots them with matplotlib (line and scatter plots, histograms compared with the theoretical density from scipy.stats), and shows the NumPy products used later: np.dot and np.inner between a matrix and a vector and between two matrices, with the transpose. This note reproduces the code of the notebook "matplotlib, random numbers and statistics" with the meaning of each call.Python Notebooks - Random Samples, Plots and Linear Algebra with NumPy → a matrix times a vector multiplies each row of the matrix by the vector: row 11 gives 2⋅1+1⋅2+0⋅(−1)=42\cdot1 + 1\cdot2 + 0\cdot(-1) = 4 and row 22 gives (−1)⋅1+3⋅2+2⋅(−1)=−1+6−2=3(-1)\cdot1 + 3\cdot2 + 2\cdot(-1) = -1 + 6 - 2 = 3, so np.dot(x, y) is [4, 3]; in the notebook np.inner(x, y) gives the same result for a matrix and a vector. So D is correct. A has the sign of the last product wrong in the second row (−1+6+2=7-1 + 6 + 2 = 7). B is false: np.inner works on these shapes, and no squareness is required. C is false: the result has one entry for each row of x, it is not a single number.

Question 5

For an object, the probability of the positive class given by a logistic-regression model is P(class=1∣x)=0.2P(\text{class} = 1 \mid \mathbf{x}) = 0.2. Which statement is correct?

  • A) The odds are 0.250.25 and the logit is ln⁡0.25≈−1.386\ln 0.25 \approx -1.386, which is negative: the object goes to class 00.
  • B) The odds are 44 and the logit is ln⁡4≈+1.386\ln 4 \approx +1.386: the object goes to class 11.
  • C) The odds are 0.20.2 and the logit is ln⁡0.2≈−1.609\ln 0.2 \approx -1.609.
  • D) The odds are 0.250.25 and the logit is +1.386+1.386, because the logit is a positive number.
Show the result

A

Show the full solution

In Logistic RegressionLogistic regression models the probability p of the positive class: the logit (log of the odds p/(1-p)) is assumed linear in the input, logit(p) = w^T x = z. Inverting it gives p = 1/(1 + e^(-z)), the logistic sigmoid; an object goes to class 1 when the sigmoid exceeds 0.5, i.e. when z > 0. The parameters are learned by maximising the Bernoulli log-likelihood, equivalently minimising the cost J = -y log(yhat) - (1 - y) log(1 - yhat), by gradient descent with gradient (1/m) X^T (h(x) - y).Logistic Regression → the odds are p/(1−p)=0.2/0.8=0.25p/(1-p) = 0.2/0.8 = 0.25 and the logit is the logarithm of the odds, ln⁡0.25≈−1.386\ln 0.25 \approx -1.386. The logit is negative for p<0.5p < 0.5, so z=wTx<0z = \mathbf{w}^T\mathbf{x} < 0 and the object is assigned to class 00 (class 11 needs z>0z > 0): A is correct. B inverts the ratio (positive over negative class is 0.2/0.80.2/0.8, not 0.8/0.20.8/0.2). C takes the probability itself as the odds. D is false: the logit maps (0,1)(0, 1) onto the whole real line, negative below p=0.5p = 0.5 and positive above.

Question 6

A data set has 120120 objects, each described by two features x1,x2x_1, x_2 and belonging to one of three classes C1,C2,C3C_1, C_2, C_3. Which description of the setting of nearest neighbour in the notes is correct?

  • A) The data matrix XX is 2×1202 \times 120, with one column per object, and the class of a test point is decided by the classes of the training points around it.
  • B) The data matrix XX is 120×3120 \times 3, with one column for each class, and the class of a test point is read from the column with the largest entry.
  • C) The data matrix XX is 120×2120 \times 2, with one row per object and one column per feature, and the class of a test point is decided by the classes of the training points close to it.
  • D) The data matrix XX is 120×2120 \times 2, and the class of a test point is decided by a weight vector found in a long training phase.
Show the result

C

Show the full solution

C is correct, as in Nearest NeighbourK-nearest neighbours (KNN) has an almost empty training phase (the data are just stored) and does all the work at test time: given a point, compute its distance from all training points, sort the distances, keep the K shortest and predict the (weighted) majority class among them. The course source has one handwritten page on it; the notebooks use it as one of six scikit-learn classifiers, and the model-comparison example contrasts KNN with K=1 and K=5.Nearest Neighbour →: the objects are collected in a data matrix XX with columns x1,x2x_1, x_2 and one row per object (here 120×2120 \times 2), and the class of a test point is decided by the classes of the training points around it (the nearest ones). A transposes the matrix: the notes put one row per object. B is false: the columns are the features x1,x2x_1, x_2, not the classes. D is false: the training phase does almost nothing (the data are stored), no weight vector is learned, and the work is done at test time.

Question 7

A linear hypothesis h(x)=w0+w1xh(x) = w_0 + w_1x is fitted to the four samples (x,y)(x, y): (0,1)(0, 1), (1,3)(1, 3), (2,2)(2, 2), (3,6)(3, 6), with the closed form w=(XTX)−1XTY\mathbf{w} = (X^TX)^{-1}X^TY and the first column of XX made of ones. What is [w0,w1][w_0, w_1]?

  • A) [1.4,0.9][1.4, 0.9]
  • B) [0.9,1.4][0.9, 1.4]
  • C) [3,25/14]≈[3,1.79][3, 25/14] \approx [3, 1.79]
  • D) [15.9,8.6][15.9, 8.6]
Show the result

B

Show the full solution

The data matrix has rows [1,0],[1,1],[1,2],[1,3][1, 0], [1, 1], [1, 2], [1, 3], so (see Linear RegressionIn linear regression the hypothesis is h(x) = w^T x and the in-sample error is E_in(w) = (1/N) sum (w^T x_i - y_i)^2 = (1/N) times the squared norm of (Xw - Y), with X the N-row data matrix. Setting the gradient (2/N) X^T (Xw - Y) to zero gives the normal equations X^T X w = X^T Y and the closed-form solution w = (X^T X)^{-1} X^T Y, where (X^T X)^{-1} X^T is the pseudo-inverse of X; only a small square matrix (number of features) has to be inverted. Polynomial regression is the same formula on a data matrix with columns 1, x, x^2, ...Linear Regression →)

XTX=[46614],XTY=[1+3+2+60+3+4+18]=[1225].X^TX = \begin{bmatrix}4 & 6\\ 6 & 14\end{bmatrix},\qquad X^TY = \begin{bmatrix}1 + 3 + 2 + 6\\ 0 + 3 + 4 + 18\end{bmatrix} = \begin{bmatrix}12\\ 25\end{bmatrix}.

The normal equations XTX w=XTYX^TX\,\mathbf{w} = X^TY are 4w0+6w1=124w_0 + 6w_1 = 12 and 6w0+14w1=256w_0 + 14w_1 = 25. The first gives w0=3−1.5w1w_0 = 3 - 1.5w_1; in the second, 18−9w1+14w1=2518 - 9w_1 + 14w_1 = 25, so w1=1.4w_1 = 1.4 and w0=0.9w_0 = 0.9: B is correct. A swaps the two weights. C solves each equation as if XTXX^TX were diagonal (12/412/4 and 25/1425/14), ignoring the off-diagonal entries 66. D comes from a wrong sign in the off-diagonal entries of the inverse matrix.

Question 8

In n=12n = 12 tosses of a coin, heads came up K=9K = 9 times. The log-likelihood of the Bernoulli parameter is ℓ(θ)=9ln⁡θ+3ln⁡(1−θ)\ell(\theta) = 9\ln\theta + 3\ln(1-\theta). Which statement is correct?

  • A) The maximum likelihood estimate is θ^=1/4\hat\theta = 1/4 and the derivative ℓ′(0.5)\ell'(0.5) is −12-12.
  • B) The maximum likelihood estimate is θ^=3/4\hat\theta = 3/4 and the derivative ℓ′(0.5)\ell'(0.5) is +24+24.
  • C) The maximum likelihood estimate is θ^=1/2\hat\theta = 1/2 and the derivative ℓ′(0.5)\ell'(0.5) is 00.
  • D) The maximum likelihood estimate is θ^=3/4\hat\theta = 3/4 and the derivative ℓ′(0.5)\ell'(0.5) is +12+12, so ℓ\ell is still increasing at θ=0.5\theta = 0.5.
Show the result

D

Show the full solution

In Bernoulli Distribution and Maximum Likelihood EstimationA binary outcome (heads = 1, tails = 0) is modelled by a Bernoulli distribution with parameter theta: P(x | theta) = theta^x (1 - theta)^(1 - x). For independent identically distributed observations the likelihood is the product of the single probabilities; with K heads in n tosses it is L(theta) = theta^K (1 - theta)^(n - K). Maximising the log-likelihood l(theta) = K log theta + (n - K) log(1 - theta) (maximum likelihood estimation) gives theta = K / n.Bernoulli Distribution and Maximum Likelihood Estimation → the derivative is Kθ−n−K1−θ\dfrac{K}{\theta} - \dfrac{n-K}{1-\theta} (the factor −1-1 comes from the chain rule on ln⁡(1−θ)\ln(1-\theta)), and setting it to zero gives θ^=K/n=9/12=3/4\hat\theta = K/n = 9/12 = 3/4. At θ=0.5\theta = 0.5: 90.5−30.5=18−6=+12>0\dfrac{9}{0.5} - \dfrac{3}{0.5} = 18 - 6 = +12 > 0: the log-likelihood is still increasing there, consistently with a maximum at 3/4>0.53/4 > 0.5. So D is correct. A uses the fraction of tails. B forgets the factor −1-1 and adds 18+6=2418 + 6 = 24. C would need K=n−KK = n - K (9=39 = 3 is false): the estimate is the observed fraction of heads, not 1/21/2.

Question 9

Which statement about the comparison of two models with a statistical test, as in the notes, is correct?

  • A) The null hypothesis is that the two models perform equally; the t-test is parametric (it assumes a distribution for the scores) and the Wilcoxon test is non-parametric (it does not).
  • B) The null hypothesis is that the two models perform differently, and the test tries to confirm it.
  • C) The t-test is non-parametric and the Wilcoxon test is parametric.
  • D) Both the t-test and the Wilcoxon test assume a distribution for the scores, the bell curves drawn in the notes.
Show the result

A

Show the full solution

A is correct: Model Selection and Validation - Hold-Out, k-Fold and Statistical TestsTo decide which of two models is better, fix their hyper-parameters, train each on a training set (this fits the parameters, the vector w or theta) and measure a performance score such as F1 on a separate test set; one split is too fragile, so the hold-out is repeated K times (or K-fold cross-validation is used) and the paired scores of the two models are compared with a statistical test. The null hypothesis H0 is that the two models perform equally; the significance level alpha is fixed before the experiment, and the p-value is the probability of observing the obtained differences under H0. Parametric test: t-test; non-parametric: Wilcoxon.Model Selection and Validation - Hold-Out, k-Fold and Statistical Tests → states that the null hypothesis (H0H_0, the initial one) is that the two models perform equally and that the test asks whether the data are compatible with it; the t-test is the parametric test (the sketch draws two assumed bell curves) and the Wilcoxon test is the non-parametric one, which does not assume a distribution. B is false: H0H_0 is equivalence, not difference. C swaps the two tests. D is false: the bell curves belong to the parametric test, while the Wilcoxon test does not assume a distribution (it is the one used in Python Notebooks - scikit-learn Model Comparison and Wilcoxon TestTwo example notebooks compare six scikit-learn classifiers (logistic regression, perceptron, KNN, decision tree, SVC, random forest) on a real data set loaded from the UCI repository: hold-out split with train_test_split, F1 score, k-fold cross-validation with cross_validate and scoring f1_macro, box plots of the fold scores, and the Wilcoxon signed-rank test (scipy.stats.wilcoxon) on the paired fold scores; the second notebook adds a stratified test set, more folds on the best models and the final training and test of the chosen model. A separate short notebook draws two normal samples to see how often the Wilcoxon test rejects at the 0.05 level.Python Notebooks - scikit-learn Model Comparison and Wilcoxon Test →).

Question 10

With line = 'a quick red fox, a quick red hare', which statement about the string methods of the course notebook is correct?

  • A) line.find('quick') returns 1.
  • B) line.startswith('quick') returns True.
  • C) line.replace('quick', 'slow', 1) returns 'a slow red fox, a quick red hare'.
  • D) line.partition('xyz') returns ('a quick red fox, a quick red hare',), a tuple with one element.
Show the result

C

Show the full solution

In Python Notebooks - Strings and Regular ExpressionsNotebook on Python strings: quoting (single, double, triple quotes), case methods (upper, lower, title, capitalize, swapcase), removing characters at the ends (strip), searching and replacing (find, startswith, endswith, replace, partition), splitting (split, splitlines), and a first use of regular expressions with the re module (search, findall, sub) with the anchors for start and end of string, the dot, the star, the plus and character classes.Python Notebooks - Strings and Regular Expressions → the third argument of replace is the maximum number of replacements, so only the first quick is replaced and C is correct. A is false: find returns the index where the substring starts, and 'quick' starts at index 2 ('a' is at 0 and the space at 1). B is false: the string starts with 'a ', so startswith('quick') is False. D is false: when the separator is not found partition returns a tuple of three elements, the whole string followed by two empty strings.

Problem 1 (points 5)

A node has four binary inputs x1,x2,x3,x4∈{0,1}x_1, x_2, x_3, x_4 \in \{0, 1\} and weights (w1,w2,w3,w4)=(2,−1,1,3)(w_1, w_2, w_3, w_4) = (2, -1, 1, 3). The rule is: the node fires (output +1+1) when ∑iwixi\sum_i w_ix_i is greater than the threshold, and does not fire (output −1-1) when the sum is less than or equal to the threshold. The threshold is 33.

a) For the inputs (1,0,1,0)(1, 0, 1, 0), (1,1,0,1)(1, 1, 0, 1) and (0,1,1,0)(0, 1, 1, 0) compute the weighted sum and say whether the node fires (1 point).

b) Write the bias trick: the weight vector w=[w0,w1,…,w4]T\mathbf{w} = [w_0, w_1, \dots, w_4]^T, the three input vectors x=[1,x1,…,x4]T\mathbf{x} = [1, x_1, \dots, x_4]^T and the three values wTx\mathbf{w}^T\mathbf{x}. What is the rule in terms of wTx\mathbf{w}^T\mathbf{x} (1 point)?

c) Among the 1616 binary inputs, list those on which the node fires. How many fire if the threshold is 2.52.5 and how many if it is 3.53.5? Explain the result for 3.53.5 (1.5 points).

d) With two inputs x1,x2∈{0,1}x_1, x_2 \in \{0, 1\} and weights (1,1)(1, 1), for which thresholds does the node compute "at least one input is 11" and for which thresholds "both inputs are 11"? For the threshold 1.51.5 write the boundary as x2=a x1+bx_2 = a\,x_1 + b and say which of the four corners lie on the firing side (1.5 points).

Show the result

a) Sums 33, 44, 00: the node does not fire on (1,0,1,0)(1,0,1,0) (the sum equals the threshold), fires on (1,1,0,1)(1,1,0,1), does not fire on (0,1,1,0)(0,1,1,0).

b) w=[−3,2,−1,1,3]T\mathbf{w} = [-3, 2, -1, 1, 3]^T; the values wTx\mathbf{w}^T\mathbf{x} are 00, 11, −3-3. The node fires when wTx>0\mathbf{w}^T\mathbf{x} > 0.

c) It fires on (0,0,1,1)(0,0,1,1), (1,0,0,1)(1,0,0,1), (1,0,1,1)(1,0,1,1), (1,1,0,1)(1,1,0,1), (1,1,1,1)(1,1,1,1): 55 inputs. Threshold 2.52.5: 88 inputs (the three with sum 33 are added). Threshold 3.53.5: the same 55 inputs, because every sum is an integer and "greater than 3.53.5" is the same as "greater than 33".

d) "At least one input is 11" (the sums are 0,1,1,20, 1, 1, 2): threshold in [0,1)[0, 1). "Both inputs are 11": threshold in [1,2)[1, 2). For 1.51.5 the boundary is x2=−x1+1.5x_2 = -x_1 + 1.5 and only the corner (1,1)(1, 1) is on the firing side.

Show the full solution

Setting: the neuron model and the bias trick of Halfspaces and the Perceptron AlgorithmA perceptron computes h(x) = sign(w^T x) where x includes a constant coordinate x0 = 1 that absorbs the threshold into the bias weight w0; its decision boundary is the hyperplane w^T x = 0 (a line in the plane, x2 = -(w1/w2) x1 - w0/w2). The Perceptron Learning Algorithm scans the data and, on every misclassified point, replaces w by w + y x. A single perceptron cannot separate the exclusive-or pattern; two perceptrons feeding a third give a multilayer network.Halfspaces and the Perceptron Algorithm →.

a) Sums. The weighted sum is 2x1−x2+x3+3x42x_1 - x_2 + x_3 + 3x_4.

  • (1,0,1,0)(1, 0, 1, 0): 2−0+1+0=32 - 0 + 1 + 0 = 3. The sum equals the threshold, and the rule says the node fires only when the sum is greater than the threshold: it does not fire (−1-1).
  • (1,1,0,1)(1, 1, 0, 1): 2−1+0+3=4>32 - 1 + 0 + 3 = 4 > 3: it fires (+1+1).
  • (0,1,1,0)(0, 1, 1, 0): 0−1+1+0=0≤30 - 1 + 1 + 0 = 0 \le 3: it does not fire.

b) Bias trick. The part ∑iwixi\sum_i w_ix_i and the term −threshold-\text{threshold} are merged by adding a constant input x0=1x_0 = 1 with weight w0=−threshold=−3w_0 = -\text{threshold} = -3, so w=[−3,2,−1,1,3]T\mathbf{w} = [-3, 2, -1, 1, 3]^T. The input vectors are [1,1,0,1,0]T[1, 1, 0, 1, 0]^T, [1,1,1,0,1]T[1, 1, 1, 0, 1]^T, [1,0,1,1,0]T[1, 0, 1, 1, 0]^T, and

  • wTx=−3+2+1=0\mathbf{w}^T\mathbf{x} = -3 + 2 + 1 = 0,
  • wTx=−3+2−1+3=1\mathbf{w}^T\mathbf{x} = -3 + 2 - 1 + 3 = 1,
  • wTx=−3−1+1=−3\mathbf{w}^T\mathbf{x} = -3 - 1 + 1 = -3.

Each value is the weighted sum minus 33 (3−33 - 3, 4−34 - 3, 0−30 - 3). The node fires when the sum is greater than the threshold, that is when wTx>0\mathbf{w}^T\mathbf{x} > 0; for the value 00 (first input) it does not fire, in agreement with part a.

c) All the inputs. The sum 2x1−x2+x3+3x42x_1 - x_2 + x_3 + 3x_4 is greater than 33 only if x4=1x_4 = 1 (without it the largest sum is 2+1=32 + 1 = 3, not greater than 33). With x4=1x_4 = 1 the sum is 3+2x1−x2+x33 + 2x_1 - x_2 + x_3, greater than 33 when 2x1−x2+x3>02x_1 - x_2 + x_3 > 0. The value of 2x1−x2+x32x_1 - x_2 + x_3 on the eight triples (x1,x2,x3)(x_1, x_2, x_3) is: (0,0,0)→0(0,0,0)\to 0, (0,0,1)→1(0,0,1)\to 1, (0,1,0)→−1(0,1,0)\to -1, (0,1,1)→0(0,1,1)\to 0, (1,0,0)→2(1,0,0)\to 2, (1,0,1)→3(1,0,1)\to 3, (1,1,0)→1(1,1,0)\to 1, (1,1,1)→2(1,1,1)\to 2. The positive ones give the inputs that fire: (0,0,1,1)(0,0,1,1), (1,0,0,1)(1,0,0,1), (1,0,1,1)(1,0,1,1), (1,1,0,1)(1,1,0,1), (1,1,1,1)(1,1,1,1): five inputs out of sixteen.

With threshold 2.52.5 the inputs with sum exactly 33 fire too: (0,0,0,1)(0,0,0,1), (0,1,1,1)(0,1,1,1), (1,0,1,0)(1,0,1,0), so 88 inputs. With threshold 3.53.5 the set does not change: the sums are integers, so no sum lies between 33 and 3.53.5, and "greater than 3.53.5" selects the same five inputs as "greater than 33" (the sums of those five are 4,5,6,4,54, 5, 6, 4, 5).

d) Two inputs. The sums over the four corners (0,0),(0,1),(1,0),(1,1)(0,0), (0,1), (1,0), (1,1) are 0,1,1,20, 1, 1, 2. "At least one input is 11" means the node fires on the sums 11 and 22 but not on 00: the threshold must satisfy 0≤threshold<10 \le \text{threshold} < 1 (greater than the sum 00 fails, smaller than 11 fires). "Both inputs are 11" means it fires only on the sum 22: 1≤threshold<21 \le \text{threshold} < 2. With threshold 1.51.5 the weight w0w_0 is −1.5-1.5 and the boundary x1+x2−1.5=0x_1 + x_2 - 1.5 = 0 is

x2=−w1w2x1−w0w2=−x1+1.5.x_2 = -\frac{w_1}{w_2}x_1 - \frac{w_0}{w_2} = -x_1 + 1.5 .

The firing side is x1+x2>1.5x_1 + x_2 > 1.5: of the four corners only (1,1)(1, 1) (sum 22) is on it; (0,1)(0, 1) and (1,0)(1, 0) have sum 1<1.51 < 1.5.

Typical mistakes: firing when the sum equals the threshold; using w0=+thresholdw_0 = +\text{threshold}; forgetting the constant input x0=1x_0 = 1 in the vector; reading a threshold of 3.53.5 as a different function from 33 when the sums are integers.

Problem 2 (points 5)

The unknown target is f(x)=x2f(x) = x^2 and the inputs x∈{0,1,2,3,4,5}x \in \{0, 1, 2, 3, 4, 5\} are drawn with the probabilities

xx 00 11 22 33 44 55
P(x)P(x) 0.300.30 0.300.30 0.250.25 0.100.10 0.040.04 0.010.01

Six independent samples (xi,yi)(x_i, y_i) with yi=f(xi)y_i = f(x_i) are drawn: xi=0,0,1,1,1,3x_i = 0, 0, 1, 1, 1, 3. The hypothesis is the line h(x)=w0+w1xh(x) = w_0 + w_1x with the weights of the closed form w=(XTX)−1XTY\mathbf{w} = (X^TX)^{-1}X^TY on the six samples. The squared error is used: Ein(h)=1N∑i(yi−h(xi))2E_{\text{in}}(h) = \frac{1}{N}\sum_i\big(y_i - h(x_i)\big)^2 on the samples, and Eout(h)=∑xP(x)(f(x)−h(x))2E_{\text{out}}(h) = \sum_x P(x)\big(f(x) - h(x)\big)^2 on the input distribution.

a) Write the data matrix, XTXX^TX and XTYX^TY and solve for w\mathbf{w} (1.5 points).

b) Compute Ein(h)E_{\text{in}}(h) (0.5 points).

c) Compute Eout(h)E_{\text{out}}(h), writing the contribution of each input (1.5 points).

d) How much of EoutE_{\text{out}} comes from the inputs x=4x = 4 and x=5x = 5, and what is the probability that none of the six samples falls there? If the six inputs had the same probability 1/61/6, what would EoutE_{\text{out}} be? What do the numbers show about EinE_{\text{in}} as a measure of the error on new inputs (1.5 points)?

Show the result

a) XTX=[66612]X^TX = \begin{bmatrix}6 & 6\\ 6 & 12\end{bmatrix}, XTY=[12,30]TX^TY = [12, 30]^T, w=[−1,3]T\mathbf{w} = [-1, 3]^T: h(x)=3x−1h(x) = 3x - 1.

b) Ein(h)=6/6=1E_{\text{in}}(h) = 6/6 = 1.

c) Contributions 0.3, 0.3, 0.25, 0.1, 1, 1.210.3,\ 0.3,\ 0.25,\ 0.1,\ 1,\ 1.21 for x=0,…,5x = 0, \dots, 5: Eout(h)=3.16E_{\text{out}}(h) = 3.16.

d) The inputs x=4,5x = 4, 5 contribute 2.212.21, about 70% of EoutE_{\text{out}}, although P(x≥4)=0.05P(x \ge 4) = 0.05. The probability that no sample falls there is 0.956≈0.7350.95^6 \approx 0.735. With the uniform probability 1/61/6: Eout=(1+1+1+1+25+121)/6=25E_{\text{out}} = (1 + 1 + 1 + 1 + 25 + 121)/6 = 25. Ein=1E_{\text{in}} = 1 says nothing about the regions without samples, and EoutE_{\text{out}} depends on the input distribution.

Show the full solution

Setting: Linear RegressionIn linear regression the hypothesis is h(x) = w^T x and the in-sample error is E_in(w) = (1/N) sum (w^T x_i - y_i)^2 = (1/N) times the squared norm of (Xw - Y), with X the N-row data matrix. Setting the gradient (2/N) X^T (Xw - Y) to zero gives the normal equations X^T X w = X^T Y and the closed-form solution w = (X^T X)^{-1} X^T Y, where (X^T X)^{-1} X^T is the pseudo-inverse of X; only a small square matrix (number of features) has to be inverted. Polynomial regression is the same formula on a data matrix with columns 1, x, x^2, ...Linear Regression → for the closed form, The Statistical Learning Framework and Empirical Risk MinimizationA hypothesis is judged by an error function computed pointwise on the data and averaged: the in-sample (empirical) error E_in(h) = (1/N) sum of e(f(x_i), h(x_i)); with squared differences it is the sum of squared errors. The learning algorithms of the course all search the hypothesis set for the h with small in-sample error, but what matters is the error on new inputs drawn from the same input distribution, so the data must cover the regions where the inputs live, and part of the data must be held back to check the result.The Statistical Learning Framework and Empirical Risk Minimization → for EinE_{\text{in}}, the input distribution and EoutE_{\text{out}}.

a) Closed form. The data matrix has the column of ones and the column of the inputs, and the outputs are y=x2y = x^2:

X=[101011111113],Y=[001119].X = \begin{bmatrix}1 & 0\\ 1 & 0\\ 1 & 1\\ 1 & 1\\ 1 & 1\\ 1 & 3\end{bmatrix},\qquad Y = \begin{bmatrix}0\\ 0\\ 1\\ 1\\ 1\\ 9\end{bmatrix}.

The sums are ∑x=6\sum x = 6, ∑x2=0+0+1+1+1+9=12\sum x^2 = 0 + 0 + 1 + 1 + 1 + 9 = 12, ∑y=12\sum y = 12, ∑xy=0+0+1+1+1+27=30\sum xy = 0 + 0 + 1 + 1 + 1 + 27 = 30, so

XTX=[66612],XTY=[1230].X^TX = \begin{bmatrix}6 & 6\\ 6 & 12\end{bmatrix},\qquad X^TY = \begin{bmatrix}12\\ 30\end{bmatrix}.

The normal equations XTX w=XTYX^TX\,\mathbf{w} = X^TY are 6w0+6w1=126w_0 + 6w_1 = 12 and 6w0+12w1=306w_0 + 12w_1 = 30. Subtracting the first from the second, 6w1=186w_1 = 18, so w1=3w_1 = 3 and w0=2−3=−1w_0 = 2 - 3 = -1. The matrix XTXX^TX is invertible (determinant 72−36=3672 - 36 = 36). Hence h(x)=3x−1h(x) = 3x - 1.

b) Error on the samples. The values of hh on the samples are −1,−1,2,2,2,8-1, -1, 2, 2, 2, 8, so the residuals yi−h(xi)y_i - h(x_i) are 1,1,−1,−1,−1,11, 1, -1, -1, -1, 1. Their squares sum to 66 and

Ein(h)=66=1.E_{\text{in}}(h) = \frac{6}{6} = 1 .

c) Error on the whole input distribution. For each input compute f(x)f(x), the value of hh, and P(x) (f(x)−h(x))2P(x)\,(f(x) - h(x))^2:

xx 00 11 22 33 44 55
f(x)f(x) 00 11 44 99 1616 2525
h(x)=3x−1h(x) = 3x - 1 −1-1 22 55 88 1111 1414
P(x) (f−h)2P(x)\,(f - h)^2 0.3⋅10.3\cdot 1 0.3⋅10.3\cdot 1 0.25⋅10.25\cdot 1 0.1⋅10.1\cdot 1 0.04⋅250.04\cdot 25 0.01⋅1210.01\cdot 121

The terms are 0.3,0.3,0.25,0.1,1,1.210.3, 0.3, 0.25, 0.1, 1, 1.21, with sum Eout(h)=3.16E_{\text{out}}(h) = 3.16.

d) What the numbers show. Ein=1E_{\text{in}} = 1 but Eout=3.16E_{\text{out}} = 3.16: the error on new inputs is more than three times the error on the samples. The inputs x=4x = 4 and x=5x = 5 give 1+1.21=2.211 + 1.21 = 2.21, that is 2.21/3.16≈0.702.21/3.16 \approx 0.70 (about 70%) of EoutE_{\text{out}}, although they have probability 0.04+0.01=0.050.04 + 0.01 = 0.05. Each sample falls in {0,1,2,3}\{0, 1, 2, 3\} with probability 0.950.95, and the samples are independent, so no sample falls in {4,5}\{4, 5\} with probability 0.956≈0.7350.95^6 \approx 0.735: most of the time the line is built without ever seeing that region. With the same probability 1/61/6 for the six inputs the squared differences are 1,1,1,1,25,1211, 1, 1, 1, 25, 121 and Eout=150/6=25E_{\text{out}} = 150/6 = 25: the same line, with the same EinE_{\text{in}}, has a very different error on new inputs when the inputs come from a different distribution. This is the point of the notes on the input distribution: the samples crowd where P(x)P(x) is large, the hypothesis follows ff there (the contributions of x=0,…,3x = 0, \dots, 3 add up to 0.950.95) and in the regions without samples it is not constrained at all, so the error on the samples is only a proxy for the error on the inputs that will really be seen.

Typical mistakes: dividing by NN when computing EoutE_{\text{out}} instead of weighting by P(x)P(x); weighting the six squared errors of the table with the same factor; forgetting that the sample contains the input x=0x = 0 twice and the input x=1x = 1 three times when building XTXX^TX.

Problem 3 (points 5)

An ADALINE unit is trained by stochastic gradient descent on three examples with labels ±1\pm 1. The data matrix and the labels are

X=[121102133],Y=[+1−1+1],X = \begin{bmatrix}1 & 2 & 1\\ 1 & 0 & 2\\ 1 & 3 & 3\end{bmatrix},\qquad Y = \begin{bmatrix}+1\\ -1\\ +1\end{bmatrix},

the initial weights are w=[0.5,0,0]T\mathbf{w} = [0.5, 0, 0]^T and the learning rate is η=0.1\eta = 0.1. The update after each example is the notebook rule w←w+η⋅2⋅xi⋅errori\mathbf{w} \leftarrow \mathbf{w} + \eta\cdot 2\cdot\mathbf{x}_i\cdot\text{error}_i, with errori=yi−wTxi\text{error}_i = y_i - \mathbf{w}^T\mathbf{x}_i computed with the current w\mathbf{w}. The in-sample error is Ein=1N∑i(yi−wTxi)2E_{\text{in}} = \frac{1}{N}\sum_i (y_i - \mathbf{w}^T\mathbf{x}_i)^2.

a) Compute EinE_{\text{in}} at the initial weights. Then run one epoch with the examples in the order 3,2,13, 2, 1: for each example write the output, the error and the weights after the update, and compute EinE_{\text{in}} at the end of the epoch (2 points).

b) Starting again from the initial weights, run one epoch with the order 1,2,31, 2, 3 and compute EinE_{\text{in}} at its end (1.5 points).

c) Compare the two epochs. For the example just used, the error after its own update is errori (1−2η ∥xi∥2)\text{error}_i\,(1 - 2\eta\,\|\mathbf{x}_i\|^2): compute the factor for the three examples and use it to explain why the first epoch lowers EinE_{\text{in}} and the second one raises it (1 point).

d) The notebook shuffles the data at every epoch with rand_perm = np.random.permutation(range(y.shape[0])) and loops over zip(X[rand_perm, :], y[rand_perm]). Which rand_perm gives the order of part a)? How many updates does an epoch make here, and how many would a batch epoch make? Why are X and y indexed with the same permutation? (0.5 points)

Show the result

a) Ein=2.75/3≈0.9167E_{\text{in}} = 2.75/3 \approx 0.9167. Example 3: output 0.50.5, error 0.50.5, w=[0.6,0.3,0.3]\mathbf{w} = [0.6, 0.3, 0.3]. Example 2: output 1.21.2, error −2.2-2.2, w=[0.16,0.3,−0.58]\mathbf{w} = [0.16, 0.3, -0.58]. Example 1: output 0.180.18, error 0.820.82, w=[0.324,0.628,−0.416]\mathbf{w} = [0.324, 0.628, -0.416]. At the end: outputs [1.164,−0.508,0.96][1.164, -0.508, 0.96], Ein=0.2706/3≈0.0902E_{\text{in}} = 0.2706/3 \approx 0.0902.

b) Example 1: output 0.50.5, error 0.50.5, w=[0.6,0.2,0.1]\mathbf{w} = [0.6, 0.2, 0.1]. Example 2: output 0.80.8, error −1.8-1.8, w=[0.24,0.2,−0.62]\mathbf{w} = [0.24, 0.2, -0.62]. Example 3: output −1.02-1.02, error 2.022.02, w=[0.644,1.412,0.592]\mathbf{w} = [0.644, 1.412, 0.592]. At the end: outputs [4.06,1.828,6.656][4.06, 1.828, 6.656], Ein=49.35/3≈16.45E_{\text{in}} = 49.35/3 \approx 16.45.

c) The factors 1−2η∥xi∥21 - 2\eta\|\mathbf{x}_i\|^2 are 1−1.2=−0.21 - 1.2 = -0.2, 1−1=01 - 1 = 0, 1−3.8=−2.81 - 3.8 = -2.8 for examples 1,2,31, 2, 3. The example 33 (large ∥x∥2=19\|\mathbf{x}\|^2 = 19) multiplies its own error by −2.8-2.8: it overshoots. If it comes first its error is small (0.50.5) and the later updates correct the result; if it comes last with a large error (2.022.02) nothing corrects it, and EinE_{\text{in}} ends at 16.4516.45, above the starting 0.91670.9167. The final weights depend on the order.

d) rand_perm = [2, 1, 0] (indices start from 00). 33 updates in the stochastic epoch, 11 in a batch epoch. The same permutation keeps every row of X paired with its label.

Show the full solution

Setting: ADALINE and Gradient DescentADALINE is a perceptron whose activation function is the identity: during training the unit outputs w^T x and the in-sample error E_in = (1/N) sum (y_i - w^T x_i)^2 is minimised by gradient descent, w_new = w_old - eta * grad E_in, with partial derivatives dE_in/dw_j = (2/N) sum (y_i - w^T x_i)(-x_i^j) and matrix form grad E_in proportional to -X^T (Y - X w). The prediction is still the sign of w^T x. Learning rate eta sets the step size and a stopping rule ends the iteration.ADALINE and Gradient Descent → for the error and the update, Python Notebooks - Perceptron and ADALINE from ScratchCode of the course notebooks that implement the Perceptron Learning Algorithm first with Python lists, then with plots of the successive lines, then with NumPy arrays (data matrix X with a column of ones, update w + eta * y * x on mistakes, epoch counter), and finally ADALINE: batch gradient descent with delta_w = eta * 2 * X^T (y - X w) / N and the stochastic version that shuffles the data and updates after every example. Differences between the versions (condition for a mistake, learning rate, stopping rule) are pointed out.Python Notebooks - Perceptron and ADALINE from Scratch → for the stochastic version (shuffling and the update with one example at a time), Python Notebooks - Random Samples, Plots and Linear Algebra with NumPyThe first course notebook draws random samples with NumPy (uniform with np.random.rand, normal and exponential), plots them with matplotlib (line and scatter plots, histograms compared with the theoretical density from scipy.stats), and shows the NumPy products used later: np.dot and np.inner between a matrix and a vector and between two matrices, with the transpose. This note reproduces the code of the notebook "matplotlib, random numbers and statistics" with the meaning of each call.Python Notebooks - Random Samples, Plots and Linear Algebra with NumPy → for np.dot and the shapes.

a) The epoch 3,2,13, 2, 1. At the start only w0w_0 is not zero and every row has x0=1x_0 = 1, so all the outputs are 0.50.5 and the errors are 0.50.5, −1.5-1.5, 0.50.5, with squares 0.250.25, 2.252.25, 0.250.25:

Ein=0.25+2.25+0.253=2.753≈0.9167.E_{\text{in}} = \frac{0.25 + 2.25 + 0.25}{3} = \frac{2.75}{3} \approx 0.9167 .

The update is w←w+0.2⋅errori⋅xi\mathbf{w} \leftarrow \mathbf{w} + 0.2\cdot\text{error}_i\cdot\mathbf{x}_i (since η⋅2=0.2\eta\cdot 2 = 0.2), and each error uses the weights as they are at that moment.

  • Example 33, x=[1,3,3]T\mathbf{x} = [1, 3, 3]^T, y=+1y = +1: output 0.50.5, error 0.50.5, correction 0.2⋅0.5⋅[1,3,3]=[0.1,0.3,0.3]0.2\cdot 0.5\cdot[1, 3, 3] = [0.1, 0.3, 0.3], so w=[0.6,0.3,0.3]T\mathbf{w} = [0.6, 0.3, 0.3]^T.
  • Example 22, x=[1,0,2]T\mathbf{x} = [1, 0, 2]^T, y=−1y = -1: output 0.6+0+0.6=1.20.6 + 0 + 0.6 = 1.2, error −1−1.2=−2.2-1 - 1.2 = -2.2, correction 0.2⋅(−2.2)⋅[1,0,2]=[−0.44,0,−0.88]0.2\cdot(-2.2)\cdot[1, 0, 2] = [-0.44, 0, -0.88], so w=[0.16,0.3,−0.58]T\mathbf{w} = [0.16, 0.3, -0.58]^T.
  • Example 11, x=[1,2,1]T\mathbf{x} = [1, 2, 1]^T, y=+1y = +1: output 0.16+0.6−0.58=0.180.16 + 0.6 - 0.58 = 0.18, error 0.820.82, correction 0.2⋅0.82⋅[1,2,1]=[0.164,0.328,0.164]0.2\cdot 0.82\cdot[1, 2, 1] = [0.164, 0.328, 0.164], so w=[0.324,0.628,−0.416]T\mathbf{w} = [0.324, 0.628, -0.416]^T.

At the end the outputs are 0.324+2⋅0.628−0.416=1.1640.324 + 2\cdot 0.628 - 0.416 = 1.164, 0.324−2⋅0.416=−0.5080.324 - 2\cdot 0.416 = -0.508 and 0.324+3⋅0.628−3⋅0.416=0.9600.324 + 3\cdot 0.628 - 3\cdot 0.416 = 0.960. The errors are −0.164-0.164, −0.492-0.492, 0.040.04, with squares 0.02690.0269, 0.24210.2421, 0.00160.0016, so Ein=0.2706/3≈0.0902E_{\text{in}} = 0.2706/3 \approx 0.0902: down from 0.91670.9167.

b) The epoch 1,2,31, 2, 3.

  • Example 11, x=[1,2,1]T\mathbf{x} = [1, 2, 1]^T, y=+1y = +1: output 0.50.5, error 0.50.5, correction 0.2⋅0.5⋅[1,2,1]=[0.1,0.2,0.1]0.2\cdot 0.5\cdot[1, 2, 1] = [0.1, 0.2, 0.1], so w=[0.6,0.2,0.1]T\mathbf{w} = [0.6, 0.2, 0.1]^T.
  • Example 22, x=[1,0,2]T\mathbf{x} = [1, 0, 2]^T, y=−1y = -1: output 0.6+0+0.2=0.80.6 + 0 + 0.2 = 0.8, error −1.8-1.8, correction 0.2⋅(−1.8)⋅[1,0,2]=[−0.36,0,−0.72]0.2\cdot(-1.8)\cdot[1, 0, 2] = [-0.36, 0, -0.72], so w=[0.24,0.2,−0.62]T\mathbf{w} = [0.24, 0.2, -0.62]^T.
  • Example 33, x=[1,3,3]T\mathbf{x} = [1, 3, 3]^T, y=+1y = +1: output 0.24+0.6−1.86=−1.020.24 + 0.6 - 1.86 = -1.02, error 2.022.02, correction 0.2⋅2.02⋅[1,3,3]=[0.404,1.212,1.212]0.2\cdot 2.02\cdot[1, 3, 3] = [0.404, 1.212, 1.212], so w=[0.644,1.412,0.592]T\mathbf{w} = [0.644, 1.412, 0.592]^T.

At the end the outputs are 0.644+2⋅1.412+0.592=4.0600.644 + 2\cdot 1.412 + 0.592 = 4.060, 0.644+2⋅0.592=1.8280.644 + 2\cdot 0.592 = 1.828 and 0.644+3⋅1.412+3⋅0.592=6.6560.644 + 3\cdot 1.412 + 3\cdot 0.592 = 6.656. The errors are −3.06-3.06, −2.828-2.828, −5.656-5.656, with squares 9.3649.364, 7.9987.998, 31.99031.990 (sum 49.3549.35), so Ein=49.35/3≈16.45E_{\text{in}} = 49.35/3 \approx 16.45: much larger than at the start.

c) Why the two epochs differ. After the update on example ii, the output on that example is wTxi+2η errori ∥xi∥2\mathbf{w}^T\mathbf{x}_i + 2\eta\,\text{error}_i\,\|\mathbf{x}_i\|^2, so its new error is errori−2η errori∥xi∥2=errori (1−2η∥xi∥2)\text{error}_i - 2\eta\,\text{error}_i\|\mathbf{x}_i\|^2 = \text{error}_i\,(1 - 2\eta\|\mathbf{x}_i\|^2) (computed here, it follows from the update rule). With ∥x∥2=6,5,19\|\mathbf{x}\|^2 = 6, 5, 19 and 2η=0.22\eta = 0.2 the factors are −0.2-0.2, 00 and −2.8-2.8. For examples 11 and 22 the error shrinks or vanishes; for example 33 the factor has absolute value larger than 11: the update overshoots and its error is multiplied by −2.8-2.8 (check: 0.5⋅(−2.8)=−1.40.5\cdot(-2.8) = -1.4, the output goes from 0.50.5 to 2.42.4). In the order 3,2,13, 2, 1 this happens at the start with a small error (0.50.5) and the two later updates, with factors 00 and −0.2-0.2, bring the errors back down. In the order 1,2,31, 2, 3 example 33 is last, with an error of 2.022.02 that has grown during the epoch, and the overshoot is not corrected within the epoch: the three errors end up between −2.8-2.8 and −5.7-5.7. So one epoch of SGD does not always lower EinE_{\text{in}}: the result depends on the order of the examples and on the size of η\eta compared with ∥x∥2\|\mathbf{x}\|^2 (the notes: the learning rate sets the step size, the result depends on where the steps start and how long they are). This is why the notebook shuffles the data at every epoch, and why a smaller η\eta is used there.

d) NumPy. The order 3,2,13, 2, 1 is the permutation of the indices [2,1,0][2, 1, 0] (counting from 00): X[rand_perm, :] is the matrix with the rows in the order 3,2,13, 2, 1 and y[rand_perm] the labels in the same order. With X of shape (3,3)(3, 3) and y, w of shape (3,)(3,), each step is error = yi - np.dot(xi, w) (a number, with xi of shape (3,)(3,)) and delta_w = eta * 2 * xi * error (shape (3,)(3,)). The loop makes N=3N = 3 updates per epoch; the batch version, delta_w = eta * 2 * np.dot(np.transpose(X), errors) / X.shape[0], makes one. The same permutation is applied to X and to y so that every row stays paired with its own label: shuffling only the rows would attach the labels to the wrong examples.

Typical mistakes: computing all three errors with the initial weights (the weights change after every example); forgetting the factor 22 of the derivative; using the weights of the end of the epoch in the errors; reading a larger EinE_{\text{in}} after an epoch as an arithmetic error instead of a too large step; shuffling the rows without the labels.

Self-assessment grid
item points what must be there for full points typical mistakes
Question 1 1.5 C: experience is the data, change in behaviour is the choice of the hypothesis swapping the roles; attributing adaptive behaviour to evolution only
Question 2 1.5 B: classification gives a class label, regression a real number, the material is supervised credit approval as regression; perceptron as regression; clustering developed in the lectures
Question 3 1.5 A: R2/γ2=900R^2/\gamma^2 = 900 updates at most R/γR/\gamma; reading the bound as exact; counting passes
Question 4 1.5 D: row by row products, [4, 3], same result for np.inner sign slip; thinking np.inner needs a square matrix; a single number
Question 5 1.5 A: odds 0.250.25, logit −1.386-1.386, class 00 inverted ratio; probability used as odds; positive logit
Question 6 1.5 C: 120×2120 \times 2, one row per object, classes of the nearby training points transposed matrix; one column per class; learned weight vector
Question 7 1.5 B: XTXX^TX and XTYX^TY, normal equations, [0.9,1.4][0.9, 1.4] swapped weights; ignoring the off-diagonal entries; wrong sign in the inverse
Question 8 1.5 D: θ^=3/4\hat\theta = 3/4, derivative +12+12 at 0.50.5 fraction of tails; missing factor −1-1; fair-coin estimate
Question 9 1.5 A: H0H_0 equal performance, t-test parametric, Wilcoxon non-parametric swapping the tests; H0H_0 as difference
Question 10 1.5 C: third argument of replace is the maximum number of replacements index of find; startswith; partition without separator
Problem 1 5 sums 3,4,03, 4, 0 and the firing decisions (1); w=[−3,2,−1,1,3]\mathbf{w} = [-3, 2, -1, 1, 3] and values 0,1,−30, 1, -3 (1); the five firing inputs and the counts 88 and 55 for thresholds 2.52.5 and 3.53.5 (1.5); thresholds of the two logic functions and the boundary x2=−x1+1.5x_2 = -x_1 + 1.5 (1.5) firing when the sum equals the threshold; w0=+thresholdw_0 = +\text{threshold}; missing constant input
Problem 2 5 XTXX^TX, XTYX^TY, w=[−1,3]\mathbf{w} = [-1, 3] (1.5); Ein=1E_{\text{in}} = 1 (0.5); Eout=3.16E_{\text{out}} = 3.16 with the contribution of each input (1.5); the 70% share of the inputs 4,54, 5, probability 0.7350.735, Eout=25E_{\text{out}} = 25 for the uniform probability and the comment (1.5) EoutE_{\text{out}} without the weights P(x)P(x); wrong counts in XTXX^TX; the same weight for every input
Problem 3 5 Ein≈0.9167E_{\text{in}} \approx 0.9167, the three updates with the weights [0.6,0.3,0.3][0.6, 0.3, 0.3], [0.16,0.3,−0.58][0.16, 0.3, -0.58], [0.324,0.628,−0.416][0.324, 0.628, -0.416] and Ein≈0.0902E_{\text{in}} \approx 0.0902 (2); the order 1,2,31, 2, 3 with Ein≈16.45E_{\text{in}} \approx 16.45 (1.5); factors −0.2-0.2, 00, −2.8-2.8 and the explanation (1); permutation [2,1,0][2, 1, 0], 33 updates against 11, same permutation (0.5) errors with the initial weights; missing factor 22; a larger EinE_{\text{in}} read as an arithmetic error; shuffling rows without labels
Total 30