




































































Prepara tus exámenes y mejora tus resultados gracias a la gran cantidad de recursos disponibles en Docsity
Gana puntos ayudando a otros estudiantes o consíguelos activando un Plan Premium
Esta página no es visible en la vista previa
¡No te pierdas las partes importantes!





































































CourseNotes
These notes have been made thanks to the effort of Polimi Data Scientists staff.
Are you interested in Data Science activities?
Polimi Data Scientist is a community of students and Alumni of Politecnico di Milano.
We organize events and activities related to Artificial Intelligence and Machine Learning, our aim is to create a strong and passionate community about Data Science at Politecnico di Milano.
Do you want to learn more? Visit our website and join our Telegram Group!!
− Learning the optimal policy − Training set: D = {〈x, u, x′, r〉} ⇒ π ∗ (x) = argmaxu{Q ∗ (x, u)} where Q*(x,u) must be estimated
− Markov Decision Process (MDP) − Partially Observable MDP (POMDP) − Stochastic Games (SG)
− Q-learning − SARSA − Fitted Q-iteration − ...
Part I
Supervised Learning
What happens if we enlarge the hypothesis space?
The loss of the best hypothesis will not increase and probably decrease. So the hypothesis space can be enlarged until the true model is inside of it, obtaining a loss value equal to zero.
If we don’t know f but we only have a few samples taken from it, the loss function is based not based on the function anymore, but on the samples. So, the optimal function h 2 based on the new loss function may be far from the true model and be farther as long as we enlarge the hypothesis space. If there are few samples, the loss function may be very noisy, and the more we enlarge the hypothesis space, the more we are sensitive to the noise. With infinite samples we can enlarge the hypothesis space without problems, because it’s like knowing f (no uncertainty).
We extend the class of models by considering linear combinations of fixed nonlinear functions (basis functions) of the input variables
y(x, w) = w 0 +
j=
wj φj (x) = wT^ φ(x)
where φ(x) = (1, φ 1 (x), ..., φM − 1 (x))T^. There are multiple examples of basis functions:
− (x−μj^ )
2 2 σ^2
They are not required to have a probabilistic interpretation, and in particular the normalization coefficient is unimportant because these basis functions will be multiplied by wj.
( (^) μj −x σ
)
If there is no linear relationship between the target and the input, it is possible to change the space through the basis function to obtain a linear model that can learn nonlinear functions of the input variable.
In this way the model is linear in the new feature space but nonlinear in the original space.
tp(t|x)dt
tp(t|x)dt
Given a data set with N samples, let us consider the following loss function
L(w) =
n=
(y(xn, w) − tn)^2
This is (half) the residual sum of squares (RSS), also known as sum of squares error (SSE). It can also be written as the sum of the ` 2 -norm^3 of the vector of the residual errors
RSS(w) = ||||^22 =
^2 i
Let’s write RSS in matrix form with Φ = (φ(x 1 ), ..., φ(xN ))T^ and t = (t 1 , ..., tN )T The residual = t − Φw, of size N × 1 − (N × M )(M × 1) = N × 1 Given that ||||^22 =
i
2 i =^
L(w) =
RSS(w) =
(t − Φw)T^ (t − Φw)
To obtain the minimum we need the point with:
We compute the first and second derivative
∂L(w) ∂w
= −ΦT^ (t − Φw);
∂^2 L(w) ∂w∂wT^
We can observe that, assuming ΦT^ Φ nonsingular, it is symmetric and positive semi-definite, so all the eigenvalues are ≥ 0. If some eigenvalues are = 0 we have infinite solutions.
−ΦT^ (t − Φw) = 0 ⇒ (ΦT^ Φ)−^1 ΦT^ Φw = (ΦT^ Φ)−^1 ΦT^ t ⇒ ΦT^ Φw = ΦT^ t
And we obtain wˆOLS = (ΦT^ Φ)−^1 ΦT^ t
It is not possible to perform the computation in two cases:
Figure 3.2: Graphical represen- tation of φ(xn) and ϕj
We consider an N -dimensional space whose axes are given by the tn, so that t = (t 1 , ..., tN ) is a vector in this space. Each basis function φ(xn) can be also represented as a vector in this space. Note that as we can see from Figure 3.2, ϕj corresponds to the jth column of Φ, whereas φ(xn) corresponds to the nth^ row of Φ. If the number M of basis functions is smaller than N, than the M vectors φ(xn) will span a linear subspace S of dimensionality M. Define ˆt as the N -dimensional vector whose nth^ element is given by y(xn, w). Because ˆt is an arbitrary linear combination ϕ 1 , ..., ϕM , it lies in an M -dimensional subspace S. The residual sum of squares is then equal to the squared Euclidean distance between ˆt and t. Thus the least-squares solution for w corresponds to that choice of ˆt that lies in the subspace S and that is closest to t. This solution corresponds to the orthogonal projection of t onto the subspace S.
ˆt = Φ ˆw = Φ(ΦT^ Φ)−^1 ΦT^ t
where H = Φ(ΦT^ Φ)−^1 ΦT^ is called hat matrix.
(^3) ||x||p = (∑ i∈N |xi|p)^1 /p
target variable. This can be done by finding the maximum likelihood, which is the probability of the target values given the assumed model. We can consider the log-likelihood function, which has the maximum value at the same point.
`(w) = ln p(t|X, w, σ^2 ) =
n=
ln p(tn|xn, w, σ^2 )
ln(2πσ^2 ) −
2 σ^2 RSS(w) = −
ln(2πσ^2 ) −
4 σ^2 (t − wΦ)T^ (t − wΦ)
Note that the first term does not depend on w, so it will disappear when we compute the gradient; the second term is basically the loss function, confirming that maximizing the likelihood is equivalent to minimizing the loss function.
∇ ln `(W) = −ΦT^ (t − Φw) = −ΦT^ t + ΦT^ Φw = 0 wM L = (ΦT^ Φ)−^1 ΦT^ t
Where (ΦT^ Φ)−^1 ΦT^ is a generalization of the inverse of a matrix for non-squared matrices (Moore-Penrose pseudo-inverse).
We assume that:
The variance-covariance matrix^4 of the least-squares estimate is:
V ar( ˆwOLS ) = (ΦT^ Φ)−^1 σ^2
Usually, the variance σ^2 is estimated by
σˆ^2 =
n=
(tn − wˆT^ φ(xn))^2
Assuming that the model is linear in the features φ 1 (), ..., φM () and that the noise is additive and Gaussian wˆ ∼ N (w, (ΦT^ Φ)−^1 σ^2 ) (N − M − 1)ˆσ^2 ∼ σ^2 χ^2 N −M − 1
Such properties can be used to form test hypothesis and confidence intervals.
Theorem (Gauss-Markov) The least squares estimate of w has the smallest variance among all linear unbiased estimates.
It follows that the least squares estimator has the lowest MSE^5 of all linear estimators with no bias (in which expected value = true value) However, there may exist a biased estimator with smaller MSE.
If we want to predict K > 1 target variables, denoted collectively by the target vector t, we could introduce a different set of basis functions for each component of t, leading to multiple, independent regression problems. Usually, a single set of basis functions is considered:
WˆM L ︸ ︷︷ ︸ M ×K
= (ΦtΦ)−^1 ΦT^ ︸︷︷︸T N ×K
For each output tk we have wˆ = (ΦT^ Φ)−^1 ΦT^ tk
where tk is an N -dimensional column vector. Thus the solution decouples between the different outputs, and we need only to compute a single pseudo-inverse matrix, which is shared by all of the vectors wk. (^4) It has variance on the diagonal and covariance on the rest of the matrix (^5) Mean Squared Error: ∑ n(y(xn,w)−tn)^2 N
Figure 3.3: ERM S on training and test sets
A model with low complexity is usually not capable of fitting the data and so representing appropriately the true model (under-fitting). A model with high complexity (e.g. high-order polynomials) gives excellent fit over the training data, but a poor representation of the true function (over-fitting). The goal is to achieve a good generalization and we obtain some quan- titative insight about that by reserving a part of the set for testing. By calculating the error also for the test set it is possible to evaluate the generalization. The root-mean-square error (RMS) is used:
2 ∗ RSS( ˆw) N
In which the division by N allows to compare sets of different sizes and the square root ensures that the error is based on the same scale as the target variable. As we can see from Figure 3.3, from a specific value of the model complexity, even if the training error continues decreasing, ato some point the test error starts to increase. That’s because there is over-fitting and the model has huge values for the parameters in order to try to fit all the points of the training set, as showed in Figure 3.4.
Figure 3.4: Table of the coefficients w* for polynomials of various order.
For a given model complexity, the over-fitting problem becomes less severe as the size of the data set increases. In other words, the larger the data set, the more complex (and flexible) the model we can afford to fit the data.
Regularization involves adding a penalty term to the error function to discourage the coefficients from reaching large values and so, to make the curve smoother.
L(w) = LD (w) + λLW (w)
Where LD (w) is the empirical loss on data (e.g. RSS) and LW (w) is the measure of the size of the parameters (the model complexity). By taking LW (w) = 12 wT^ w = 12 ||w||^22 we obtain the ridge regression (or weight decay)
L(w) =
i=
(ti − wT^ φ(xi))^2 +
λ 2
||w||^22
Note that if there are a lot of samples, the regularization is useless and it may even worsen the performance of the model. The loss function is still quadratic in w:
wˆridge = (λI + ΦT^ Φ)−^1 ΦT^ t
or posterior ∝ likelihood · prior
Where all of these quantities are viewed as functions of w. The aim is to obtain the most probable value of w given the data (maximum a posteriori or MAP), which is the mode of the posterior.
If new data is available, it is possible to use the posterior value as prior to compute a new poste- rior. This sequential approach of learning depends only on the assumption of i.i.d^6 data. It can be useful in real-time learning scenarios o in case of large datasets, because they do not require the whole dataset to be stored or loaded into memory. It is important that the prior and posterior have the same distribution. This fact leads to introducing the concept of conjugate priors: for a given probability distribution, we can seek a prior that is conjugate to the likelihood function, so that the posterior has the same distribution as the prior. For example the prior of a Gaussian is a Gaussian, and the prior of a Beta is a Bernoulli (so it gives another Beta as posterior).
Prediction for a new data point x* (given the training dataset D) can be done by integrating over the posterior distribution:
p(x*|D) =
p(x|w, D)p(w|D)dw = E[p(x|w, D)]
which is sometimes called predictive distribution. Note that computing the predictive distribution requires knowledge of the posterior distribution, which is usually intractable.
In the Bayesian approach the parameters of the model are considered as drawn from some distribution. Assuming a Gaussian likelihood model, the conjugate prior is Gaussian too
p(w) = N (w|w 0 , S 0 )
Given the data D, the posterior is still Gaussian:
p(w|t, Φ, σ^2 ) ∝ N (w|w 0 , S 0 )N (t|Φw, σ^2 IN ) = N (w|wN , SN )
where
wN = SN
− 1 ︸ ︷︷ ︸^0 w^0 from prior
ΦT^ t ︸ ︷︷ ︸^ σ^2 from data
σ^2
In a Gaussian distribution the mode coincides with the mean. It follows that wN is the MAP estimator. In many cases we may have little idea of what form the distribution should take. We may then seek a form of prior distribution, called a noninformative prior, which is intended to have as little influence on the posterior distribution as possible. In this case the value S 0 → ∞ and so
σ^2
⇒ SN = σ^2 (ΦT^ Φ)−^1 ⇒ wN = σ^2 (ΦT^ Φ)−^1
ΦT^ t σ^2
= (ΦT^ Φ)−^1 ΦT^ t
so, wN reduces to the ML estimator. If w 0 = 0 and S 0 = τ 2 I, then wN reduces to the ridge estimate, where λ = σ
2 τ 2 (^6) independent and identically distributed
We are interested in the posterior predictive distribution, which is the distribution over the output variable obtained taking into account all the models, each one with its density.
p(t|x, D, σ^2 ) =
N (t|wT^ φ(x), σ^2 )
probability ︷ of the model︸︸ ︷ N (w|wN , SN ) dw
= N (t|wTN φ(x), σ^2 N (x))
where σ^2 N (x) = (^) ︸︷︷︸σ^2 noise in the target values
In the limit, as N → ∞, the second term goes to zero. The variance of the predictive distribution arises only from the additive noise governed by parameter σ.
Figure 3.5: Example of the predictive distribution. The pink area will never go to zero for the intrinsic noise in the samples, even asymptotically
n(ˆtn^ −^ tn)
(^2) , representing how much of the prediction differs from the true value.
RSS(w) N , representing approximately how much error we get on a predicted data over the training set (i.e. a normalized version of the RSS.
(^7) ¯t = ∑ n tn N