so this time I'm going to be talking
about language modeling uh obviously
language modeling is a big topic and I'm
not going to be able to cover it all in
one class but this is kind of the basics
of uh what does it mean to build a
language model what is a language model
how do we evaluate language models and
other stuff like that and around the end
I'm going to talk a little bit about
efficiently implementing things in
neural networks it's not directly
related to language models but it's very
important to know how to do uh to solve
your assignments so I'll cover both
is
cool okay so the first thing I'd like to
talk about is generative versus
discriminative models and the reason why
is up until now we've been talking about
discriminative models and these are
models uh that are mainly designed to
calculate the probability of a latent
trait uh given the data and so this is
uh P of Y given X where Y is the lat and
trait we want to calculate and X is uh
the input data that we're calculating it
over so just review from last class what
was X from last class from the example
in L
class
anybody yeah some text yeah and then
what was
why it shouldn't be too
hard yeah it was a category or a
sentiment label precisely in the
sentiment analysis tasks so so um a
generative model on the other hand is a
model that calculates the probability of
data itself and is not specifically
conditional and there's a couple of
varieties um this isn't like super
standard terminology I just uh wrote it
myself but here we have a standalone
probability of P of X and we can also
calculate the joint probability P of X
and Y
so probabilistic language models
basically what they do is they calculate
this uh probability usually uh we think
of it as a standalone probability of P
of X where X is something like a
sentence or a
document and it's a generative model
that calculates the probability of
language recently the definition of
language model has expanded a little bit
so now
um people also call things that
calculate the probability of text and
images as like multimodal language
models or uh what are some of the other
ones yeah I think that's the main the
main exception to this rule usually
usually it's calculating either of text
or over text in some multimodal data but
for now we're going to
consider
um then there's kind of two fundamental
operations that we perform with LMS
almost everything else we do with LMS
can be considered like one of these two
types of things the first thing is calc
scoring sentences or calculating the
probability of
sentences and this
is uh for example if we calculate the
probability of Jane went to the store uh
this would have a high probability
ideally um and if we have this kind of
word salid like this this would be given
a low probability uh according to a
English language model if we had a
Chinese language model ideally it would
also probably give low probability first
sentence too because it's a language
model of natural Chinese and not of
natural English so there's also
different types of language models
depending on the type of data you play
in
the another thing I can do is generate
sentences and we'll talk more about the
different methods for generating
sentences but typically they fall into
one of two categories one is sampling
like this where you try to sample a
sentence from the probability
distribution of the language model
possibly with some modifications to the
probability
distribution um the other thing which I
didn't write on the slide is uh finding
the highest scoring sentence according
to the language model um and we do both
of those
S so more concretely how can we apply
these these can be applied to answer
questions so for example um if we have a
multiple choice question we can score
possible multiple choice answers and uh
the way we do this is we calculate
we first
take uh like we have
like
um
where is
CMU
located um
that's and actually maybe promete this
all again to an a here and then we say X
X1 is equal to
this and then we have X2 which is
Q
where is
CMU
located
a um what's something
plausible
uh what was
it okay now now you're going to make it
tricky and make me talk about when we
have multiple right answers and how we
evaluate and stuff let let's ignore that
for now it's say New
York it's not located in New York is
it
okay let's say
Birmingham hopefully there's no CMU
affiliate in Birmingham I think we're
we're pretty so um and then you would
just calculate the probability of X1 and
the probability of X2 X3 X4 Etc and um
then pick the highest saring one and
actually um there's a famous
there's a famous uh leaderboard for
language models that probably a lot of
people know about it's called the open
llm
leaderboard and a lot of these tasks
here basically correspond to doing
something like that like hel swag is
kind of a multiple choice uh is a
multiple choice question answering thing
about common sense where they calculate
it by scoring uh scoring the
outputs so that's a very common way to
use language
models um another thing is generating a
continuation of a question prompt so
basically this is when you uh
sample and so what you would do is you
would prompt the
model with this uh X here and then you
would ask it to generate either the most
likely uh completion or generate um
sample multiple completions to get the
answer so this is very common uh people
are very familiar with this there's lots
of other uh things you can do though so
um you can classify text and there's a
couple ways you can do this uh one way
you can do this is um like let's say we
have a sentiment sentence
here
um you can say uh
this is
gr and then you can say um
star
rating five or something like that and
then you could also have star rating
four star rating three star rating two
star rating one and calculate the
probability of all of these and find
which one has the highest probability so
this is a a common way you can do things
another thing you can do which is kind
of interesting and um there are papers
on this but they're kind of
underexplored is you can do like star
rating
five and then
generate generate the output um and so
that basically says Okay I I want a
positive sentence now I'm going to score
the actual review and see whether that
matches my like conception of a positive
sentence and there's a few uh papers
that do
this
um let
me
this is a kind of older one and then
there's another more recent one by Sean
Min I believe um uh but they demonstrate
how you can do both generative and
discriminative classification in this
way so that's another thing that you can
do uh with language
models and then the other thing you can
do is you can generate the label given a
classification proc so you you say this
is is great star rating and then
generate five
whatever finally um you can do things
like correct a grammar so uh for example
if you score the probability of each
word and you find words that are really
low probability then you can uh replace
them with higher probability words um or
you could ask a model please paraphrase
this output and it will paraphrase it
into something that gives you uh you
know that has better gra so basically
like as I said language models are very
diverse um and they can do a ton of
different things but most of them boil
down to doing one of these two
operations scoring or
generating any questions
s
okay so next I I want to talk about a
specific type of language models uh Auto
regressive language models and auto
regressive language models are language
models that specifically calculate this
probability um in a fashion where you
calculate the probability of one token
and then you calculate the probability
of the next token given the previous
token the probability of the third token
G given the previous two tokens almost
always this happens left to right um or
start to finish um and so this is the
next token here this is a context where
usually um the context is the previous
tokens Can anyone think of a time when
you might want to do
right to left instead of left to
right yeah language that's from right to
yeah that's actually exactly what I what
I was looking for so if you have a
language that's written from right to
left actually uh things like uh Arabic
and Hebrew are written right to left so
um both of those are
chronologically like earlier to later
because you know if if you're thinking
about how people speak um the the first
word that an English speaker speaks is
on the left just because that's the way
you write it but the first word that an
Arabic speaker speaks is on the the
right because chronologically that's uh
that's how it works um there's other
reasons why you might want to do right
to left but uh it's not really that left
to right is important it's that like
start to finish is important in spoken
language so um one thing I should
mention here is that this is just a rule
of probability that if you have multiple
variables and you're calculating the
joint probability of variables the
probability of all of the variables
together is equal to this probability
here so we're not making any
approximations we're not making any
compromises in order to do this but it
all hinges on whether we can predict
this probability um accurately uh
actually another question does anybody
know why we do this decomposition why
don't we just try to predict the
probability of x
directly any
ideas uh of big X sorry uh why don't we
try to calculate the probability of this
is great directly without deated the
IND that
possibility it could be word salid if
you did it in a in a particular way yes
um so that that's a good point
yeah yeah so for example we talked about
um uh we'll talk about
models um or I I mentioned this briefly
last time you can mention it in more
detail this time but this is great we
probably have never seen this before
right so if we predict only things that
we've seen before if we only assign a
non-zero probability to the things we've
seen before there's going to be lots of
sentences that we've never seen before
it makes it
supercars um that that's basically close
to what I wanted to say so um the reason
why we don't typically do it with um
predicting the whole sentence directly
is because if we think about the size of
the classification problem we need to
solve in order to predict the next word
it's a v uh where V is the size of the
vocabulary but the size of the
classification problem that we need to
um we need to solve if we predict
everything directly is V to the N where
n is the length of the sequence and
that's just huge the vocabulary is so
big that it's hard to kind of uh know
how we handle that so basically by doing
this sort of decomposition we decompose
this into uh
n um prediction problems of size V and
that's kind of just a lot more
manageable for from the point of view of
how we train uh know how we train
models um that being said there are
other Alternatives um something very
widely known uh very widely used is
called a MK language model um a mast
language model is something like Bert or
debera or Roberta or all of these models
that you might have heard if you've been
in MLP for more than two years I guess
um and basically what they do is they
predict
uh they like mask out this word and they
predict the middle word so they mask out
is and then try to predict that given
all the other words the problem with
these models is uh twofold number one
they don't actually give you a uh good
probability here uh like a a properly
formed probability here
because this is true only as long as
you're only conditioning on things that
you've previously generated so that
they're not actually true language
models from the point of view of being
able to easily predict the probability
of a sequence um and also it's hard to
generate from them because you need to
generate in some order and mass language
models don't specify economical orders
so they're good for some things like
calculating representations of the
output but they're not useful uh they're
not as useful for
Generation Um there's also energy based
language models which basically create a
scoring function that's not necessarily
left to right or right to left or
anything like that but that's very
Advanced um if you're interested in them
I can talk more about them that we'll
skip
them and um also all of the language
models that you hear about nowadays GPT
uh llama whatever else are all other
models cool so I'm going to go into the
very um any questions about that
yeah
yeah so in Mass language models the
question was in Mass language models
couldn't you just mask out the last
token and predict that sure you could do
that but there it's just not trained
that way so it won't do a very good job
if you always trained it that way it's
an autor regressive language model so
you're you're back to where you were in
the first place um cool so now we I'll
talk about unigram language models and
so the simplest language models are
count-based unigram language models and
the way they work is um basically we
want to calculate this probability
conditioned on all the previous ones and
the way we do this is we just say
actually we're not going to worry about
the order at all and we're just going to
uh predict the probability of the next
word uh independently of all the other
words so if you have something like this
it's actually extremely easy to predict
the probability of this word and the way
you do this is you just count up the
number of times this word appeared in
the training data set and divide by the
uh divide by the total number of words
in the pring data set and now you have a
language model this is like language
model 101 it's the easiest possible
language model you can write in you know
three lines of python
basically
um so it has a few problems uh the first
problem with this language model is um
handling unknown words so what happens
if you have a word that you've never
seen before um in this language model
here what is the probability of any
sequence that has a word that you've
never seen before yeah the probability
of the sequence gets zero so there might
not be such a big problem for generating
things from the language model because
you know maybe it's fine if you only
generate words that you've seen before
uh but it is definitely a problem of
scoring things with the language model
and it's also a problem of uh for
something like translation if you get an
unknown word uh when you're translating
something then you would like to be able
to translate it reasonably but you can't
do that so um that's an issue so how do
we how do we fix this um there's a
couple options the first option is to
segment to characters and subwords and
this is now the preferred option that
most people use nowadays uh just run
sentence piece segment your vocabulary
and you're all set you're you'll now no
longer have any unknown words because
all the unknown words get split into
shorter
units there's also other options that
you can use if you're uh very interested
in or serious about this and want to
handle this like uh as part of a
research project or something like this
and uh the way you can do this is you
can build an unknown word model and an
unknown word model basically what it
does is it uh predicts the probability
of unknown words using characters and
then it models the probability of words
using words and so now you can you have
kind of like a hierarchical model where
you first try to predict words and then
if you can't predict words you predict
unknown words so this isn't us as widely
anymore but it's worth thinking about uh
or knowing
about okay uh so a second detail um a
parameter uh so parameterizing in log
space
so the um multiplication of
probabilities can be reexpressed is the
addition of log
probabilities uh so this is really
important and this is widely used in all
language models whether they're unigram
language models or or neural language
models there's actually a very simple
reason why we why we do it this way does
anybody uh know the
answer what would happen if we
multiplied uh let's say uh 30 30 tokens
worth of probabilities together um
yeah uh yeah too too small um so
basically the problem is numerical
underflow um so modern computers if if
we weren't doing this on a computer and
we were just doing math it wouldn't
matter at all um but because we're doing
it on a computer uh we
have
ours we have our
32bit
float
where we have uh the exponent in the the
fraction over here so the largest the
exponent can get is limited by the
number of exponent bits that we have in
a 32-bit float and um if that's the case
I forget exactly how large it is it's
like yeah something like 30 minus 38 is
that
right yeah but anyway like if the number
gets too small you'll underflow it goes
to zero and you'll get a zero
probability despite the fact that it's
not actually zero so um that's usually
why we do this it's also a little bit
easier for people just to look at like
minus 30 instead of looking to something
something time 10 to the minus 30 or
something so uh that is why we normally
go um another thing that you can note is
uh you can treat each of these in a
unigram model you can treat each of
these as parameters so we talked about
parameters of a model uh like a um like
a bag of words model and we can
similarly treat these unigram
probabilities as parameters so um how
many parameters does a unigram model
have any
ideas
yeah yeah exactly parameters equal to
the size of the vocabulary so this one's
easy and then we can go um we can go to
the slightly less easy ones
there so anyway this is a unigram model
uh it's it's not too hard um you
basically count up and divide and then
you add the the probabilities here you
could easily do it in a short Python
program higher order engram models so
higher order engram models um what these
do is they essentially limit the context
length to a length of N and then they
count and divide so the way it works
here maybe this is a little bit uh
tricky but I can show an example so what
we do is we count up the number of times
we've seen this is an example and then
we divide by the number of times we've
seen this is n and that's the
probability of example given the the
previous
coms
so the problem with this is anytime we
get a sequence that we've never seen
before like we would like to model
longer sequences to make this more
accurate but anytime we've get a uh we
get a sequence that we've never seen
before um it will get a probability of
zero similarly because this count on top
of here will be zero so the way that uh
engram language models work with this uh
handle this is they have fall back to
Shorter uh engram models so um this
model sorry when I say NR uh n is the
length of the context so this is a four
gr model here because the top context is
four so the photogram model would
calculate this and then interpolate it
like this with a um with a trigram model
uh and then the trigram model itself
would interpolate with the Byram model
the Byram model would interpolate with
the unram
model oh this one oh
okay
um one
second could you uh help get it from the
lock
box
um okay sorry
so getting bad
here just
actually
okay uh oh wow that's a lot
better cool okay so
um so this is uh how we deal with the
fact that models can
be um models can be more precise but
more sparse and less precise but less
sparse this is also another concept that
we're going to talk about more later uh
in another class but this is a variety
of
ensembling where we have different
models that are good at different things
and we combine them together so this is
the first instance that you would see of
this there are other instances of this
but the reason why I mentioned that this
is a a variety of ensembling is actually
you're probably not going to be using
engram models super widely unless you
really want to process huge data sets
because that is one advantage of them
but some of these smoothing methods
actually might be interesting even if
you're using other models and ensembling
them together so
the in order to decide this
interpolation coefficient one way we can
do it is just set a fixed um set a fixed
amount of probability that we use for
every um every time so we could say that
we always set this Lambda to 0.8 and
some always set this Lambda 1us Lambda
to 0.2 and interpolate those two
together but actually there's more
sophisticated methods of doing this and
so one way of doing this is uh called
additive
smoothing excuse me and the the way that
additive smoothing works is um basically
we add Alpha to the uh to the top and
the
bottom and the reason why this is slight
different as is as our accounts get
larger we start to approach the true
distribution so just to give an
example let's say we have uh the
box
is
um let's say initially we
have
uh let let's say our Alpha is
one so initially if we have
nothing um if we have no no evidence for
our sorry I I
realize let's say this is
our fallback
distribution um where this is a
probability of Z 0.5 this is a
probability of 0.3 and this is a
probability of
0.2 so now let's talk about our byr
model um and our byr
model has counts which is the
the the box and the
is so if we do something like this then
um initially we have no counts like
let's say we we have no data uh about
this distribution
um our counts would be zero and our
Alpha would be
one and so we would just fall back to
this distribution we just have like one
times uh one times this distribution
let's say we then we have one piece of
evidence and once we have one piece of
evidence now this would be
0.33 um and this would uh be Alpha equal
to 1 so we'd have
0.5 *
0.33
uh and
0.5 time
0.3 uh is the probability of the Box
because um basically we we have one
piece of evidence and we are adding a
count of one to the lower order
distribution then if we increase our
count
here um now we rely more
strongly sorry that that would be wrong
so so now we rely more strongly on the
higher order distribution because we
have more evidence for the higher order
distribution so basically in this case
um the probability
[Music]
of Lambda which I showed
before is equal to the the sum of the
counts plus um the sum of the counts
over the sum of the counts plus
Ali so as the sum of the counts gets
larger you rely on the higher order
distribution is the sum of the counts is
if the sum of the counts is smaller you
rely more on the lower order
distribution so the more evidence you
have the more you rely on so that's the
basic idea behind these smoothing things
um there's also a number of other
varieties called uh
discounting so uh the discount
hyperparameter basically you subtract
this off um uh you subtract this from
the count so you would subtract like 0.5
from each of the counts that you it's
just empirically this is a better match
for the fact that um natural language
has a very longtailed distribution um
you can kind of do the math and show
that that works and that's actually in
this um in this paper if you're
interested in looking at more details of
that um and then kind of the
stateoftheart in language modeling
before neural language models came out
was this kesser smoothing and what it
does is it discounts but it also
modifies the lower order distribution so
in the lower order distribution you
basically um modify the counts with
respect to how many times that word has
appeared in new contexts with the IDE
idea being that you only use the lower
order distribution when you have uh new
contexts um and so you can kind of Be
Clever
About You Can Be Clever about how you
build this distribution based on the
fact that you're only using it in the
case when this distribution is not very
Rel
so I I would spend a lot more time
teaching this when uh engram models were
kind of the thing uh that people were
using but now I'm going to go over them
very quickly so you know don't worry if
you weren't able to follow all the
details but the basic um the basic thing
take away from this is number one these
are the methods that people use for
engram language models number two if
you're thinking about combining language
models together in some way through you
know ensembling their probability or
something like this this is something
that you should think about a little bit
more carefully because like some
language models might be good in some
context other language models might be
good in other contexts so you would need
to think about that when you're doing um
when you're combining the model
that cool um any any questions about
this Okay
cool so there's a lot of problems that
we have to deal with um when were
creating engram models and that actually
kind of motivated the reason why we
moved to neural language models the
first one is similar to what I talked
about last time with text classification
um that they can't share strength among
similar words like bought and
purchase um another thing is that they
can't easily condition on context with
intervening words so engram models if
you have a rare word in your context
immediately start falling back to the
unigram distribution and they end up
being very bad so uh that was another
issue
and they couldn't handle long distance
um dependencies so if this was beyond
the engram context that they would uh be
handling then you wouldn't be able to
manage this so actually before neural
language models became a really big
thing uh people came up with a bunch of
individual solutions for this in order
to solve the problems but actually it
wasn't that these Solutions didn't work
at all it was just that engineering all
of them together was so hard that nobody
actually ever did that and so they
relied on just engram models out of the
box and that wasn't scalable so it's
kind of a funny example of how like
actually neural networks despite all the
pain that they cause in some areas are a
much better engineering solution to
solve all the issues that previous
method cool um so when they use uh Eng
grab models neural language models
achieve better performance but Eng grab
models are very very fast to estimate
and apply you can even estimate them
completely in
parallel um engram models also I I don't
know if this is necessarily
A a thing that
you a reason to use engram language
models but it is a reason to think a
little bit critically about uh neural
language models which is neural language
models actually can be worse than engram
language models at modeling very low
frequency phenomenas so engram language
model can learn from a single example
they only need a single example of
anything before the probability of that
continuation goes up very high um and uh
but neural language models actually can
forget or not memorize uh appropriately
from single examples so they can be
better at that um there's a toolkit the
standard toolkit for estimating engram
language models is called KLM it's kind
of frighteningly fast um and so people
have been uh saying like I've seen some
jokes which are like job postings that
say people who have been working on
large language models uh for we want
people who have been 10 years of
experience working on large language
models or something like that and a lot
of people are saying wait nobody has 10
years of experience working on large
language models well Kenneth hfield who
created KLM does have 10 years of
experience working on large language
models because he was estimating uh
seven gr
bottles um seven models with a
vocabulary of let's say
100,000 on um you know web text so how
many parameters is at that's more than
any you know large neural language model
that we have nowadays so um they they
have a lot of these parameters are
sparse they're zero counts so obviously
you don't uh you don't memorize all of
them but uh
yeah cool um another thing that maybe I
should mention like so this doesn't
sound completely outdated there was a
really good paper
recently that used the fact that engrams
are
so uses effect that engram models are so
scalable it's this paper um it's called
Data selection for language models via
importance rese sampling and one
interesting thing that they do in this
paper is that they don't
actually um they don't
actually use neural models in any way
despite the fact that they use the
downstream data that they sample in
order to calculate neural models but
they run engram models over um over lots
and lots of data and then they fit a
gaussian distribution to the enr model
counts basically uh in order to select
the data in the reason why they do this
is they want to do this over the entire
web and running a neural model over the
entire web would be too expensive so
they use angr models instead so that's
just an example of something in the
modern context where keeping this in
mind is a good
idea okay I'd like to move to the next
part so a language model evaluation uh
this is important to know I'm not going
to talk about language model evaluation
on other tasks I'm only going to talk
right now about language model
evaluation on the task of language
modeling and there's a number of metrics
that we use for the task of language
modeling evaluating language models on
the task of language modeling the first
one is log likelihood and basically uh
the way we calculate log likelihood is
uh sorry there's an extra parenthesis
here but the way we calculate log
likelihood is we get a test set that
ideally has not been included in our
training data and we take all of the
documents or sentences in the test set
we calculate the log probability of all
of them uh we don't actually use this
super broadly to evaluate models and the
reason why is because this number is
very dependent on the size of the data
set so if you have a larger data set
this number will be larger if you have a
smaller data set this number will be
smaller so the more common thing to do
is per word uh log likelihood and per
word log likelihood is basically
dividing the um dividing the log
probability of the entire corpus with uh
the number of words that you have in the
corpus
um it's also common for papers to report
negative log likelihood uh where because
that's used as a loss and there lower is
better so you just need to be careful
about which one is being
reported so this is pretty common I
think most people are are somewhat
familiar with
this another thing that you might see is
uh
entropy and uh specifically this is
often called cross entropy because
you're calculating
the you're estimating the model on a
training data set and then evaluating it
on a separate data set uh so uh on the
test data set and this is calcul often
or usually calculated as log 2 um of the
probability divided by the number of
words or units in the Corpus does anyone
know why this is log
two as opposed to a normal uh
log
anyone yeah
so yeah so it's calculating as bits um
and this is kind of
a um this is kind of a historical thing
and it's not super super important for
language models but it's actually pretty
interesting uh to to think about and so
actually any probabilistic distribution
can also be used for data compression
um and so you know when you're running a
zip file or you're running gzip or bz2
or something like that uh you're
compressing a file into a smaller file
and any language model can also be used
to compress a SM file into a smaller
file um and so the way it does this is
if you have more likely
sequences uh for example more likely
sentences or more likely documents you
can press them into a a shorter uh
output and
kind of
the
ideal I I think it's pretty safe to say
ideal because I think you can't get a
better method for compression than this
uh if I unless I'm uh you know not well
versed enough in information Theory but
I I think this is basically the ideal
method for data compression and the way
it works is um I have a figure up here
but I'd like to recreate it here which
is let's say we have a vocabulary of
a um which has
50% and then we have a vocabulary uh B
which is
33% and a vocabulary
C
uh yeah C which is about
17% and so if you have a single token
sequence um if you have a single token
sequence
what you do is you can
see divide this into zero and one so if
your single token sequence is a you can
just put zero and you'll be done
encoding it if your single token
sequence is B
then um one overlaps with b and c so now
you need to further split this up into
uh o and one and you can see
that let make sure I did that right yeah
you can you can see
that one zero is entirely encompassed by
uh by B so now B is one Z and C uh C is
not L encompassed by that so you would
need to further break this up and say
it's Z one here and now one one
one is encompassed by this so you would
get uh you would get C if it was 111 and
so every every sequence that started
with zero would start out with a every
sequence that started out with one zero
would start with b and every sequence
that started with 11 one1
start um and so then you can look at the
next word and let's say we're using a
unigram model if we're using a unigram
model for the next uh the next token
let's say the next token is C
so now the next token being C we already
have B and now we take we subdivide
B into
a BC ba a BB and BC and then we find the
next binary sequence that is entirely
encompassed by uh BC by this like
interval and so the moment we find a a
binary sequence that's entirely
encompassed by the interval uh then that
is the the sequence that we can use to
represent that SC and so um if you're
interested in this you can look up the
arithmetic coding on on wikip it's
pretty fascinating but basically um here
this is showing the example of the
unigram model where the probabilities
don't change based on the context but
what if we knew that
c had a really high probability of
following B so if that's the case now we
have like a a b c here um like based on
our our byr model or neural language
model or something like that so now this
is interval is much much larger so it's
much more likely to entirely Encompass a
shorter string and because of that the
um the output can be much shorter and so
if you use this arithmetic encoding um
over a very long sequence of outputs
your the length of the sequence that is
needed to encode this uh this particular
output is going to be essentially um the
number of bits according to times the
times the sequence so this is very
directly connected to like compression
and information Theory and stuff like
that so that that's where entropy comes
from uh are are there any questions
about
this
yeah
uh for
c um so
111 is
because let me let me see if I can do
this
again
so I had one
one so here this interval is
one this interval is one one this
interval is 111
and 111 is the first interval that is
entirely overlapping with with c um and
it's not one Z because one one Z is
overlaping with b and
c so which
Cas so which case one
Z
one one one
Z
when would you use 110 to represent
something it's a good question I guess
maybe you wouldn't which seems a little
bit wasteful
so let me let me think about that I
think um it might be the case that you
just don't use it um
but yeah I'll try to think about that a
little bit more because it seems like
you should use every bet string right so
um yeah if anybody uh has has the answer
I'd be happy to hear it otherwise I take
you cool um so next thing is perplexity
so this is another one that you see
commonly and um so perplexity is
basically two to the ENT uh two to the
per word entropy or e to the uh negative
word level log likelihood in log space
um and so this uh T larger tends to be
better I'd like to do a little exercise
to see uh if this works so like let's
say we have one a dog sees a squirrel it
will usually um and can anyone guess the
next word just yell it
out bar
okay uh what about that what about
something
else
Chase Run
Run
okay John
John anything
else any other
ones so basically what this shows is
humans are really bad language
models so uh interestingly every single
one of the words you predicted here is a
uh a regular verb
um but in natural language model gpt2 uh
the first thing it predicts is B uh
which is kind of a like the Cula there's
also start and that will be like start
running start something um and humans
actually are really bad at doing this
are really bad at predicting next words
we're not trained that way um and so uh
we end up having these biases but anyway
um the reason why I did this quiz was
because that's essentially what
perplexity
means um and what what perplexity is is
it's the number of times you'd have to
sample from the probability distribution
before you get the answer right so you
were a little bit biased here because we
were doing sampling without replacement
so like nobody was actually picking a
word that had already been said but it's
essentially like if you guessed over and
over and over again how many times would
you need until you get it right and so
here like if the actual answer was start
the perplexity would be 4.66 so we'd
expect language model to get it in uh
four guesses uh between four and five
guesses and you guys all did six so you
lose um so uh another important thing to
mention is evaluation in vocabulary uh
so for fair
comparison um make sure that the
denominator is the same so uh if you're
calculating the perplexity make sure
that you're dividing by the same number
uh every time you're dividing by words
if it's uh the other paper or whatever
is dividing by words or like let's say
you're comparing llama to gp2 they have
different tokenizers so they'll have
different numbers of tokens so comparing
uh with different denominators is not uh
not fair um if you're allowing unknown
words or characters so if you allow the
model to not predict
any token then you need to be fair about
that
too um so I'd like to go into a few
Alternatives these are very similar to
the Network classifiers and bag of words
classifiers that I talked about before
so I'm going to go through them rather
quickly because I think you should get
the basic idea but basically the
alternative is uh featued models so we
calculate features of to account based
models as featued models so we calculate
features of the context and based on the
features calculate probabilities
optimize the feature weights using
gradient descent uh
Etc and so for example if we have uh
input giving a
uh we calculate features so um we might
look up uh the word identity of the two
previous words look up the word identity
of the word uh directly previous add a
bias add them all together get scores
and calculate probabilities where each
Vector is the size of the output
vocabulary and feature weights are
optimized using SGD so this is basically
a bag of words classifier but it's a
multiclass bag of words classifier over
the next token so it's very similar to
our classification task before except
now instead of having two classes we
have you know 10,000 classes or 100,000
classes oh yeah sorry very quick aside
um these were actually invented by Rony
Rosenfeld who's the head of the machine
learning department at the end the
machine learning Department uh so um 27
years ago I guess so he has even more
experience large language modeling than
um
cool so um the one difference with a bag
of words classifier is
um we we have
biases um and we have the probability
Vector given the previous word but
instead of using a bag of words this
actually is using uh How likely is it
giving given two words previous so uh
the feature design would be a little bit
different and that would give you a
total
score um as a reminder uh last time we
did a training algorithm where we
calculated gradients loss function with
respect to the
parameters and uh we can use the chain
Rule and back propagation and updates to
move in the direction that increases
enough so nothing extremely different
from what we had for our
B um similarly this solves some problems
so this didn't solve the problem of
sharing strength among similar words it
did solve the problem of conditioning on
context with intervening words because
now we can condition directly on Doctor
without having to um combine with
gitrid um and it doesn't necessarily
handle longdistance dependencies because
we're still limited in our context with
the model I just
described so um if we so sorry back to
neural networks is what I should say um
so if we have a feedforward neural
network language model the way this
could work is instead of looking up
discrete features uh like we had in a
bag of words model uh we would look up
dents embeddings and so we concatenate
together these dense
embeddings and based on the dense
embeddings uh we do some sort of uh
intermediate layer transforms to extract
features like we did for our neural
network based
classifier um we multiply this by
weights uh we have a bias and we
calculate
scores and uh then we take a soft Max to
do
classification so um this can calculate
combination features uh like we we also
used in our uh neural network based
classifiers so um this could uh give us
a positive number for example if the
previous word is a determiner and the
second previous word is a verb so that
would be like uh in giving and then that
would allow us upway to that particular
examples um so this allows us to share
strength in various places in our model
which was also You Know instrumental in
making our our neural network
classifiers work for similar work and
stuff and so these would be word
embeddings so similar words get similar
embeddings another really important
thing is uh similar output words also
get similar rows in The softmax Matrix
and so here remember if you remember
from last class this was a big Matrix
where the size of the Matrix was the
number of vocabulary items times the
size of a word embedding this is also a
matrix where this is
the number of vocabulary items times the
size of a context embedding gr and so
these will also be similar because words
that appear in similar contexts will
also you know want similar embeddings so
they get uploaded in at the same
time and similar hidden States will have
similar context so ideally like if you
have giving a or delivering a or
something like that those would be
similar contexts so they would get
similar purple embeddings out out of the
so one trick that's widely used in
language model that further takes
advantage of this is uh tying
embeddings so here what this does is
sharing parameters between this um
lookup Matrix here and this uh Matrix
over here that we use for calculating
the
softmax and um the reason why this is
useful is twofold number one it gives
you essentially more training data to
learn these embeddings because instead
of learning the embeddings whenever a
word is in
context separately from learning the
embeddings whenever a word is predicted
you learn the the same embedding Matrix
whenever the word is in the context or
whatever it's predicted and so um that
makes it more accurate to learn these uh
embeddings well another thing is the
embedding mat can actually be very large
so like let's say we have aab of
10 100,000 and we have an embedding a
word embedding size of like 512 or
something like that
that's um 51 million
parameters um and this doesn't sound
like a lot of parameters at first but it
actually is a lot to learn when um
these get updated relatively
infrequently uh because
um these get updated relatively
infrequently because they only are
updated whenever that word or token
actually appears in your training data
so um this can be a good thing for
parameter savings parameter efficiency
as
well um so this uh solves most of the
problems here um but it doesn't solve
the problem of longdistance dependencies
because still limited by the overall
length of uh the context that we're
concatenating together here sure we
could make that longer but that would
make our model larger and um and bring
various
issues and so what I'm going to talk
about in on thur day is how we solve
this problem of modeling long contexts
so how do we um build recurrent neural
networks uh how do we build
convolutional uh convolutional networks
or how do we build attention based
Transformer models and these are all
options that are used um Transformers
are kind of
the the main thing that people use
nowadays but there's a lot of versions
of Transformers that borrow ideas from
recurrent uh and convolutional models
um recently a lot of long context models
us use ideas from recurrent networks and
a lot of for example speech models or
things like or image models use ideas
from convolutional networks so I think
learning all but at the same time is a
good idea in comparing
them cool uh any any questions about
this part I went through this kind of
quickly because it's pretty similar to
the the classification stuff that we
covered last time but uh any any things
that people want to
ask okay so next I'm going to talk about
a few other desiderata of language
models so the next one is really really
important it's a concept I want
everybody to know I actually
taught this informally up until this
class but now I I actually made slides
for it starting this time which is
calibration so the idea of calibration
is that the model quote unquote knows
when it knows or the the fact that it is
able to provide a a good answer um uh
provide a good confidence in its answer
and more formally this can be specified
as
the
feature that the model probability of
the answer matches the actual
probability of getting it right um and
so what this means
is the
probability of the
answer um is
correct given the fact that
the model
probability is equal to
P is equal to
ke
so I know this is a little bit hard to
parse I it always took me like a few
seconds to parse before I uh like when I
looked at it but basically if the model
if the model says the probability of it
being correct is
0.7 then the probability that the answer
is correct is actually 0.7 so um you
know if it says uh the probability is
0.7 100 times then it will be right 70
times and so the way we formalize this
um is is by this uh it was proposed by
this seminal paper by gu it all in
2017
and
unfortunately this data itself is hard
to collect
because the model probability is always
different right and so if the model
probability is like if the model
probability was actually 0.7 that'd be
nice but actually it's 0.793 to 6 8 5
and you never get another example where
the probability is exactly the same so
what we do instead is we divide the
model probabilities into buckets so we
say the model probability is between 0
and 0.1 we say the model probability is
between 0.1 and 0.2 0.2 and 0.3 so we
create buckets like this like these and
then we looked at the model confidence
the average model confidence within that
bucket so maybe uh between 0.1 and 0 uh
between 0 and 0.1 the model confidence
on average is 0 055 or something like
that so that would be this T here and
then the accuracy is how often did it
actually get a correct and this can be
plotted in this thing called a
reliability diagram and the reliability
diagram basically um the the
outputs uh
here so this is
um the this is the model
yeah I think the red is the model
um expected probability and then the
blue uh the blue is the actual
probability and then um
the difference between the expected and
the actual probability is kind of like
the penalty there is how how poorly
calibrated
the and one really important thing to
know is that calibration in accuracy are
not necessarily they don't go hand inand
uh they do to some extent but they don't
uh they don't necessarily go hand in
hand and
the example on the left is a a bad model
but a well calibrated so its accuracy is
uh its error is
44.9% um but it's well calibrated as you
can see like when it says it knows the
answer it knows the answer when it
doesn't answer does this model on the
other hand has better erir and um but
worse calibration so the reason why is
the model is very very confident all the
time and usually what happens is um
models that overfit to the data
especially when you do early stopping on
something like accuracy uh when you stop
the training on something like accuracy
will become very overconfident and uh
give confidence estimates um that are in
cor like this so this is important to
know and the reason why it's important
to know is actually because you know
models are very good at making up things
that aren't actually correct nowadays um
and but if you have a really well
calibrated model you could at least say
with what confidence you have this
working so how do you calculate the
probability of an answer so H yeah sorry
uh yes
yes yeah please
go the probability of percent or
percent um usually this would be for a
generated output because you want to
know the the probability that the
generated output is
cor
great that's what I'm about to talk
about so perfect perfect question um so
how do we calculate the answer
probability or um how do we calculate
the confidence in an answer um we're
actually going to go into more detail
about this um in a a later class but the
first thing is probability of the answer
and this is easy when there's a single
answer um like if there's only one
correct answer and you want your model
to be solving math problems and you want
it to return only the answer and nothing
else if it returns anything else like it
won't work then you can just use the
probability of the answer but what
if
um what if there are multiple acceptable
answers um and maybe a perfect example
of that is like where is CMU located
or um uh where where are we right now um
if the answer is where are we right
now um could be
Pittsburgh could be
CMU could be carnegy
melon could be other other things like
this right um and so another way that
you can calculate the confidence is
calculating the probability of the
answer plus uh you know paraphrases of
the answer or other uh other things like
this and so then you would just sum the
probability over all the qu like
acceptable
answers
um another thing that you can do is um
sample multiple outputs and count the
number of times you get a particular
answer this doesn't solve the problem of
paraphrasing ex paraphrases existing but
it does solve the problem of uh it does
solve two problems sometimes there are
language models where you can't get
probabilities out of them um this is not
so much of a problem anymore with the
GPT models because they're reintroducing
the ability to get probabilities but um
there are some models where you can just
sample from them and you can't get
probabilities out but also more
importantly um sometimes when you're
using things like uh Chain of Thought
reasoning which I'll talk about in more
detail but basically it's like um please
solve this math problem and explain
explain your solution and then if it
will do that it will generate you know a
really long explanation of how it got to
the solution and then it will give you
the answer at the very end and so then
you can't calculate the probability of
the actual like answer itself because
there's this long reasoning chain in
between and you have like all these
other all that other text there but what
you can do is you can sample those
reasoning chains 100 times and then see
how many times you got a particular
answer and that's actually a pretty um a
Prett pretty reasonable way of uh
getting a have
yet this is my favorite one I I love how
we can do this now it's just absolutely
ridiculous but you could ask the model
how confident it is and um it sometimes
gives you a reasonable uh a reasonable
answer um there's a really nice
comparison of different methods uh in
this paper which is also on on the
website and basically long story short
the conclusion from this paper is the
sampling multiple outputs one is the
best way to do it if you can't directly
calculate
probabilities um another thing that I'd
like people to pay very close attention
to is in the
Generation Um in the generation class
we're going to be talking about minimum
based risk which is a Criterion for
deciding how risky an output is and it's
actually a really good uh confidence
metric as well but I'm going to leave
that till when we discuss it more detail
with
it um any any questions
here okay
cool um so the other Criterion uh this
is just yet another Criterion that we
would like language models to be good at
um its efficiency and so basically the
model is easy to run on limited Hardware
by some you know uh metric of easy and
some metrics that we like to talk about
our parameter account so often you will
see oh this is the best model under
three billion parameters or this is the
best model under seven billion
parameters or um we trained a model with
one trillion parameters or something
like that you know
uh the thing is parameter count doesn't
really mean that much um from the point
of view of like ease of using the model
um unless you also think about other uh
you know deser
like just to give one example this is a
parameter count um let's say you have a
parameter count of 7 billion is that 7
billion parameters at 32-bit Precision
or is that 7 billion parameters at 4bit
Precision um will make a huge difference
in your memory footprint your speed
other things like that um so some of the
things that are more direct with respect
to efficiency are memory usage um and
there's two varieties of memory usage
one is model uh model only memory usage
so when you load loaded the model into
memory uh how much space does it take
and also Peak memory consumption when
you run have run the model over a
sequence of a certain length how much is
it going to P so that's another
thing another thing is latency um and
with respect to latency this can be
either how long does it take to start
outputting the first token um and how
long does it take to uh finish
outputting uh a generation of a certain
length and the first will have more to
do with how long does it take to encode
a sequence um which is usually faster
than how long does it take to generate a
sequence so this will have to do with
like encoding time this will require
encoding time of course but it will also
require generation
time also throughput so you know how
much um how many sentences can you
process in a certain amount of time so
of these are kind of desad that you you
would
say um we're going to be talking about
this more in the distillation and
compression and generation algorithms
classes so I won't go into a whole lot
of detail about this but um it's just
another thing that we want to be
thinking about in addition to
complexity um but since I'm I'm on the
topic of efficiency I would like to talk
just a little bit about it um in terms
of especially things that will be useful
for implementing your first
assignment and uh one thing that every
body should know about um if you've done
any like deep learning with pytorch or
something like this you already know
about this probably but uh I think it's
worth mentioning but basically mini
batching or batching uh is uh very
useful and the basic idea behind it is
that on Modern Hardware if you do many
of the same operations at once it's much
faster than doing um
like uh operations executively and
that's especially the case if you're
programming in an extremely slow
programming language like python um I
love python but it's slow I mean like
there's no argument about that um and so
what mini batching does is it combines
together smaller operations into one big
one and the basic idea uh for example if
we want to calculate our um our linear
layer with a t uh nonlinearity after it
we will take several inputs X1 X2 X3
concatenate them together and do a
Matrix Matrix multiply instead of doing
three Vector Matrix
multiplies and so what we do is we take
a whole bunch of examples we take like
64 examples or something like that and
we combine them together and calculate
out thingsit one thing to know is that
if you're working with sentences there's
different ways you can calculate the
size of your mini
normally nowadays the thing that people
do and the thing that I recommend is to
calculate the size of your mini batches
based on the number of tokens in the
mini batch it used to be that you would
do it based on the number of sequences
but the the problem is um one like 50
sequences of length like 100 is much
more memory intensive than uh 50
sequences of Link five and so you get
these vastly varying these mini batches
of vastly varying size and that's both
bad for you know memory overflows and
bad for um and bad for learning
stability so I I definitely recommend
doing it based on the number of
comps uh another thing is gpus versus
CPUs so
um uh CPUs one way you can think of it
is a CPUs kind of like a motorcycle it's
very fast at picking up and doing a
bunch of uh things very quickly
accelerating uh into starting new uh new
tasks a GPU is more like an airplane
which uh you wait forever in line in
security and
then and then uh it takes a long time to
get off the ground and start working but
once it does it's extremely fast um and
so if we do a simple example of how long
does it take to do a Matrix Matrix
multiply I calculated this a really long
time ago it's probably horribly out of
date now but the same general principle
stands which is if we have have um the
number of seconds that it takes to do a
Matrix Matrix multiply doing one of size
16 is actually faster on CPU because uh
the overhead it takes to get started is
very low but if you um once you start
getting up to size like 128 by 128
Matrix multiplies then doing it on GPU
is faster and then um it's you know a
100 times faster once you start getting
up to very large matrices so um if
you're dealing with very large networks
handling a GPU is good
um and this is the the speed up
percentage um one thing I should mention
is
um compute with respect to like doing
the assignments for this class if you
have a relatively recent Mac you're kind
of in luck because actually the gpus on
the Mac are pretty fast and they're well
integrated with um they're well
integrated with pipor and other things
like that so decently sized models maybe
up to the size that you would need to
run for assignment one or even
assignment two might uh just run on your
uh laptop computer um if you don't have
a GPU uh that you have immediately
accessible to you I we're going to
recommend that you use collab where you
can get a GPU uh for the first
assignments and then we'll have plug
reddits that you can use otherwise but
um GPU is usually like something that
you can get on the cloud or one that you
have on your Mac or one that you have on
your gaming computer or something like
that um there's a few speed tricks that
you should know for efficient GPU
operations so um one mistake that people
make when creating models is they repeat
operations over and over again and um
you don't want to be doing this so like
for example um this is multiplying a
matrix by a constant multiple times and
if you're just using out of thee box pie
torch this would be really bad because
you'd be repeating the operation uh when
it's not
necessary um you can also reduce the
number of operations that you need to
use so uh use Matrix Matrix multiplies
instead of Matrix Vector
multiplies and another thing is uh
reducing CPU GPU data movement and um so
when you do try to move memory um when
you do try to move memory try to do it
as early as possible and as uh and as
few times as possible and the reason why
you want to move things early or start
operations early is many GPU operations
are asynchronous so you can start the
operation and it will run in the
background while other things are
processing so um it's a good idea to try
to um to optimize and you can also use
your python profiler or um envidia GPU
profilers to try to optimize these
things as
well cool that's all I have uh we're
right at time