forlanguageprocessing l101: machinelearning · 2018. 10. 8. · it almost goes without saying that...

23
L101: Machine Learning for Language Processing Lecture 1 Guy Emerson

Upload: others

Post on 22-Aug-2020

10 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: forLanguageProcessing L101: MachineLearning · 2018. 10. 8. · It almost goes without saying that Machine Learn-ing requires machine-readable data. Pre-processing refers to automated

L101: Machine Learningfor Language Processing

Lecture 1

Guy Emerson

Page 2: forLanguageProcessing L101: MachineLearning · 2018. 10. 8. · It almost goes without saying that Machine Learn-ing requires machine-readable data. Pre-processing refers to automated

About the course

� Introduction to using Machine Learningfor Natural Language Processing

� Prerequisites:� L90 (or similar) – essential

� L95 – desirable

� 8 lectures, 8 seminars, 1 essay/project

1

The focus is on using Machine Learning – this is nota pure Machine Learning course, but rather an NLPcourse where Machine Learning provides a set oftools.

I (Guy Emerson) will give 7 of the lectures.Ted Briscoe will give 1 lecture, the 8 seminars, andassess the essay or mini-project.

Page 3: forLanguageProcessing L101: MachineLearning · 2018. 10. 8. · It almost goes without saying that Machine Learn-ing requires machine-readable data. Pre-processing refers to automated

Sources of Information

� Course web pages� Handouts include additional notes!

� L90 (and L95) notes

� Textbooks, e.g. Jurafsky & Martin

� Ask questions!

2

Jurafsky & Martin, draft 3rd edition available online:https://web.stanford.edu/~jurafsky/slp3/

Page 4: forLanguageProcessing L101: MachineLearning · 2018. 10. 8. · It almost goes without saying that Machine Learn-ing requires machine-readable data. Pre-processing refers to automated

Today’s Lecture

� What is Machine Learning?

� Example: topic classification

� How do we know if it works?

3

Page 5: forLanguageProcessing L101: MachineLearning · 2018. 10. 8. · It almost goes without saying that Machine Learn-ing requires machine-readable data. Pre-processing refers to automated

What is Machine Learning?

� Task

� Data

� Model

� Training

4

We can break down Machine Learning into fourparts, which I will go through one at a time.

Page 6: forLanguageProcessing L101: MachineLearning · 2018. 10. 8. · It almost goes without saying that Machine Learn-ing requires machine-readable data. Pre-processing refers to automated

Tasks

� What do we want to do?

� Abstract from a real-world problem

� Examples:� Sentiment analysis

� Topic classification

� Machine translation

5

The first step is to define the task. This includes “non-technical” questions, such as who the intended user is (a re-searcher? a company? a member of the public?), which canbe important in deciding how to formalise the task. This stepis often the least discussed, but in real-world applications, itcan be the most difficult.

Defining a task usually requires simplifying a real-world prob-lem. Sentiment analysis (for example, deciding if a prod-uct review is positive or negative) simplifies the complexityof possible opinions into a binary value. Topic classificationsimplifies the complexity of possible topics into a small set.Machine translation might at first look like a real-world prob-lem (after all, people translate things!), but it usually involvestranslating one sentence at a time, without any context, andis usually evaluated using BLEU scores, rather than consider-ing the purpose of the document. Further reading on BLEU:Callison-Burch et al. (2006) http://aclweb.org/anthology/E06-1032

Page 7: forLanguageProcessing L101: MachineLearning · 2018. 10. 8. · It almost goes without saying that Machine Learn-ing requires machine-readable data. Pre-processing refers to automated

Data

� Types of data:� Natural (e.g. “raw” text)

� Pre-processed (e.g. tokenised text)

� Annotated (e.g. pos-tagged text)

6

It almost goes without saying that Machine Learn-ing requires machine-readable data.

Pre-processing refers to automated data prepara-tion.

Annotation is something in addition to the text it-self, such as part-of-speech tags, parse trees, andso on. Text might be manually annotated (by ex-perts or by crowdworkers) or automatically anno-tated (by existing NLP tools), or the annotationsmight be “found” (e.g. star ratings of reviews). Thebare term “annotation” often refers to manual an-notation.

Page 8: forLanguageProcessing L101: MachineLearning · 2018. 10. 8. · It almost goes without saying that Machine Learn-ing requires machine-readable data. Pre-processing refers to automated

Supervised vs. Unsupervised

f : x 7→ y

input output

� Supervised: observe pairs (x,y)

� Unsupervised: observe only x

� Semi-supervised: observe both

7

How does the data relate to the task? We can view a task aswanting to find a function f that maps from an input x to anoutput y.

For example, for sentiment analysis, the input would bea text, and the output would be the sentiment (posi-tive/negative). For topic classification, the input would be atext, and the output would be a topic. For machine transla-tion, the input would be a sentence in the source language,and the output would be a sentence in the target language.

For supervised learning, we observe both inputs and outputs.For unsupervised learning, we observe only inputs, and thetask is to find some kind of structure in the data – for a prob-abilistic model, this might mean estimating the probabilityof the input; for a non-probabilistic model, this might meangrouping the input into clusters. Further reading on unsu-pervised learning: Ghahramani (2004) http://mlg.eng.cam.ac.uk/zoubin/papers/ul.pdf

Page 9: forLanguageProcessing L101: MachineLearning · 2018. 10. 8. · It almost goes without saying that Machine Learn-ing requires machine-readable data. Pre-processing refers to automated

Models

f : x 7→ y

� How do we represent f?

� Parameters

8

There are many ways that we could represent afunction. One way to do this is to define a modelwith a fixed structure (or architecture), but whichincludes many parameters which will be decidedbased on the data. If we specify the model and theparameters, we have a specific function. If we onlyspecify the model, we have a family of functions.

(I will not be discussing nonparametric models,which can also be seen as having an infinite num-ber of parameters. Further reading on nonpara-metric models: Gershman and Blei (2011) https://arxiv.org/pdf/1106.2697 )

Page 10: forLanguageProcessing L101: MachineLearning · 2018. 10. 8. · It almost goes without saying that Machine Learn-ing requires machine-readable data. Pre-processing refers to automated

Discriminative vs. Generative

f : x 7→ y

� Non-probabilistic: f

� Discriminative: P(y|x)

� Generative: P(x,y)

9

A non-probabilistic model deterministically mapsinputs to outputs.

Probabilistic models can be broken down into twotypes. Discriminative models model the conditionaldistribution over outputs, given an input. Gener-ative models model the joint probability of inputsand outputs. Generative models are more general,since P(x,y) = P(y|x)P(x), so they also tell us theprobability of inputs P(x) – but this can also makethem more challenging.

Page 11: forLanguageProcessing L101: MachineLearning · 2018. 10. 8. · It almost goes without saying that Machine Learn-ing requires machine-readable data. Pre-processing refers to automated

What is Machine Learning?

� Task – what function do we want?

� Data – what do we observe?

� Model – how do we represent the function?

� Training – how do we fix the representation,based on what we observe?

10

We can see a model as parametrising a family offunctions. Training optimises the parameters, ac-cording to the observed data.

Page 12: forLanguageProcessing L101: MachineLearning · 2018. 10. 8. · It almost goes without saying that Machine Learn-ing requires machine-readable data. Pre-processing refers to automated

Topic Classification

� Task� Input: text

� Output: topic (out of small set)

� Data� Texts, each labelled with a topic

� (If unsupervised: topic discovery)

11

Topic classification is a simple example of a super-vised learning task.

The related unsupervised task (where texts are un-labelled) is topic discovery, which is relevant for in-formation retrieval and in the digital humanities.

Page 13: forLanguageProcessing L101: MachineLearning · 2018. 10. 8. · It almost goes without saying that Machine Learn-ing requires machine-readable data. Pre-processing refers to automated

Naive Bayes

� Generative model – P(x,y)

12

Page 14: forLanguageProcessing L101: MachineLearning · 2018. 10. 8. · It almost goes without saying that Machine Learn-ing requires machine-readable data. Pre-processing refers to automated

Naive Bayes

argmaxy

P(y|x) = argmaxy

P(y)P(x|y)

≈ argmaxy

P(y)∏

i

P(xi|y)

Bayes

Naive

� Bernoulli NB – xi binary-valued

� Multinomial NB – xi integer-valued

13

Naive Bayes is a simple generative model, which makes astrong (“naive”) independence assumption – all input featuresare assumed to be independent.

For text, we can take the features to be words. In BernoulliNaive Bayes, the features are binary-valued (1 if a word ap-pears, 0 if not). In Multinomial Naive Bayes, they are integer-valued (the number of times the word appears) – a “bag ofwords”. The data must be pre-processed appropriately.

Naive Bayes can generate the training data – we first gen-erate the topic y, and then we generate the features giventhe topic. For Bernoulli Naive Bayes, we independently gen-erate each binary feature. For Multinomial Naive Bayes, weindependently generate one word at a time.

The independence assumption is clearly wrong (e.g. “Hong”and “Kong”), but Naive Bayes is often surprisingly accurate.Fast and dumb models are sometimes better than slow andsophisticated models!

Page 15: forLanguageProcessing L101: MachineLearning · 2018. 10. 8. · It almost goes without saying that Machine Learn-ing requires machine-readable data. Pre-processing refers to automated

Naive Bayes

� Parameters: P(y), P(xi|y)

� Training (Bernoulli NB):

� P(y) =Ny + α

N+Kα

� P(xi|y) =Ny,i + β

Ny + 2β

� Hyperparameters: α, β

14

Bernoulli Naive Bayes, trained based on observed counts:N – total number of documentsNy – number of documents with topic yNy,i – number of documents with topic y where word i appearsK – number of topicsMore precisely, P(xi=1|y) = Ny,i+β

Ny+2β , and P(xi=0|y) = Ny−Ny,i+βNy+2β .

Multinomial Naive Bayes can be trained similarly, except:Ny,i – number of times word i appears in topic y documentsV – vocabulary sizeGenerate word i with probability

Ny,i+β∑

jNy,j+Vβ

Smoothing avoids zero probabilities (and can improve theprobability estimates in general). The amount of smoothing isan example of a hyperparameter – a quantity controlling howthe parameters are fixed.

Page 16: forLanguageProcessing L101: MachineLearning · 2018. 10. 8. · It almost goes without saying that Machine Learn-ing requires machine-readable data. Pre-processing refers to automated

Example: English Wikipedia

Thus, what started as an effort to translatebetween languages evolved into an entirediscipline devoted to understanding how torepresent and process natural languagesusing computers.

15

What topic is this about?

Which features (words) are relevant?

Page 17: forLanguageProcessing L101: MachineLearning · 2018. 10. 8. · It almost goes without saying that Machine Learn-ing requires machine-readable data. Pre-processing refers to automated

Example: English Wikipedia

An extreme example is the alien species,the Vulcans, who had a violent past butlearned to control their emotions.

16

Page 18: forLanguageProcessing L101: MachineLearning · 2018. 10. 8. · It almost goes without saying that Machine Learn-ing requires machine-readable data. Pre-processing refers to automated

Example: German Wikipedia

Es umschließt die Mündungen des HudsonRiver und des East River in den Atlantis-chen Ozean und erhebt sich durchschnit-tlich sechs Meter über den Meeresspiegel.

17

If you speak English but not German, you mightbe able to guess a topic without understanding thewhole text.

For many NLP tasks, full text understanding is notnecessary!

Page 19: forLanguageProcessing L101: MachineLearning · 2018. 10. 8. · It almost goes without saying that Machine Learn-ing requires machine-readable data. Pre-processing refers to automated

Example: German Wikipedia

Schließlich bediente sich Ian Fleming auchder Geschichten und des Charakters desserbischen Doppelagenten Dusko Popovaus dem Zweiten Weltkrieg.

18

Even a very small number of features can beenough.

Page 20: forLanguageProcessing L101: MachineLearning · 2018. 10. 8. · It almost goes without saying that Machine Learn-ing requires machine-readable data. Pre-processing refers to automated

Evaluation

How do we know if it works?

19

Page 21: forLanguageProcessing L101: MachineLearning · 2018. 10. 8. · It almost goes without saying that Machine Learn-ing requires machine-readable data. Pre-processing refers to automated

Training and Testing

� Split data:� Training

� Development

� Testing

� Metric (e.g. accuracy, F1)

� Baseline, significance test

20

Splitting the data allows us to test if a system re-ally works. The training and test data should notoverlap, and the test data should ideally only beused once. Development data is used for prelimi-nary tests, and for hyperparameter tuning.

Cross-validation uses multiple data splits (and itshould ideally only be done once).

A metric is necessary to quantitatively test a sys-tem. The results can be compared against a base-line (a simple model) and a ceiling (perfect or hu-man performance). Significance tests can be usedto probe whether a difference between systemscould be a real effect, or just down to randomchance (sadly often missing or done badly in NLP!)

Page 22: forLanguageProcessing L101: MachineLearning · 2018. 10. 8. · It almost goes without saying that Machine Learn-ing requires machine-readable data. Pre-processing refers to automated

Shared Tasks

� Task

� Data

� Model

� Training

Provided

Participant

21

A shared task is a standard dataset, which allowsdifferent research groups to more easily comparemodels or training algorithms.

Page 23: forLanguageProcessing L101: MachineLearning · 2018. 10. 8. · It almost goes without saying that Machine Learn-ing requires machine-readable data. Pre-processing refers to automated

Summary

� ML – task, data, model, training

� Topic classification with Naive Bayes

� Evaluation – data split, shared tasks

22