Docsity

Introduction to machine learning - full course, Sintesi del corso di Machine learning

Toggle sidebar
bg1
1
INTRODUCTION TO MACHINE LEARNING
BASIC CONCEPTS 2
WHAT IS MACHINE LEARNING? 2
DATA AND FEATURES 2
TYPES OF LEARNING 2
KEY INGREDIENTS 3
TASK 4
DATA 4
MODEL HYPOTHESIS 4
OBJECTIVE FUNCTION 4
LEARNING ALGORITHM 5
K-NEAREST NEIGHBORS 6
THE GENERAL IDEA 6
DECISION BOUNDARIES 7
PERFORMANCE 7
PERCEPTRON 8
LINEAR MODELS 8
THE PERCEPTRON LEARNING ALGORITHM 9
MULTICLASS CLASSIFICATION 10
GRADIENT DESCENT 11
LOSS FUNCTIONS 11
GRADIENT DESCENT ALGORITHM 12
REGULARIZATION 13
SUPPORT VECTOR MACHINES 14
LARGE MARGIN CLASSIFIERS 14
SOFT MARGIN CLASSIFICATION 14
THE DUAL PROBLEM 15
NON-LINEAR SVM 15
RANKING 16
MULTICLASS AND MULTILABEL 16
THE RANKING LEARNING ALGORITHM 16
DECISION TREES 18
DECISION TREE LEARNING ALGORITHM 18
RANDOM FOREST LEARNING ALGORITHM 20
NEURAL NETWORKS 21
FROM PERCEPTRON TO DEEP NETWORKS 21
FEEDFORWARD NEURAL NETWORKS 22
BACKPROPAGATION 24
TRAINING A NEURAL NETWORK 24
CONVOLUTIONAL NEURAL NETWORKS 25
OTHER NEURAL NETWORKS 26
UNSUPERVISED LEARNING 27
TASKS 27
DIMENSIONALITY REDUCTION 27
CLUSTERING 29
DENSITY ESTIMATION 31
REINFORCEMENT LEARNING 33
THE GENERAL IDEA 33
MARKOV DECISION PROCESS 34
VALUE-BASED METHODS 34
DEEP Q-LEARNING 35
POLICY GRADIENT METHOD 36
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24

Anteprima parziale del testo

Scarica Introduction to machine learning - full course e più Sintesi del corso in PDF di Machine learning solo su Docsity!

INTRODUCTION TO MACHINE LEARNING

  • BASIC CONCEPTS
  • WHAT IS MACHINE LEARNING?
  • DATA AND FEATURES
  • TYPES OF LEARNING
  • KEY INGREDIENTS
  • TASK
  • DATA
  • MODEL HYPOTHESIS
  • OBJECTIVE FUNCTION
  • LEARNING ALGORITHM
  • K-NEAREST NEIGHBORS
  • THE GENERAL IDEA
  • DECISION BOUNDARIES
  • PERFORMANCE
  • PERCEPTRON
  • LINEAR MODELS
  • THE PERCEPTRON LEARNING ALGORITHM
  • MULTICLASS CLASSIFICATION
  • GRADIENT DESCENT
  • LOSS FUNCTIONS
  • GRADIENT DESCENT ALGORITHM
  • REGULARIZATION
  • SUPPORT VECTOR MACHINES
  • LARGE MARGIN CLASSIFIERS
  • SOFT MARGIN CLASSIFICATION
  • THE DUAL PROBLEM
  • NON-LINEAR SVM
    • RANKING
    • MULTICLASS AND MULTILABEL
    • THE RANKING LEARNING ALGORITHM
    • DECISION TREES
    • DECISION TREE LEARNING ALGORITHM
    • RANDOM FOREST LEARNING ALGORITHM
    • NEURAL NETWORKS
    • FROM PERCEPTRON TO DEEP NETWORKS
    • FEEDFORWARD NEURAL NETWORKS
    • BACKPROPAGATION
    • TRAINING A NEURAL NETWORK
    • CONVOLUTIONAL NEURAL NETWORKS
    • OTHER NEURAL NETWORKS
    • UNSUPERVISED LEARNING
    • TASKS
    • DIMENSIONALITY REDUCTION
    • CLUSTERING
    • DENSITY ESTIMATION
    • REINFORCEMENT LEARNING
    • THE GENERAL IDEA
    • MARKOV DECISION PROCESS
    • VALUE-BASED METHODS
    • DEEP Q-LEARNING
    • POLICY GRADIENT METHOD

Basic concepts

What is Machine Learning?

Machine learning is a branch of AI that tries to create models learning from the experience.

data →

result →

machine learning → program

This paradigm is necessary because sometimes conventional programming doesn’t work!

In particular: data – appropriate algorithm → model (used on future data)

So ML is about predicting the future based on the past, i.e. we learn a model from training data and then

we apply the model to testing data to make a prediction.

We can also say that ML is the study of algorithms that improve their performance P at some task T with

experience E.

Artificial intelligence → engineering of making intelligent machines and programs.

Machine learning → ability to learn without being explicitly programmed.

Deep learning → learning based on Deep Neural Networks → learning over different stages (layers of

nodes between input and output) and representing data through multiple levels of abstraction.

Data and features

Data is a set of examples.

A sample is an unbiased number of observations that is collected.

We need to map data into features, that are generally represented with vectors.

Feature → question about the examples, possible characteristic that describe them.

There are difficulties in choosing the “right” features for the given problem i.e. feature descriptor that

lose the minimum of information.

Feature descriptor is a method that extracts the features.

Learning is about generalizing from the training data!

There is some unknown probability distribution over examples called the data generating distribution.

Both the training and the test set are generated based on this distribution.

A probability distribution describes how likely certain events are.

The learning process:

  • Measuring devices → collect data and represent them
  • Preprocessing → noise filtering, feature extraction, normalization
  • Dimensionality reduction → feature selection and projection
  • Model learning → model depends on the problem and on the algorithm
  • Model testing loop → validate the model by accuracy to find the best parameters

Types of learning

SUPERVISED LEARNING → given data with associated labels, build a model that predict the label of a new

example.

  • Classification problem → predict discrete labels

Given a training set 𝒯 = {(𝑥

1

1

𝑚

𝑚

)}, learn a function 𝑓 (model) to predict 𝑦 given 𝑥

such that 𝑓: ℝ

𝑑

𝑦 is categorical (can take on one of a limited, and usually fixed, number of possible values)

Task

A task represents the type of prediction or inference being made to solve a problem on some data.

A task can be identified with ℱ task

, the set of functions that can potentially solve it.

In general it consists of mappings from an input space 𝒳 to an output space 𝒴.

In classification we need to find a function 𝑓 ∊ 𝒴

𝒳

assigning each input 𝑥 ∈ 𝒳 a discrete label, i.e.

1

𝑘

} where 𝑘 is finite.

In regression we need to find a function 𝑓(𝑥) ∊ 𝒴 assigning each input a continuous label.

In particular 𝒴 = ℝ

𝑑

, where 𝑑 is the number of continuous values to predict (when 𝑑 > 1 we talk about

multi-output regression).

Density estimation is about describing the distribution of data points. So we need to find a probability

distribution 𝑓 ∈ 𝛥(𝒳) that fits the data 𝑥 ∈ 𝒳.

In clustering we need to find a function 𝑓 ∈ ℕ

𝒳

that assigns each input 𝑥 ∈ 𝒳 a cluster index 𝑓(𝑥) ∈ ℕ.

All points mapped to the same index form a cluster.

In dimensionality reduction we need to find a function 𝑓 ∊ 𝒴

𝒳

mapping each (high dimensionality) input

𝑥 ∈ 𝑋 to a lower dimensional embedding 𝑓(𝒳) ∈ 𝒴, where dim(𝒴) ≪ dim(𝒳).

Data

Data is information about the problem to solve and is in the form of a distribution, 𝑝

data

This distribution in supervising learning is 𝑝 data

∈ 𝛥(𝒳 × 𝒴), while in unsupervised learning is 𝑝

data

The data distribution 𝑝 data

is typically unknown but we can sample from it → larger training set better

model.

The underline assumption for any ML algorithm to work is that training set, validation set and test set

are sample from the same distribution!

Training set is the part of data that the model actually sees and is used to train it.

Validation set is the part of data that is used to do a frequent evaluation of the model, fit on the training

dataset along with improving involved hyperparameters (initially set parameters before the model

begins learning).

Testing set is the data feeded to the model for predict values (without seeing actual output).

Model hypothesis

A model is like a “program” to solve the problem.

It’s the implementation of a function 𝑓 ∈ ℱ

task

that can be tractably computed.

A set of models forms a hypothesis space ℋ ⊂ ℱ task

The learning algorithm seeks a solution within the hypothesis space.

Objective function

We want to minimize the generalization error function 𝐸(𝑓; 𝑝 data

), i.e. search the best 𝑓

∗

arg min

𝑓∈ℱ

task

data

The error function determines how well a solution 𝑓 ∈ ℱ task

fits some given data.

We cannot search too much → need to restrict the focus on finding functions that can be implemented

and evaluated in a tractable way.

We define a model hypothesis space ℋ ⊂ ℱ task

and seek a solution within that space, i.e. 𝑓

ℋ

∗

arg min

𝑓∈ℋ

data

But we don’t have access to 𝑝 data

→ need to work on data samples i.e. training set 𝒟

𝑛

1 ,⋯

𝑛

where 𝑧 𝑖

data

. So we need to find 𝑓

ℋ

∗

𝑛

) ∈ arg min

𝑓∈ℋ

𝑛

Typically the generalization and training error functions can be written it terms of a pointwise loss

ℓ(𝑓; 𝑧) measuring the error incurred by 𝑓 on the training example 𝑧, i.e. 𝐸(𝑓; 𝑝

data

𝑧~𝑝

data

[ℓ(𝑓; 𝑧)].

We need to implement the error function: 𝐸(𝑓; 𝒟

𝑛

1

𝑛

𝑖

𝑛

𝑖= 1

The pointwise loss is a discrepancy measure between the ground truth and the output that is computed

given the function 𝑓 → we can choose any reasonable measure.

Learning algorithm

The learning algorithm solves the optimization problem targeting 𝑓 ℋ

∗

𝑛

) but this could be very difficult

(e.g. saddle points, local minimum).

We need to use a training error as a proxy for the generalization error.

The ideal situation is being able to learn a model that works decently both on training and validation.

Overfitting → you have a function that learns extremely well on training data but works poorly on test

data → is not able to generalize.

Underfitting → large training error.

The hypothesis space has a strong impact on how well we can do.

Approximation error → error that results from reducing my hypothesis space.

Irreducible error → it’s an error that we cannot reduce by choosing a better model, it’s due to

randomness or natural variability in a system. There are deterministic factors that we don’t observe, but

which could be interpreted as inherent variability.

Model complexity matters for overfitting!

Distance → numerical measure of how different two data objects are, where the minimum is often 0 and

the upper limit varies.

A more general measure of distance is the Minkowski distance 𝐷(𝑎, 𝑏) = (∑ |𝑎

𝑘

𝑘

𝑟

𝑛

𝑘= 1

1

𝑟

⁄

where 𝑛 is

the number of dimensions and 𝑟 is a hyper-parameter that has implication on what neighbor is.

In the case of 𝑟 = 1 we talk about Manhattan or L1 norm distance, while in the case of 𝑟 = 2 we’re

referring to Euclidian distance.

The Euclidian and the Minkowski distances are very good metrics when we operate in the vector space,

but in some specific cases there are better options.

When analyzing documents it’s preferred to use the cosine similarity cos(𝑑

1

2

1

2

1

2

This because, after represented each document as a vector indicating how many times a word is present

inside it, it’s easy to say if two documents are discussing the same topic just by looking the angle in

between.

Decision boundaries

Decision boundaries → places in the feature space where the classification of a point/example changes.

Decision boundaries of k-NN are locally defined and derive from the Voronoi partitions.

Voronoi diagram describes the areas that are nearest to any given point, given a set of data. Each line

segment is equidistant between two points.

K-NN algorithm does not explicitly compute decision boundaries. The decision boundaries form a subset

of the Voronoi diagram for the training data. The more examples that are stored, the more complex the

decision boundaries can become.

The decision boundary change with 𝑘 → larger 𝑘 produces smoother boundary effect but cannot be too

big respect to the size of the training set (e.g. when 𝑘 = 𝑛 always predict the majority class).

How to pick 𝑘 :

  • Choose an odd number to avoid ties (often 3, 5, 7)
  • Use validation set
  • Use cross-validation
  • Rule of thumb is 𝑘 <

𝑛, where 𝑛 is the number of training examples

Cross-validation → technique for evaluating a model and testing its performance (e.g. detect

overfitting) when it’s not given a validation set. It consists in dividing the given dataset in two parts: one

for training and other for validation. Then the model its trained and validated. These steps are done

multiple times.

Performance

If we choose as class the majority class within the 𝑘 nearest neighbors, this means that all the samples

weigh the same → suboptimal.

A better version is weighted k-NN → the samples don’t have the same importance, but each sample has a

weight assigned.

The weight is inversed proportional to the distance of the points respect to the test sample → this is one

way to compute the weights through exponential decay (of the distance).

K-NN is belongs to the class of lazy learning algorithms.

Lazy learning → simply stores training data (or perform limited preprocessing) and operates when it is

given a test example.

Eager learning → given a training set, constructs a classification model before receiving new test data to

classify.

K-NN can operate in any dimensions, but it’s severely affected to curse of dimensionality.

Curse of dimensionality → in high dimensions almost all points are far away from each other.

The success of k-NN is very dependent on having a dense dataset, where points are close in every single

dimension. There isn’t any procedure to assign more importance to some dimensions rather than others,

so a big dataset is needed.

In practical applications is very rare to use k-NN for very high dimensional problem because inference

time matters!

A solution could be put some preprocessing like feature selection mechanism or dimensionality

reduction mechanism, before applying k-NN.

Computational cost:

  • Linear algorithm (no pre-processing):
    • Compute distance for all 𝑛 datapoints
    • Complexity of distance computation: 𝑂(𝑘𝑛) if the number of dimensions is relatively

small

  • No additional space needed (compute on the-fly)
  • Tree-based data structures (with pre-processing)
  • Allocate some memory to be faster at query time

K-dimensional tree → space-partitioning data structure for organizing points in a 𝑘-dimensional space.

To build a k-d tree from training data we have to pick a random dimension, find the median, split the

data and repeat.

To find the nearest neighbors for a new point we need to find the region containing the point and then

compare to all points in the region.

K-NN advantages:

  • Easy to program
  • No optimization or training required
  • Classification accuracy can be very good, it can outperform more complex models

K-NN issues:

  • Choose distance measure (most common is Euclidean)
  • Choosing 𝑘
  • Curse of dimensionality
  • Memory based technique (can be prohibitive for large data set)

Perceptron

Linear models

ML approaches can be categorized in:

  • Models that make strong assumptions → if the assumptions are correct it can lead to better

performance e.g. learn even with a few training data

  • Models don’t make assumptions → allow to learn from varied (flexible!) data but are more prone

to overfitting and require more training data

Where do these assumptions come from? Sometimes domain knowledge.

In k-NN there aren’t model assumptions → k-NN learns everywhere i.e. there aren’t data sets that could

never be learned (suboptimal ≠ can’t learn).

Note that model assumptions (e.g. k-NN none) are different from algorithm design assumptions (e.g. in

k-NN proximity relates to class)!

Bias of a model → how strong the model assumptions are.

Classify models:

  • Low bias → make minimal or no assumptions about the data
  • High bias → make strong assumptions about the data

A linear model is a (high-bias) model that assumes the data is linearly separable (by a line in 2

dimensions or by hyperplanes in 𝑛-dim).

Multiclass classification

Binary classification → given an input space 𝒳, an unknown distribution 𝒟 over 𝒳 × {− 1 , + 1 }, and a

training set D sampled from 𝒟, the goal is to compute a function 𝑓 minimizing 𝔼

(𝑥,𝑦)~𝒟

[
]

Multiclass classification → given an input space 𝒳 and number of classes 𝑘, an unknown distribution 𝒟

over 𝒳 × [

]

, and a training set D sampled from 𝒟, the goal is to compute a function 𝑓 minimizing

(𝑥,𝑦)~𝒟

[𝑓(𝑥) ≠ 𝑦].

So multiclass classification is a natural extension of binary classification where the difference is that we

have 𝑘 > 2 classes to choose the labels from.

Most real-world applications involve multiclass predictions!

The k-NN natively can be applied to multiclass classification problem, while the perceptron cannot. To

apply it to multiclass classification problem we need an external procedure that combines multiple lines.

One vs all (OVA) means that we want to learn 𝑘 hyperplane (𝑘 = #labels) and for each label 𝑖 we define

a binary problem: we assign + 1 to the samples of the class 𝑖 and − 1 to the other examples. We repeat

this procedure 𝑘 times i.e. for each class we relabel the training set.

OVA training algorithm:

for 𝑖 = 1 to 𝑘

𝑏𝑖𝑛

← relabel 𝐷

𝑚𝑢𝑙𝑡𝑖𝑐𝑙𝑎𝑠𝑠

so class 𝑖 is positive and ¬𝑖 is negative

𝑖

← BinaryTrain(𝐷

𝑏𝑖𝑛

return 𝑓

1

𝑘

In general classifiers provide confidence: when classifying a sample we pick the most confident positive

or the least confident negative if there aren’t positive votes.

If classifier doesn’t provide confidence and there’s ambiguity, we can simple pick one of the ones in

conflict.

The confidence is the distance from the hyperplane: 𝑝𝑟𝑒𝑑𝑖𝑐𝑡𝑖𝑜𝑛 = 𝑏 + ∑ 𝑤 𝑖

𝑖

𝑛

𝑖= 1

OVA testing algorithm:

for 𝑖 = 1 to 𝑘

𝑖

𝑖

𝑖

return argmax

𝑘

𝑘

Even if OVA is an external procedure to learn multiclass problem, there still are decision boundaries.

All vs all (AVA) means that we want to learn 𝑘(𝑘 − 1 ) ⁄ 2 (all the possible pairs) classifiers and we

consider only two classes at the time: we take out only the examples that correspond to the two classes

from my training set (we have a reduced training set) and we learn the model (binary classification).

AVA training algorithm:

𝑖𝑗

← Ø, ∀ 1 ≤ 𝑖 < 𝑗 ≤ 𝑘

for 𝑖 = 1 to 𝑘 − 1

𝑝𝑜𝑠

← all 𝑥 ∈ 𝐷

𝑚𝑢𝑙𝑡𝑖𝑐𝑙𝑎𝑠𝑠

labeled 𝑖

for 𝑗 = 𝑖 + 1 to 𝑘

𝑛𝑒𝑔

← all 𝑥 ∈ 𝐷

𝑚𝑢𝑙𝑡𝑖𝑐𝑙𝑎𝑠𝑠

labeled 𝑗

𝑏𝑖𝑛

𝑝𝑜𝑠

𝑛𝑒𝑔

𝑖𝑗

← BinaryTrain(𝐷

𝑏𝑖𝑛

return all 𝑓

𝑖𝑗

s

At test time all possible models are evaluated: if the model predicts positive then we assign 1 vote to that

class, otherwise we assign 1 vote to the opponent. After running all the 𝑘(𝑘 − 1 ) ⁄ 2 classifiers, the class

with the most votes wins.

If we want to do something more sophisticated, we can work with weighted votes based on confidence.

AVA testing algorithm:

for 𝑖 = 1 to 𝑘 − 1

for 𝑗 = 𝑖 + 1 to 𝑘

𝑖𝑗

𝑖

𝑖

𝑗

𝑗

return argmax

𝑘

𝑘

AVA performance:

  • At training time AVA learns more classifiers than OVA, however, this is fast because they are

trained on much smaller and balanced data

  • At testing time AVA is often slower than OVA and has more chances for errors because of the

number of classifiers

When using a binary classifier, the most common thing to do is OVA because it’s more robust in term of

performance.

In real-life problem the perceptron (with these strategies) is not much used because:

  • DT and k-NN work reasonably well without modifications
  • Other more sophisticated methods work better (e.g. feedforward neural networks)

We evaluate the performance of models by accuracy, but this measure doesn’t suit well multiclass

classification problem because of the data imbalance.

Accuracy → count every time if the label is equal to the prediction.

Data imbalance → data with skewed class proportions i.e. don’t consider how much common or rare the

categories are.

Micro-averaging → average over examples.

Macro-averaging → calculate evaluation score (e.g. accuracy) for each label, then average over labels.

When we have multiclass classification problem is common to have the confusion matrix → table where

each entry (𝑖, 𝑗) contains the number of examples with label 𝑖 that were predicted to have label 𝑗. A good

model only fills the diagonal.

Gradient descent

Loss functions

Perceptron algorithm is one example of a linear classifier, but there are many other algorithms that

learn a line.

Remember the machine learning procedure: we pick a model, we pick an objective function to optimize

and then we develop a learning algorithm that should try to minimize the criteria.

If our model is a linear function we have a hyperplane 0 = 𝑏 + ∑

𝑖

𝑖

𝑛

𝑖= 1

The objective function is the 0/1 loss ∑ 1 [𝑦

𝑖

𝑖

+ 𝑏) ≤ 0 ]

𝑛

𝑖= 1

, where 1 [𝑥] is the indicator function that

maps elements to 1 if 𝑥 = true, and to 0 otherwise.

The learning algorithm aims to find the parameters 𝑏 and 𝑤 such that minimize the number of errors

there are in the training set of size 𝑛 i.e. argmin

𝑤,𝑏

1 [𝑦

𝑖

𝑖

+ 𝑏) ≤ 0 ]

𝑛

𝑖= 1

It’s hard to minimize the 0/1 loss function because it’s a not differentiable function → each time we

change 𝑤 such that the example is right/wrong the loss will increase/decrease. This turns out to be NP-

hard!

Regularization

Defining a loss function and calculating the gradient descent on the training set is not enough because

we want the classifier performs well also on test data to avoid overfitting. In general min 𝑤,𝑏

𝐿𝑜𝑠𝑠 on

training and testing set are different!

Regularizer → additional criterion to the loss function to make sure that we do not overfit. It’s a bias on

the model that forces the learning to prefer certain types of weights over others.

Generally, we don’t want huge weights because a small change in a feature can result in a large change

in the prediction, that is suboptimal. We might also prefer weights of 0 for features that are not useful.

So learning algorithm computes argmin

𝑤,𝑏

′

𝑛

𝑖= 1

where 𝜆 is the hyper-

parameter for the trade-off.

Common regularizers:

  • L1 norm → 𝑟(𝑤, 𝑏) = ∑|𝑤

𝑗

  • L2 norm → 𝑟(𝑤, 𝑏) =

𝑗

2

Note that if the L1 norm is used it’s very likely that the solution will have one feature that will be 0 and

the another not, while with the L2 norm the solution will have small values, both close to 0 but not

exactly 0.

In general all the family of the Lp norm can be adopted as regularizer: 𝑟(𝑤, 𝑏) = √

𝑗

𝑝

𝑝

𝑝

where

larger 𝑝 discourages large weights more.

Since Lp norms for 𝑝 ≥ 1 are convex, we can still use the gradient descent to solve the minimization

problem.

The gradient descent updating rule with the regularization term will be: 𝑤 𝑗

𝑗

𝑑

𝑑𝑤

𝑖

In particular:

  • L1 norm → 𝑤

𝑗

𝑗

  • 𝜂(𝑙𝑜𝑠𝑠′ − 𝜆 sign(𝑤

𝑗

  • L2 norm → 𝑤

𝑗

𝑗

′

𝑗

  • Lp norm → 𝑤

𝑗

𝑗

′

𝑗

𝑝− 1

There are iterative procedures like gradient descent that can always been applied, however in some

situations there are specific forms of convex optimization problem which have specialized solvers.

Support vector machines

Large margin classifiers

Main variations in linear classifiers:

  • Which hyperplane is chosen when the data are linearly separable
  • How they handle data that is not linearly separable

The perceptron algorithm finds some hyperplane that separates the data, but if the data points aren’t

linearly separable it will continue to adjust as it iterates through the examples and the final hyperplane

will depend on which examples it saw recently.

SVM instead, is aiming to find a linear model which attempt to the maximize the margin (this ensures

generalization).

The margin of a classifier is the distance to the closest points of either class.

For any separating hyperplane, there exist some set of closest points, called the support vectors. For 𝑛

dimensions, there will be at least 𝑛 + 1 support vectors.

In the final decision model only the support vectors matter, while all the other training examples can be

ignored → efficient at inference time because decision function is computed on a subset of data points.

The margin of a hyperplane is measured as 1 ⁄‖ 𝑤‖, so to maximize it the objective function must be

max 𝑤,𝑏

1

||𝑤||

. But this formula doesn’t consider our data points! To do that we have to put a constraint:

𝑖

𝑖

  • 𝑏) ≥ 1 ∀𝑖 i.e. we want the hyperplane that guarantees the max margin such that all the points

are correctly classified (data are separable) and outside the margin.

Maximizing the margin is equivalent to minimize the norm of the weights, so instead of max 𝑤,𝑏

1

|

| 𝑤

| |

it’s

better to use min

𝑤,𝑏

2

, a more convenient optimization problem.

Soft margin classification

To extend this problem in the case of not linearly separable data points we need to tolerate some error

using the soft margin classification.

The objective function is min 𝑤,𝑏

2

𝑖 𝑖

subject to 𝑦

1

𝑖

𝑖

𝑖

≥ 0 , where 𝜍

𝑖

are

the so called slack penalties that tell how far from “correct” are each point.

In particular, the slack values for points outside (or on) the margin and correctly classified are 0, while

for points incorrectly classified is the distance to the margin:

𝑖

0 i𝑓 𝑦

𝑖

𝑖

𝑖

𝑖

  • 𝑏) otherwise

𝑖

= max( 0 , 1 − 𝑦

𝑖

𝑖

  • 𝑏)) = max( 0 , 1 − 𝑦𝑦

′

C is a regularization parameter that trades off the relative importance of maximizing the margin and

fitting the training data i.e. it controls overfitting.

In general if C is small we have large margin (constraints can be easily ignored), if C is large we have

narrow margin, and if C = ∞ we have hard margin.

A good value of C depends on validation set so to find the optimal hyper-parameter we have to solve the

quadratic optimization problem multiple times and see the performance of it.

SVM can also been seen as an unconstrained optimization problem since is possible to bring constraints

into the objective function: min

𝑤,𝑏

2

  • 𝐶 ∑ max( 0 , 1 − 𝑦

𝑖

𝑖

𝑖

Note that the objective function can be traced back to argmin

𝑤,𝑏

′

𝑛

𝑖= 1

by imposing the loss to be the hinge loss max( 0 , 1 − 𝑦𝑦

′

) and 𝜆 = 1 ⁄𝐶 , so SVM can be solved with

gradient descent too.

Most of the problems are not-linearly separable, so it’s always convenient to choose a non-linear kernel

instead of a linear one.

The most common choice for data that are vectors, it’s the gaussian kernel that map the data into an

infinite-dimensional space.

SVMs can be applied to complex data beyond feature vectors by designing kernel functions for such

data.

Concluding, a non-linear SVM problem is about solving the dual optimization problem

max

𝛼

𝑖 𝑖

1

2

𝑖

𝑗

𝑖

𝑗

𝑇

𝑖

𝑗 𝑖 𝑗

such that ∑ 𝛼

𝑖

𝑖

𝑖

𝑖

, so the solution is the

classifying function 𝑓 (

𝑖

𝑖

𝑖

𝑖

Note that the optimization techniques for finding 𝛼 𝑖

’s remain the same!

SVM power:

  • Theoretical guarantee
  • Possibility to use the same machinery for linear and non-linear case
  • Possibility to deal with structured data → no information loss

Ranking

Multiclass and multilabel

In some classification problems we could have exclusive classes, while in others there could be some

form of hierarchy or some general structure between the labels.

A model that exploit these relationships between classes can help to reduce the error.

Multiclass classification → given a sample you need to produce as output exactly one label, choosing

from N possible classes.

Multilabel classification → given a sample you need to produce as output a set (zero or more) of labels.

Multilabel classification problems can be solved by computing separate linear models (one for each

attribute of the identification task). However most of the sophisticated machine learning methods

perform multitask learning.

The ranking learning algorithm

Assuming to have 𝑁 queries and 𝑀 samples in the training set, the goal of ranking is to train a binary

classifier to predict a preference function.

So given a query 𝑞 and two samples 𝑥 𝑖

and 𝑥

𝑗

, the classifier should predict whether 𝑥

𝑖

should be

preferred to 𝑥 𝑗

with respect to the query 𝑞.

Note that a binary classifier takes as input a vector that is associated to a test sample, then outputs + 1

or − 1 (or optionally a confidence/score). But how can it predict preferences to turns them into a

ranking?

An idea is to train a classifier to decide if the first input is better than second.

So given some ranking, we can create an appropriate training set by considering all possible pairings of

the examples in a ranking and labeling them as positive if the first is higher ranked, negative otherwise.

Binary classifier only takes one input, so we have to create a new vector representation for each pair

made of features that aggregate information between samples.

Common approaches:

𝑖

′

𝑖

𝑖

𝑖

′

𝑖

𝑖

Ranking training algorithm:

𝐷 ← [ ]

for 𝑛 = 1 to 𝑁

for all 𝑖, 𝑗 = 1 to 𝑀 and 𝑖 ≠ 𝑗

if 𝑖 is preferred to 𝑗 on query 𝑛

𝑛𝑖𝑗

else if 𝑗 is preferred to 𝑖 on query 𝑛

𝑛𝑖𝑗

return BinaryTrain(𝐷)

At test time we receive some new vectors of features, and to produce a ranking we again consider all the

possible pairings, extract the features and then apply the binary classifier.

After that each binary sample has a score based on the prediction, we have to call a sorting algorithm to

recover the ranking.

Testing algorithm:

for all 𝑖, 𝑗 = 1 to 𝑀 and 𝑖 ≠ 𝑗

𝑖𝑗

𝑖

𝑖

𝑗

𝑗

return ArgSort(𝑠𝑐𝑜𝑟𝑒)

Note that this simple ranking routine doesn’t take into account how far two objects are (in the ranking)

but this isn’t an issue for bipartite ranking problems, where the only goal is to ensure that all As are

ahead of all Bs (predict binary response).

For non-bipartite ranking problems, are used more sophisticated methods where the weight is based on

distance in ranking.

If the classifier outputs a confidence, we can sort the examples using it as the similarity between

examples.

Another improvement is to use a preference function as sorting function.

In this case we define a ranking as a function 𝜎 that maps the objects we are ranking to the desired

position in the list: 1 , 2 , … , 𝑀. So given data with observed rankings 𝜎, our goal is to learn to predict

rankings for new objects, 𝜎

∗

If we also want to model the fact that making a mistake on some pairs is worse than making a mistake

on others, we need to define a cost function 𝜔, where 𝜔(𝑖, 𝑗) is the cost for accidentally putting

something in position 𝑗 when it should have gone in position 𝑖.

A valid cost function 𝜔 must be symmetric, monotonic and satisfy the triangle inequality.

Depending on the problem the cost function may be defined in different ways:

= 1 whenever 𝑖 ≠ 𝑗 → simply counts the number of pairwise mis-ordered items

1 if min{𝑖, 𝑗} ≤ 𝑘 and 𝑖 ≠ 𝑗

0 otherwise

→ impose the top 𝑘 predictions correct

Ranking task → Given an input space 𝒳, an unknown distribution 𝒟 over 𝒳 × 𝛴

𝑀

(where 𝛴

𝑀

is the set

of all ranking functions over M objects), a training set D sampled from 𝒟, we want to compute a function

𝑀

minimizing 𝔼

(𝑥,𝜎)~𝒟

[∑ [𝜎

𝑢

𝑣

][𝜎̂

𝑣

𝑢

]𝜔(𝜎

𝑢

𝑣

𝑢≠𝑣

] where 𝜎̂ = 𝑓(𝑥).

Ranking improved training algorithm:

𝑏𝑖𝑛

← [ ]

for all (𝑥, 𝜎) ∈ 𝐷

𝑟𝑎𝑛𝑘

for all 𝑢 ≠ 𝑣

𝑦 ← Sign(𝜎

𝑣

𝑢

𝑣

𝑢

𝑏𝑖𝑛

𝑏𝑖𝑛

𝑢𝑣

return BinaryTrain(𝐷

𝑏𝑖𝑛

The tree-growing strategy recursively partitions the training set and decides whether to grow leaves or

non-terminal nodes.

In general we fix a set of leaf predictions ℋ

𝑙𝑒𝑎𝑓

𝑡𝑎𝑠𝑘

, and a set of possible routing (or split)

functions 𝜙 ⊂ {𝐿, 𝑅}

𝒳

If 𝒟 = {𝑧 1

𝑛

} is the training set reaching the node than the optimal leaf predictor can be computed

as ℎ

𝒟

∗

∈ arg min

ℎ∈ℋ

𝑙𝑒𝑎𝑓

The optimal error value is also called impurity measure 𝐼(𝒟) = 𝐸(ℎ

𝒟

∗

; 𝒟) and it indicates the amount of

elements that isn’t on the associated class.

Pure set → set with homogeneous samples (same class).

Impure set → group of elements of different “types”.

We grow a leaf 𝐿𝑒𝑎𝑓(ℎ

𝒟

∗

) if some criterion like these is met:

  • Purity → 𝐼 (
  • Minimum cardinality → |𝒟| < 𝑘
  • Others

If no stopping criterion is met we need to find an optimal split function 𝜙 𝒟

∗

∈ arg min

𝜙∈𝛷

𝜙

The impurity 𝐼 𝜙

(𝒟) of a split function 𝜙 given a training set 𝒟 is computed in terms of the impurity of

the split data: 𝐼

𝜙

|𝒟

𝑑

𝜙

|

| 𝒟

𝑑∈{𝐿,𝑅} |

𝑑

𝜙

) where 𝒟

𝑑

𝜙

To recap how to create a tree:

𝒟

∗

) if stopping criterion met

𝒟

∗

𝐿

∗

𝑅

∗

)) otherwise

Sometimes the selection of the best split function is not given in terms of minimizing the impurity of the

split, but in the equivalent maximization of information gain, which is given by ∆ 𝜙

𝜙

The information gain is non-negative, i.e. ∆ 𝜙

(𝒟) ≥ 0 for any 𝜙 ∈ {𝐿, 𝑅}

𝒳

and any training set 𝒟 ⊂ 𝒳 × 𝒴,

so with this criterion the impurity will never increase for any randomly chosen split.

The leaf prediction can be an arbitrary function ℎ ∈ ℱ task

, but in practice we restrict it to a subset

𝑙𝑒𝑎𝑓

of simple ones.

The simplest predictor is a function returning a constant i.e. ℎ ∈ ℋ

𝑙𝑒𝑎𝑓

𝑦∈𝒴

𝒳

For classification we consider as error function 𝐸(𝑓; 𝒟) =

1

|𝒟|

𝑧∈𝒟

If ℓ(𝑓; (𝑥, 𝑦)) = 1 𝑓(𝑥)≠𝑦

and ℋ

𝑙𝑒𝑎𝑓

𝑦∈𝒴

𝒳

than the impurity measure is the classification error:

𝐼(𝒟) = 1 − max

𝑦∈𝒴

|𝒟

𝑦

|

|𝒟|

where 𝒟

𝑦

′

′

} denote the subset of training samples in 𝒟 with

class label 𝑦.

But we can do different choices for the loss and the leaf function:

  • Entropy

If ℓ(𝑓; (𝑥, 𝑦)) = − log 𝑓

𝑦

(𝑥) and ℋ

𝑙𝑒𝑎𝑓

𝜋∈∆(𝒴)

𝒳

(constant label distribution as leaf

prediction) than the impurity measure is the entropy: 𝐼(𝒟) = − ∑

|𝒟

𝑦

|

|𝒟|

log

|𝒟

𝑦

|

|𝒟|

𝑦∈𝒴

  • Gini impurity

If ℓ(𝑓; (𝑥, 𝑦)) = ∑ [𝑓

𝑐

𝑐=𝑦

]

2

𝑐∈𝒴

and ℋ

𝑙𝑒𝑎𝑓

𝜋∈∆(𝒴)

𝒳

than the impurity measure is

the Gini impurity: 𝐼 (

|𝒟

𝑦

|

| 𝒟

|

2

𝑦∈𝒴

  • Variance

If ℓ(𝑓; (𝑥, 𝑦)) = ‖𝑓(𝑥) − 𝑦‖

2

and ℋ

𝑙𝑒𝑎𝑓

𝑦∈𝒴

𝒳

than the impurity measure is the variance:

1

|𝒟|

𝒟

2

(𝑥,𝑦)∈𝒟

where 𝜇

𝒟

1

|𝒟|

(𝑥,𝑦)∈𝒟

The routing or split 𝜙 ∈ {𝐿, 𝑅}

𝒳

determines whether a data point 𝑥 ∈ 𝒳 should move left or right.

The possible split functions is restricted to some predefined set 𝛷 ∈ {𝐿, 𝑅}

𝒳

depending on the nature of

the feature space.

If we assume discrete nominal features taking values in 𝒦, than the split function can be implemented

given a partition of 𝒦 into 𝒦 𝑅

and 𝒦

𝐿

as 𝜙(𝑥) = {

𝐿 if 𝑥 ∈ 𝒦

𝐿

𝑅 if 𝑥 ∈ 𝒦

𝑅

Finding the optimal split requires testing 2

|𝒦|− 1

− 1 bi-partitions.

If we assume ordinal features (i.e. can be sorted) taking values in 𝒦, than the split function can be

implemented given a threshold 𝑟 ∈ 𝒦 as 𝜙(𝑥) = {

𝐿 if 𝑥 ≤ 𝑟

𝑅 if 𝑥 > 𝑟

Note that if |𝒦| ≤ |𝒟|, finding the optimal split requires testing |𝒦| − 1 thresholds. Otherwise, it requires

sorting the input values in 𝒟, where 𝒟 is the training set reaching the node, and testing |𝒟| − 1

thresholds.

Sometimes it’s convenient to split using multiple features at once. Such split functions work with

continuous features and are called oblique, because can generate oblique decision boundaries.

If 𝑥 ∈ ℝ

𝑑

than the split function can be implemented given 𝑤 ∈ ℝ

𝑑

and 𝑟 ∈ ℝ as 𝜙(𝑥) = {

𝐿 if 𝑤

𝑇

𝑅 otherwise

Note that this type of functions allow to have more expressivity but lose efficiency because it’s harder to

optimize.

Basic algorithm pseudocode:

node = root of DT

Main loop:

  1. Find the “best” decision attribute A for the next node
  2. Assign A as decision attribute for node
  3. For each value of A, create a new descendant of node
  4. Sort training examples to leaf nodes
  5. If training example are perfectly classified, stop

Else, recurse over new leaf nodes

Decision trees are very convenient because they are explainable models: at the end we know we have

predicted a certain label because we have certain features activated → non-trivial.

However, since we are trying to reach the minimum error on the training set, we are very likely to incur

into overfitting.

Strategies to improve generalization:

  • Early stopping
  • Regularization
  • Data augmentation
  • Complexity reduction
  • Ensembling → creating a random forest
  • Pruning → replacing a whole subtree by a leaf node

Problems of DT:

  • Many kinds of “noise” can occur in the training data
  • Irrelevant attribute can result in overfitting the training data
  • If we have a large number of attributes, we may find meaningless regularity in the data
  • To reduce overfitting we can remove irrelevant attributes by a manual process → not always

possible

Random forest learning algorithm

Ensemble learning → general approach to machine learning that seeks better predictive performance

(i.e. more robust to overfitting) by combining the predictions from multiple models.

The general idea is to create small datasets from the original one, learn a model for each of these, and

then aggregate them to produce the final model.