1 of 67

CS 162: Natural Language Processing

Saadia Gabriel

Lecture 5:

Language Modeling Part 2

2 of 67

Announcements

  • Homework #1 will be out later today
  • Final enrollment update
  • Schedule changes

The midterm will be on 5/4,

with a make-up midterm available on 5/8.

3 of 67

Quiz Recap

4 of 67

Quiz Recap

Semantics concerns the literal meaning of words and sentences, while pragmatics concerns how meaning is interpreted in context and use

5 of 67

Quiz Recap

Statistical associations are what language models learn rather than true semantic understanding. That being said, to what degree learning the statistical associations leads to implied semantic understanding is not clear…

No, because model can learn and generate according to patterns in the corpus without understanding what they mean

Maybe???

Yes, because it should be generated by not just arranging words, but understanding the context and intent of language to make a sensible sentence

6 of 67

This is debatable

7 of 67

Quiz Recap

8 of 67

Q & A

We don’t like negative numbers

When you’re first developing a LM, perplexity is a good check to make sure it’s sensible. Then you can further tune that model to perform well on downstream tasks.

9 of 67

Q & A

They are limited in terms of capturing discourse

coherence.

This limited context window can also lead to coreference resolution issues...

10 of 67

Q & A

We’re going to do a quick recap!

11 of 67

Smoothing Recap

12 of 67

Add-0.001 Smoothing

High variance solution: this overfits to the observed data

“Novel event” = 0-count event (never happened in training data).

“Novel event” = 0-count event (never happened in training data).

13 of 67

Add-1000 Smoothing

High bias solution: this underfits to the observed data

“Novel event” = 0-count event (never happened in training data).

“Novel event” = 0-count event (never happened in training data).

14 of 67

Add-Lambda Smoothing

15 of 67

Setting Smoothing Parameters

16 of 67

Setting Smoothing Parameters

17 of 67

Setting Smoothing Parameters

18 of 67

5-fold Cross-Validation

19 of 67

N-fold Cross-Validation

20 of 67

But is Add-Lambda the best strategy we could come up with to handle unseen events?

*** Short Discussion ***

21 of 67

Consider for a moment…

22 of 67

Backoff Smoothing

23 of 67

Backoff Smoothing

Solution:

24 of 67

Backoff Smoothing

We won’t cover Good-Turing, but if curious you can read the paper “Good-Turing smoothing without tears”

25 of 67

Backoff Smoothing

*** Short Discussion ***

26 of 67

Tokenization

  • With word-level tokenization, we have to introduce a special symbol <UNK> for unseen words (make sure to do this during training).

Example from Yoav Artzi

27 of 67

Tokenization

*** Short Discussion ***

  • What are the limitations of using <UNK>?

28 of 67

Tokenization

  • What are the limitations of using <UNK>?

Example from Yoav Artzi

  • We can use character-level models instead, but this increases training time, and these models can fail at learning higher-level semantics and long-range dependencies (more weird and unusually long sequences)

29 of 67

Tokenization

Slide from Yoav Artzi

https://www.cs.cornell.edu/courses/cs5740/2025sp/

30 of 67

We’ll revisit this later when we talk about transformers and byte-pair encodings

31 of 67

Back to smoothing:

Model averaging

32 of 67

Backoff Smoothing

33 of 67

Log-linear Language Models

34 of 67

Recall Logistic Regression

Input features

Output classes

Just linear scoring!

Sigmoid converts to class probability

35 of 67

Linear Scoring for NLP

Word probs are conditioned on some context

36 of 67

Linear Scoring

Example from Graham Neubig

Special case for sparsity:

37 of 67

Linear Scoring

Example from Graham Neubig

Special case for sparsity:

38 of 67

Log-Linear Conditional Probability�(interpret score as a log-prob)

39 of 67

Log-Linear Conditional Probability�(interpret score as a log-prob)

This is softmax!

40 of 67

Training

41 of 67

Gradient-based training

Recall:

Recall:

Apply log rules: (1) log(a/b) = log(a) - log(b), (2) log(e^a) = a

Observed features

Expected features

42 of 67

Recap: optimizing language models

We want to minimize the difference between our predicted and actual language distributions

We predict q

We observe p

43 of 67

Neural Language Models

44 of 67

From https://research.gatech.edu/new-neural-network-makes-decisions-human-would

Early work on neural networks began in the 40s and 50s.

Earliest neural network (the perceptron) implemented in 1958.

(Roughly inspired by natural brain function)

45 of 67

Downsides of Prior Models

We’re still relying on very simple features and narrow context windows but human language is extremely complex…

Ideally, we should have models that can learn appropriate features on their own and capture richer patterns in language data for modeling linguistic phenomena like entity disambiguation…

46 of 67

Neural language model

47 of 67

How to model word similarities?

48 of 67

Neural language model

We embed text

We apply linear and non-linear transformations to embedded text

We output a probability distribution

We apply linear and non-linear transformations to embedded text

49 of 67

Neural Network Basics

50 of 67

How Neural Networks Make Predictions

z1 = xW1 + b1

a1 = σ(z1)

z2 = a1W2 + b2

a2 = softmax(z2)

activation units

z1

x

a1

z2

a2

51 of 67

Learning the Parameters

Guess?

52 of 67

Backpropagation

53 of 67

Backpropagation

54 of 67

Backpropagation

55 of 67

Backpropagation

56 of 67

Update the Parameters

57 of 67

Use Case:

Text Classification

58 of 67

Positive or negative review?

59 of 67

Positive or negative review?

60 of 67

Text Classification

  • Sentiment analysis
  • Spam detection
  • Authorship identification
  • Language Identification
  • Assigning subject categories, topics, or genres

61 of 67

Sentiment Analysis

62 of 67

Why sentiment analysis?

63 of 67

Recall our old solution:

64 of 67

Recall our old solution:

65 of 67

Supervised Classification

66 of 67

We can build a neural classifier!

We’ll also talk about attention mechanisms next time, these are central to transformers!

In the next lecture we’ll discuss a specific kind of RNN design, the long-short term memory model or LSTM

Example of RNN classifier (courtesy of MDPI)

67 of 67

Next time…

  • Describe feed-forward and recurrent neural networks

  • Compare specific types of neural network designs

  • Discuss NLP application #1 (text classification) in-depth