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

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

就是对于每个在 t-th 的 word, conditional probability 根据他前面的 word , 所有的相乘。
但是,在很早以前的时候,这个算力的话是跟不上的,因为每个 word 都是一个 random variable,它的可能性是非常多的。
所以就有了unigram:

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

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

有没有一种可能,就是让整个这句话的length的词来决定呢?
那就是现代的 neural network:

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

One-hot tri-gram 的例子
trigram 就是由t-1, 和t-2的word的onehot作为input,来预测t的word。

图中Linear 的V*2是因为这里有两个word onehot的input。
Encoders & Decoders
下面这个例子,是encoder把onehot的word作为input,然后经过encoder compress,再由decoder recover这个word。

但是一般这个application很少,人们很少会预测用这个词预测当前这个词。
所以可以看bigram, input是t-1的word的onehot,output是t的word onehot

可以看到output会有一些和blue相关的词的activation。
trigram会有前面两个word的onehot input

pytorch里的用法, nn.Linear就是take onehot length of vocab,然后到hidden state上。
nn.Embedding就是会把integer先转成onehot,然后再Linear to hidden state

Recurrent neural network
RNN就是由很多的bigram的encoder-decoder 连接而成的。
下面这个例子,最右边的那个blue的output其实不止是sea,还可是berry (blueberry),如果能提示更前面的词(deep),就能知道大概率是deep blue sea了,所以需要把前面的deep的信息告诉后面那个model。

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

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

算的时候所有bigram的loss都sum或者做平均,直接backpropagate。
注意,cross entropy loss不是output和target onehot 做减法(mse),而是找到对应的那个position的位置,希望它的值趋近于1 ,越接近1, loss就越接近0 : 根据-logP(w), -log1 = 0

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

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) 即可

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

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

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

LSTM

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





seq to seq model
translation task里,encoder 和decoder可以分开

具体的模型look like

training process

overview

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


without teacher forcing训练的很差,经常会有random word。
with teacher forcing 训练的更好,因为每次decoer的input都是正确的。
Attention

Perplexity
branching factor 就是1/P

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

这时候就需要一个geometric mean
