CS 294-5: Statistical Natural Language Processing

Download Report

Transcript CS 294-5: Statistical Natural Language Processing

Advanced Artificial Intelligence
Lecture 7: Machine Learning
Outline





Machine Learning
Classification (Naïve Bayes)
Regression (Linear, Smoothing)
Linear Separation (Perceptron, SVMs)
Non-parametric classification (KNN)
Outline





Machine Learning
Classification (Naïve Bayes)
Regression (Linear, Smoothing)
Linear Separation (Perceptron, SVMs)
Non-parametric classification (KNN)
Machine Learning
 Up until now: how to reason in a give
model
 Machine learning: how to acquire a model
on the basis of data / experience
 Learning parameters (e.g. probabilities)
 Learning structure (e.g. BN graphs)
 Learning hidden concepts (e.g. clustering)
Machine Learning Lingo
What?
Parameters
Structure
Hidden concepts
What from?
Supervised
Unsupervised
Reinforcement
Self-supervised
What for?
Prediction
Diagnosis
Compression
Discovery
How?
Passive
Active
Online
Offline
Output?
Classification
Regression
Clustering
Details??
Generative
Discriminative
Smoothing
Supervised Machine Learning
f(x)
f(x)
f(x)
x
(a)
f(x)
x
(b)
x
(c)
Given a training set:
(x1, y1), (x2, y2), (x3, y3), … (xn, yn)
Where each yi was generated by an unknown y = f (x),
Discover a function h that approximates the true function f.
x
(d)
Outline





Machine Learning
Classification (Naïve Bayes)
Regression (Linear, Smoothing)
Linear Separation (Perceptron, SVMs)
Non-parametric classification (KNN)
Classification Example: Spam Filter
 Input: x = email
 Output: y = “spam” or “ham”
 Setup:
 Get a large collection of
example emails, each
labeled “spam” or “ham”
 Note: someone has to hand
label all this data!
 Want to learn to predict
labels of new, future emails
 Features: The attributes used to
make the ham / spam decision
 Words: FREE!
 Text Patterns: $dd, CAPS
 Non-text: SenderInContacts
 …
Dear Sir.
First, I must solicit your confidence in this
transaction, this is by virture of its nature
as being utterly confidencial and top
secret. …
TO BE REMOVED FROM FUTURE
MAILINGS, SIMPLY REPLY TO THIS
MESSAGE AND PUT "REMOVE" IN THE
SUBJECT.
99 MILLION EMAIL ADDRESSES
FOR ONLY $99
Ok, Iknow this is blatantly OT but I'm
beginning to go insane. Had an old Dell
Dimension XPS sitting in the corner and
decided to put it to use, I know it was
working pre being stuck in the corner, but
when I plugged it in, hit the power nothing
happened.
A Spam Filter
 Naïve Bayes spam filter
 Data:
 Collection of emails,
labeled spam or ham
 Note: someone has to
hand label all this data!
 Split into training, held-out,
test sets
Dear Sir.
First, I must solicit your confidence in this
transaction, this is by virture of its nature
as being utterly confidencial and top
secret. …
TO BE REMOVED FROM FUTURE
MAILINGS, SIMPLY REPLY TO THIS
MESSAGE AND PUT "REMOVE" IN THE
SUBJECT.
99 MILLION EMAIL ADDRESSES
FOR ONLY $99
 Classifiers
 Learn on the training set
 (Tune it on a held-out set)
 Test it on new emails
Ok, Iknow this is blatantly OT but I'm
beginning to go insane. Had an old Dell
Dimension XPS sitting in the corner and
decided to put it to use, I know it was
working pre being stuck in the corner, but
when I plugged it in, hit the power nothing
happened.
Naïve Bayes for Text
 Bag-of-Words Naïve Bayes:
 Predict unknown class label (spam vs. ham)
 Assume evidence features (e.g. the words) are independent
 Generative model
Word at position
i, not ith word in
the dictionary!
 Tied distributions and bag-of-words
 Usually, each variable gets its own conditional probability
distribution P(F|Y)
 In a bag-of-words model
 Each position is identically distributed
 All positions share the same conditional probs P(W|C)
General Naïve Bayes
 General probabilistic model:
|Y| x |F|n parameters
 General naive Bayes model:
Y
F1
|Y| parameters
F2
Fn
n x |F| x |Y|
parameters
 We only specify how each feature depends on the class
 Total number of parameters is linear in n
Example: Spam Filtering
 Model:
 What are the parameters?
ham : 0.66
spam: 0.33
the :
to :
and :
of :
you :
a
:
with:
from:
...
0.0156
0.0153
0.0115
0.0095
0.0093
0.0086
0.0080
0.0075
 Where do these tables come from?
the :
to :
of :
2002:
with:
from:
and :
a
:
...
0.0210
0.0133
0.0119
0.0110
0.0108
0.0107
0.0105
0.0100
Counts from examples!
Spam Email Example



Bag of Words:
 Representation of documents
 Counts the frequency of words
 “Hello I will say Hello” Hello(2) I (1) Will(1) Say(1)
Spam
 Offer is secret
 Click secret link
 Secret sports link
Ham
 Play sports today
 Went play sports
 Secret sports event
 Sport is today
 Sport costs money
Spam Email Example
 Quiz 1: Size of vocabulary = ?
 Quiz 2: P(Spam) = ?
 Maximum likelihood P(data)=s3*(1-s)5
 Quiz 3: P(“secret”|Spam)=? P(“secret”|Ham)=?
 Quiz 4: Bayes Network, how many parameters needed?
 Quiz 5: Message M=“Sports”, P(Spam|M)
 Quiz 6: M=“Secret is secret”, P(Spam|M)
 Quiz 7: M=“Today is secret”, P(Spam|M)
Generalization and Overfitting
 Raw counts will overfit the training data!




Unlikely that every occurrence of “minute” is 100% spam
Unlikely that every occurrence of “seriously” is 100% ham
What about all the words that don’t occur in the training set at all? 0/0?
In general, we can’t go around giving unseen events zero probability
 At the extreme, imagine using the entire email as the only feature
 Would get the training data perfect (if deterministic labeling)
 Wouldn’t generalize at all
 Just making the bag-of-words assumption gives us some generalization,
but isn’t enough
 To generalize better: we need to smooth or regularize the estimates
Estimation: Smoothing
 Maximum likelihood estimates:
r
g
g
 Problems with maximum likelihood estimates:
 If I flip a coin once, and it’s heads, what’s the estimate for
P(heads)?
 What if I flip 10 times with 8 heads?
 What if I flip 10M times with 8M heads?
 Basic idea:
 We have some prior expectation about parameters
(here, the probability of heads)
 Given little evidence, we should skew towards our prior
 Given a lot of evidence, we should listen to the data
Estimation: Laplace Smoothing
 Laplace’s estimate
(extended):
 Pretend you saw every outcome
k extra times
 What’s Laplace with k = 0?
 k is the strength of the prior
 Laplace for conditionals:
 Smooth each condition
independently:
H
H
T
Spam Email Example (Laplace)
 Quiz 1: Size of vocabulary = ?
 Quiz 2: P(Spam) = ?
 Maximum likelihood P(data)=s3*(1-s)5
 Quiz 3: P(“secret”|Spam)=? P(“secret”|Ham)=?
 Quiz 4: Bayes Network, how many parameters needed?
 Quiz 5: Message M=“Sports”, P(Spam|M)=?
 Quiz 6: M=“Secret is secret”, P(Spam|M)=?
 Quiz 7: M=“Today is secret”
 K=1
 P(Spam)=(3+1)/(8+2)=2/5 P(Ham)=?
 P(“today”|Spam)=? P(“today”|Ham)=?
 P(Spam|M)=?
Tuning on Held-Out Data
 Now we’ve got two kinds of unknowns
 Parameters: the probabilities P(Y|X), P(Y)
 Hyperparameters, like the amount of
smoothing to do: k
 How to learn?
 Learn parameters from training data
 Must tune hyperparameters on different data
 Why?
 For each value of the hyperparameters, train
and test on the held-out (validation)data
 Choose the best value and do a final test on
the test data
How to Learn

Data: labeled instances, e.g. emails marked spam/ham
 Training set
 Held out (validation) set
 Test set

Features: attribute-value pairs which characterize each x

Experimentation cycle





Learn parameters (e.g. model probabilities) on training set
Tune hyperparameters on held-out set
Compute accuracy on test set
Very important: never “peek” at the test set!
Evaluation
 Accuracy: fraction of instances predicted correctly

Training
Data
Held-Out
Data
Overfitting and generalization
 Want a classifier which does well on test data
 Overfitting: fitting the training data very closely, but not
generalizing well to test data
Test
Data
What to Do About Errors?
 Need more features– words aren’t enough!






Have you emailed the sender before?
Have 1K other people just gotten the same email?
Is the sending information consistent?
Is the email in ALL CAPS?
Do inline URLs point where they say they point?
Does the email address you by (your) name?
 Can add these information sources as new variables in
the Naïve Bayes model
A Digit Recognizer
 Input: x = pixel grids
 Output: y = a digit 0-9
Example: Digit Recognition
 Input: x = images (pixel grids)
 Output: y = a digit 0-9
 Setup:
 Get a large collection of example
images, each labeled with a digit
 Note: someone has to hand label all
this data!
 Want to learn to predict labels of new,
future digit images
 Features: The attributes used to make the
digit decision
 Pixels: (6,8)=ON
 Shape Patterns: NumComponents,
AspectRatio, NumLoops
 …
0
1
2
1
??
Naïve Bayes for Digits
 Simple version:
 One feature Fij for each grid position <i,j>
 Boolean features
 Each input maps to a feature vector, e.g.
 Here: lots of features, each is binary valued
 Naïve Bayes model:
Learning Model Parameters
1
0.1
1
0.01
1
0.05
2
0.1
2
0.05
2
0.01
3
0.1
3
0.05
3
0.90
4
0.1
4
0.30
4
0.80
5
0.1
5
0.80
5
0.90
6
0.1
6
0.90
6
0.90
7
0.1
7
0.05
7
0.25
8
0.1
8
0.60
8
0.85
9
0.1
9
0.50
9
0.60
0
0.1
0
0.80
0
0.80
Problem: Overfitting
2 wins!!
Outline





Machine Learning
Classification (Naïve Bayes)
Regression (Linear, Smoothing)
Linear Separation (Perceptron, SVMs)
Non-parametric classification (KNN)
Regression
 Start with very simple example
 Linear regression
 What you learned in high school math
 From a new perspective
 Linear model
 y=mx+b
 hw(x) = y = w1 x + w0
 Find best values for parameters
 “maximize goodness of fit”
 “maximize probability” or “minimize loss”
Regression: Minimizing Loss
 Assume true function f is given by
y = f (x) = m x + b + noise
where noise is normally distributed
 Then most probable values of parameters
found by minimizing squared-error loss:
Loss(hw ) = Σj (yj – hw(xj))2
Regression: Minimizing Loss
House price in $1000
1000
900
800
700
600
500
400
300
500
1000 1500 2000 2500 3000 3500
House size in square feet
Regression: Minimizing Loss
House price in $1000
1000
900
800
700
600
Loss
500
w0
400
w1
300
500
1000 1500 2000 2500 3000 3500
House size in square feet
y = w1 x + w0
Linear algebra gives
an exact solution to
the minimization
problem
Linear Algebra Solution
w1 =
M å xi yi - å xi å yi
Måx 2
i
(å x )
i
1
w1
w0 = å yi - å xi
M
M
2
Linear Regression





X: 3, 6, 4, 5
Y: 0, -3, -1, -2
f(x)=w1x+w0
w1=-1, w0 =3
Minimizing quadratic loss
Loss=(y  w x  w )
w*= arg min Loss
w
 Recaculate w0,w1 L L
2
i
1 i
0
i
 w0
 w1
 Another quiz: X(2,4,6,8), Y(2,5,5,8)
Don’t Always Trust Linear Models
Regression by Gradient Descent
w = any point
loop until convergence do:
for each wi in w do:
wi = wi – α ∂ Loss(w)
∂ wi
Loss
w0
w1
Multivariate Regression
 You learned this in math class too
 hw(x) = w ∙ x = w xT = Σi wi xi
 The most probable set of weights, w*
(minimizing squared error):
 w* = (XT X)-1 XT y
Overfitting
 To avoid overfitting, don’t just minimize loss
 Maximize probability, including prior over w
 Can be stated as minimization:
 Cost(h) = EmpiricalLoss(h) + λ Complexity(h)
 For linear models, consider
 Complexity(hw) = Lq(w) = ∑i | wi |q
 L1 regularization minimizes sum of abs. values
 L2 regularization minimizes sum of squares
Regularization and Sparsity
w2
w2
w*
w*
w1
w1
Cost(h) = EmpiricalLoss(h) + λ Complexity(h)
L1 regularization
L2 regularization
Outline





Machine Learning
Classification (Naïve Bayes)
Regression (Linear, Smoothing)
Linear Separation (Perceptron, SVMs)
Non-parametric classification (KNN)
Linear Separator
Perceptron
ìï 1 if w x + w ³ 0
1
0
f (x) = í
ïî 0 if w1 x + w0 < 0
Perceptron Algorithm
 Start with random w0, w1
 Pick training example <x,y>
 Update (α is learning rate)
 w1  w1+α(y-f(x))x
 w0  w0+α(y-f(x))
 Converges to linear separator (if exists)
 Picks “a” linear separator (a good one?)
What Linear Separator to Pick?
What Linear Separator to Pick?
Maximizes the “margin”
Support Vector Machines
Non-Separable Data?
X2
X1
X3
 Not linearly separable
for x1, x2
 What if we add a
feature?
 x3= x12+x22
 See: “Kernel Trick”
Outline





Machine Learning
Classification (Naïve Bayes)
Regression (Linear, Smoothing)
Linear Separation (Perceptron, SVMs)
Non-parametric classification (KNN)
Nonparametric Models
 If the process of learning good values for
parameters is prone to overfitting,
can we do without parameters?
Nearest-Neighbor Classification
 Nearest neighbor for digits:
 Take new image
 Compare to all training images
 Assign based on closest example
 Encoding: image is vector of intensities:
 What’s the similarity function?
 Dot product of two images vectors?
 Usually normalize vectors so ||x|| = 1
 min = 0 (when?), max = 1 (when?)
x2
Earthquakes and Explosions
7.5
7
6.5
6
5.5
5
4.5
4
3.5
3
2.5
4.5
5
5.5
6
6.5
7
x1
Using logistic regression (similar to linear regression) to do linear classification
K=1 Nearest Neighbors
x1
7.5
7
6.5
6
5.5
5
4.5
4
3.5
3
2.5
4.5
5
5.5
6
6.5
x2
Using nearest neighbors to do classification
7
K=5 Nearest Neighbors
x1
7.5
7
6.5
6
5.5
5
4.5
4
3.5
3
2.5
4.5
5
5.5
6
6.5
7
x2
Even with no parameters, you still have hyperparameters!
Edge length of neighborhood
Curse of Dimensionality
1
0.9
0.8
0.7
0.6
0.5
0.4
0.3
0.2
0.1
0
25
50
75
100 125 150 175 200
Number of dimensions
Average neighborhood size for 10-nearest neighbors, n dimensions, 1M uniform points
Proportion of points in exterior shell
Curse of Dimensionality
1
0.9
0.8
0.7
0.6
0.5
0.4
0.3
0.2
0.1
0
25
50
75
100 125 150 175 200
Number of dimensions
Proportion of points that are within the outer shell, 1% of thickness of the hypercube