[omscs NLP] module 4

Modeling fluency

UNK: unknown

SOS: start of seq

EOS: end of seq

Variable

V : vocab of most freq 50,000 words

W1, W2, W3 : number is word order, W means word random variable (probability of the 50,000 words)

Modeling Fluency Examples

image-20260920152340349

上面这个是一个例子,如果算这句话的 probability 的话,那根据 bayesian statistics 将会是下面的公式:

image-20260920152437019

就是对于每个在 t-th 的 word, conditional probability 根据他前面的 word , 所有的相乘。

但是,在很早以前的时候,这个算力的话是跟不上的,因为每个 word 都是一个 random variable,它的可能性是非常多的。

所以就有了unigram:

image-20260920152708859

就是 suppose 每一个 word 的probability是互相独立的,但如果是这样的话,就会导致一个句子打乱顺序,和这个句子正确的顺序的概率,也是相同的。

那不想要这样打乱顺序的句子,就有了 bigram:

image-20260920152840447

Bigram 是每一个单词,它是由前面的那个词决定的。

那么同理,就有了n-gram。 每个word是由它前面 k 个word决定的:

image-20260920152941860

有没有一种可能,就是让整个这句话的length的词来决定呢?

那就是现代的 neural network:

image-20260920153102812

Neural Language Models

Onehot的length就是vocab length,某个word由某个位置上的1来代表(其余为0)。

image-20260920155200751

One-hot tri-gram 的例子

trigram 就是由t-1, 和t-2的word的onehot作为input,来预测t的word。

image-20260920153827939

图中Linear 的V*2是因为这里有两个word onehot的input。

Encoders & Decoders

下面这个例子,是encoder把onehot的word作为input,然后经过encoder compress,再由decoder recover这个word。

image-20260920155557695

但是一般这个application很少,人们很少会预测用这个词预测当前这个词。

所以可以看bigram, input是t-1的word的onehot,output是t的word onehot

image-20260920155741514

可以看到output会有一些和blue相关的词的activation。

trigram会有前面两个word的onehot input

image-20260920155901975

pytorch里的用法, nn.Linear就是take onehot length of vocab,然后到hidden state上。

nn.Embedding就是会把integer先转成onehot,然后再Linear to hidden state

image-20260920155935393

Recurrent neural network

RNN就是由很多的bigram的encoder-decoder 连接而成的。

下面这个例子,最右边的那个blue的output其实不止是sea,还可是berry (blueberry),如果能提示更前面的词(deep),就能知道大概率是deep blue sea了,所以需要把前面的deep的信息告诉后面那个model。

image-20260920160803636

RNN的解决办法,就是把第一个的hidden state作为vector copy到第二个模型的input里,concatenate with onehot input, go through Linear (vocab + hidden length now), 以此类推。

image-20260920161049367

因此训练过程中,hidden state要学会如何把之前的信息很好的encode好,pass到下面去。

image-20260920161228789

算的时候所有bigram的loss都sum或者做平均,直接backpropagate。

注意,cross entropy loss不是output和target onehot 做减法(mse),而是找到对应的那个position的位置,希望它的值趋近于1 ,越接近1, loss就越接近0 : 根据-logP(w), -log1 = 0

image-20260920162734610

RNN generate的时候,一个方法是用argmax

image-20260920163036538

Generative sampling

如果都用argmax,就会产生local maxima,有些词总会出现,比如and,逗号,之类的

一个办法是用multinomial sampling,根据softmax出来的词的概率进行sampling。

但有的时候很多prob它在高处比较集中,比如deep blue sea/disportion, etc.

有什么办法可以让prob区分开来呢?可以用temperature

对于softmax出来的每个词的概率,normalize(logPw/T, logPw2/T, etc) 即可

image-20260920164750281

这样再sampling,出来的句子就比之前make sense多了。

RNN一个问题就是,随着seq length增加,会逐渐忘记前面的,因为hidden state有限,很难pack那么多info进来,新的word seq会overwrite过去的东西,所以会有遗忘问题。

image-20260920164404810

遗忘的例子,随着往下走,midnight的时候,moles的onehot已经不存在了。

image-20260920165553686

LSTM 本质上就是要解决RNN的遗忘问题,把encoder的部分,替换成了一个memory cell。

image-20260920165650590

LSTM

image-20260920170941108

forget gate, 两种表达都写了,左边的是先linear再concatenate,右边手写的是先concatenate再linear

image-20260920171237898

image-20260920171442405

image-20260920171533163

image-20260920171545100

image-20260920171622604

seq to seq model

translation task里,encoder 和decoder可以分开

image-20260922210031700

具体的模型look like

image-20260922210204469

training process

image-20260922210630476

overview

image-20260922210738777

Teacher forcing

in the decoder, instead of using predicted word to use as the input, use the true word as the input of the next, but still use the predicted word for calculating loss

image-20260922211059991

image-20260922211216414

without teacher forcing训练的很差,经常会有random word。

with teacher forcing 训练的更好,因为每次decoer的input都是正确的。

Attention

image-20260922212234483

Perplexity

branching factor 就是1/P

image-20260923004619364

但是如果P都乘起来就会越来越小,

image-20260923004754295

这时候就需要一个geometric mean

image-20260923004555032