Scarica Riassunti Machine Learning e più Sintesi del corso in PDF di Machine learning solo su Docsity!
Machine Learning
NOTES
Giacomo Varini
LEZIONE 2 – INTRODUCTION TO SUPERVISED LEARNING
We suppose that there exists an unknown function that maps an input 𝑥 to an output 𝑡 Goal: learn a good approximation of 𝑓
- Input variables 𝑥 are usually called features or attributes
- Output variables 𝑡 are also called targets or labels Tasks:
- Classification if 𝑡 is discrete
- Regression if 𝑡 is continuous
- Probability estimation if 𝑡 is a probability When supervised learning?
- When human cannot perform the task (e.g., DNA analysis)
- When human can perform the task but cannot explain how (e.g., medical image analysis)
- When the task changes over time (e.g., stocks price prediction)
- When the task is user-specific (e.g., movie recommendation) We want to approximate a function 𝑓 given a data set 𝒟 The steps are:
- Define a loss function L
- Choose the hypothesis space H
- Find in H an approximation h of 𝑓 that minimizes L NB: If we enlarge the hypothesis space we can approximate 𝑓 without error, but... Supervised learning can be:
- Parametric: fixed and finite number of parameters
- Nonparametric: the number of parameters depends on the training set
- Frequentist: estimate model parameters
- Bayesian: use prior probabilities to model uncertainty about the estimates
- Empirical Risk Minimization: minimize error over the training set
- Structural Risk Minimization: balance training error with model complexity We distinguish between 3 different approaches:
- Direct approach: learn directly an approximation of 𝑓 form 𝒟
- Generative approach: learns the joint probability distribution 𝑝(𝑥, 𝑡) and infer conditional density 𝑝(𝑡|𝑥)
- Discriminative approach: learns directly the conditional probability distribution 𝑝(𝑡|𝑥)
Closed-form optimization (OLS) is not feasible with large dataset Apply gradient descent Least Mean Square (LMS) algorithm: Multiple Outputs What happens if our regression problem has multiple outputs, i.e., t is not scalar? It is possible to use the same set of basis functions: Regularization Models with low complexity will not fit training data very well → under-fitting Models with high-complexity will fit training data very well but the model will not behave well on unseen data → over-fitting The evaluation is usually done computing the error of the model on a test set RMSE vs Model Complexity (order)
How do we avoid over-fitting?
- Model selection: choose the proper model
- Rely on very large training set
- Regularization : account for model complexity in the loss function Over-fitting is always associated with extremely large weight estimates! How do we extend the loss function?
- L_D(𝑤) is the usual loss function (e.g., RSS)
- L_𝑤(𝑤) accounts for model complexity penalizing weights’ dimensions
- 𝜆 is the regularization coefficient How do we design L𝑤(𝑤)? Ridge Regression The loss function is still quadratic with respect to 𝑤 and closed-form optimization is still possible: What happens to the weights when lambda increases? Lasso Regression In this case, closed-form optimization is not possible! Lasso typically leads to sparse regression models: when regularization coefficient 𝜆 is large enough, some estimates of the weights become equal to zero.
- Observed data 𝒟 is used to estimate the parameters 𝑤 We want the most probable value of w given the data, i.e., the mode of the posterior, also known as maximum a posteriori (MAP). How to model the prior? A conjugate prior is the most convenient choice: As a result, the posterior is still Gaussian! When posterior is gaussian the MAP is equal to the mean. NB : When prior is infinitely broad MAP is equal to ML solution. The ML estimate of w has the smallest variance and the lowest MSE among linear unbiased estimates. NB : MAP is equivalent to the solution of ridge regression with How to exploit the Bayesian approach for sequential learning?
- We compute posterior with initial data
- When additional data is available, the posterior becomes the prior With a Bayesian framework, it is possible to compute the probability distribution of the target for a new sample x∗ by integrating over the posterior distribution: This is often called predictive distribution. Unfortunately, computing the predictive distribution requires knowledge of the posterior distribution, which is usually intractable! In our example (with normal likelihood and conjugate prior) we can compute the predictive distribution analytically:
NB : when 𝑁 → ∞ the uncertainty associated to parameters (second term) goes to zero and the variance of predictive distribution depends only on variance of data. Observations:
- Analytical integreation is possible only using conjugate priors and works for simple models
- Approximated approaches are used in the more general case (e.g. Monte Carlo integration) The prior should not give zero or small probabilities to possible values, but at the same time also avoid to be uninformative. solution: introduce hyperparameters into the prior Linear models with fixed basis functions have several advantages:
- Allow closed-form solution
- Tractable Bayesian treatment However, they have also several limitations:
- Basis functions are not adaptive with respect to the training data
- These models suffer of the curse of dimensionality
LEZIONE 4 – LINEAR CLASSIFICATION
What is classification? Learn, from a dataset 𝒟, an approximation of function 𝑓(𝑥) that maps input 𝑥 to a discrete class 𝐶_k (with k = 1,...,𝐾). How do we model 𝑓? How do we encode 𝐶_k? How do we evaluate our approximation? How do we optimize our approximation? Classification approaches Discriminant function
- Model a parametric function that maps input to classes
- Learn parameters from data Probabilistic discriminative approach
- Design a parametric model of 𝑝(𝐶_k|x)
- Learn model parameters from data Probabilistic generative approach
- Model 𝑝(x|𝐶k) and class priors 𝑝(𝐶𝑘)
- Fit models to data
- Map x to class 𝐶_k if 𝑦_k > 𝑦_j ∀ 𝑗 != 𝑘
- No ambiguity
- DS are singly connected and convex Basis Functions So far we considered models that work in the input space (i.e., with x) However, we can still extend models by using a fixed set of basis function 𝜙(x) Basically, we apply a non-linear transformation to map the input space into a feature space As a result, decision boundaries that are linear in the feature space would correspond to nonlinear boundaries in the input space This allows to apply linear classification models also to problem where samples are not linearly separable Example: using two Gaussian basis functions Least Squares for Classification Let consider a K-class problem:
Problems with Least Squares
- Outliers
- Output Distribution Least Squares work under the assumption that the output follows a Gaussian distribution! Perceptron The perceptron is a linear discriminant model proposed by Rosenblatt Perceptron is devised for a two-class problem, where classes encoding is {-1,1}
- The perceptron algorithm aims at finding a decision surface (also called separating hyperplane ) by minimizing the distance of misclassified samples to the boundary
- Minimization of this loss function can be performed using stochastic gradient descent
- Despite a simpler loss function could be used in principle (e.g., number of misclassified samples), this are more complex to minimize The Perceptron Criterion is defined as: Samples clssified correctly do not contribute to L 𝐿_𝑃 can be minimized using stochastic gradient descent: Since the scale of w does not change the perceptron function, the learning rate 𝛼 is set to 1
A convenient loss function to minimize is the negative log-likelihood (also known as cross-entropy error function): Now we have to compute the derivative of 𝐿(w): The gradient of the loss function is: Due to the nonlinear logistic regression function it is not possible to find a closed-form solution. However, the error function is convex and gradient-based optimization can be applied (also in an online learning setting). Multiclass Problem In multiclass problems, 𝑝(𝐶_k|𝝓) is modeled by a softmax transformation of the output of K linear functions (one for each class): We can compute likelihood as: As for the two-class problem, we can minimize the cross-entropy error function: Then, we compute the gradient for each weight vector: Observation: if we replace the sigmoid function with a step function, logistic regression leads to the same updating rule of perceptron algorithm
LEZIONE 5 – MODEL EVALUATION, SELECTION AND
ENSEMBLES
Is the loss function 𝐿 computed on 𝒟 a good way to evaluate a model? No! This is the performance measure we want to compute: Bias-Variance Decomposition The Bias-Variance is a framework to analyze the performance of models Model Variance If we sample several datasets 𝒟 we will learn different model y(x) Variance measures the difference between each model learned from a particular dataset and what we expect to learn:
- Decreases with simpler models
- Decreases with more samples Model Bias Bias measures the difference between truth (𝑓) and what we expect to learn (𝔼[𝑦(x)]):
- Decreases with more complex models NB: Data noise (𝜎^2) is the variance of data and does not depend neither from data sampling nor from the model complexity.
So what do we do in practice? Test Error
- Split randomly data into a training set and test set
- Estimate model parameters using the training set
- Estimate the prediction error using the test set The analysis of train-test errors allows to identify possible problems
- high bias: training error is close to test error but they are both higher than expected
- high variance: training error is smaller than expected and it slowly approaches test error Test error cannot be used for model selection → we can overfit test set Only if test set is never used for training and model selection, it can provide an unbiased estimate of prediction error! So how to perform model selection? We split data in three parts:
- Training Data
- Validation Data
- Test Data We use training data to learn model parameters For each model learned we use validation data to compute the validation error We select the model with the lowest validation error and finally use test data to estimate prediction error BUT to be reliable, validation data should be enough! → less training data Attention: we might overfit validation data and eventually not choose the best model Leave-One-Out Cross Validation (LOO) For each sample {x𝑖, 𝑡𝑖} ∈ 𝒟:
- We train the model on 𝒟{x𝑖,𝑡𝑖}
- We compute the error of the resulting model on {x𝑖,𝑡𝑖} The estimate of the prediction error of our model will be the average of all the error computed using a single sample:
Where 𝑦𝒟𝑖 is the model trained on 𝒟{x𝑖,𝑡𝑖} NB: 𝐿𝐿𝑂𝑂 provides an almost unbiased estimate of prediction error (slightly pessimistic) Unfortunately, LOO is extremely expensive to compute! K-Fold Cross Validation We randomly split the training data 𝒟 into 𝑘 folds: 𝒟1, ... , 𝒟𝑘 For each fold 𝒟𝑖:
- We train the model on 𝒟\𝐷i
- We compute the error on 𝐷i Finally, we estimate the prediction error as the average error computed: 𝐿_𝑘−𝑓𝑜𝑙𝑑 provides a slightly biased estimate of prediction error (pessimistic) but it is much cheaper to compute (usually 𝑘= 10 is used). Complexity-Adjusted Model Evaluation Other metrics are used to evaluate models adjusting their training error based on their complexity: Attention:
- AIC and BIC are generally used when maximizing the log-likelihood
- BIC generally penalize more than AIC model complexity «No Free Lunch» Theorems
- Let 𝐴𝐶𝐶_g(𝐿) be the generalization accuracy of learner 𝐿, i.e., the accuracy of L on samples that are not in the training set
- Let F be the set of all the possible concepts 𝑦 = 𝑓(x) For any learner L and any possible training set: Corollary For any two learners, L 1 and L 2 , if ∃𝑓 where ACC_g(L 1 ) > ACC_g(L 2 ) then ∃𝑓’ where ACC_g(L 2 ) > ACC_g(L 1 )
Dimensionality Reduction Dimensionality reduction aims at reducing the dimensions of input space, but it differs from feature selection in two major aspects:
- it uses all the features and maps them into a lower-dimensionality space
- it is an unsupervised approach PCA is the most used technique to perform dimensionality reduction PCA Steps: Limitations:
- Explains just a part of the variance of the data higher the variance, better the reconstruction
- PCs are linear combination of features
- Fails when data is distributed in multiple clusters
- It is not always the best data representation for regression/classification Bagging and Boosting So far we saw how to reduce the variance, searching the best tradeoff with the increased bias NOW is it possible to reduce variance without increasing bias? Is it possible to reduce bias? We can achieve these results by using two ensemble methods that consist of learning several models and combine them Bagging Assume to have N datasets and to learn from them N models 𝑦_ 1 , … , y_N Compute an aggregate model as 𝑦_agg = 1/𝑁 sum(𝑦_i) If the datasets are independent the model variance of y_agg is 1/N of the model variance of the single y_i In general we don’t have N independent datasets. What can we do? Bootstrap Aggregation
- Generate N datasets applying random sampling with replacement
- Train a model (classification or regression model) from each dataset generated
- Compute the prediction for new samples applying all the trained models and combining the outputs with majority voting (classification) or averaging (regression) Bagging is generally helpful and reduce variance, although sampled datasets are not indep
- It helps with unstable learners, i.e., learners that change significantly with even small changes in the dataset (low bias and high variance)
- It helps when we have a lot of overfitting (low bias and high variance)
- It does not help when learners is robust, i.e., not sensitive to change in data (usually higher bias but lower variance)
Boosting The goal of boosting is to achieve a small bias by using sequential (weak) learners At the same time, using weak learners, aims at keeping a small variance Steps:
- Give an equal weight to all the samples in the training set
- Train a weak learner on the weighted training set
- Compute the error of the trained model on the weighted training set
- Increase the weights of samples missclassfied by the model
- Repeat from 2 until some criteria is met The ensemble of models learned can be applied on new samples by computing the weighted prediction of each model (more accurate models weight more)
- What weak learners? Decision stumps, decision trees…
- How to compute weights? See AdaBoost AdaBoost Comparison
LEZIONE 6 – COMPUTATIONAL LEARNING THEORY
It aims at studying the general laws of inductive learning, by modeling:
- Complexity of hypothesis space
- Bound on training samples
- Bound on accuracy
- Probability of succesfull learning This allows to answer to questions like: