神经概率语言模型:NPLM

news/2024/10/24 2:28:58/

NPLM :神经概率语言模型

  本文主要参考《A Neural Probabilistic Language Model》这是一篇很重要的语言模型论文,发表于2003年。主要贡献如下:

  1. 提出了一种基于神经网络的语言模型,是较早将神经网络应用于语言模型领域的工作之一,具有里程碑意义。
  2. 采用神经网络模型预测下一个单词的概率分布,已经成为神经网络语言模型训练的标准方法之一。
  3. 在论文中,作者训练了一个前馈神经网络,同时学习词的特征表示和词序列的概率函数,并取得了比 trigram 语言模型更好的单词预测效果,这证明了神经网络语言模型的有效性。
  4. 论文提出的思想与模型为后续的许多神经网络语言模型研究奠定了基础。

模型结构

lookup in C
lookup in C
lookup in C
Tanh
Wt-n+1
Ct-n+1
Wt-2
Ct-2
Wt-1
Ct-1
concate layer
hidden layer
PWt:softmax

模型的目标是学到一个概率函数,根据上下文预测下一个词的概率分布:

f ( C ( w t − 1 ) , . . . C ( w t − n + 1 ) ) = P ^ ( w t ∣ w 1 t − 1 ) f(C(w_{t-1}), ... C(w_{t-n+1})) = \hat{P}(w_t|w_1^{t-1}) f(C(wt1),...C(wtn+1))=P^(wtw1t1)

  • C C C : 词的特征向量表,一个 ∣ V ∣ × m |V|\times m V×m的矩阵
  • ∣ V ∣ |V| V:训练集中词的数量
  • m m m:特征向量的维度
  • C ( w t ) C(w_t) C(wt) : 词 w t w_t wt的特征向量表示
  • n n n : 上下文的长度
import os
import time
import pandas as pd
from dataclasses import dataclassimport torch
import torch.nn as nn
from torch.nn import functional as F
from torch.utils.data import Dataset
from torch.utils.data.dataloader import DataLoader
from torch.utils.tensorboard import SummaryWriter

加载数据

数据集来10+中文外卖评价数据集:

data = pd.read_csv('./dataset/waimai_10k.csv')
data.dropna(subset='review',inplace=True)
data['review_length'] = data.review.apply(lambda x:len(x))
data.sample(5)
labelreviewreview_length
45450从说马上送餐,到收到餐,时间特别久,给的送餐电话打不通!28
98550韩餐做得像川菜,牛肉汤油得不能喝,量也比实体少很多,送餐时间久得太久了,1个半小时,唉。44
56640太糟了。等了两个小时,牛肉我吃的快吐了,再也不可能第二次28
23231很好吃,就是粥撒了点,等了一个多小时18
81170送餐员给我打电话比较粗鲁12

统计信息:

data = data[data.review_length <= 50] # 过滤长度小于50的评价
words = data.review.tolist()
chars = sorted(list(set(''.join(words))))    
max_word_length = max(len(w) for w in words)print(f"number of examples: {len(words)}")
print(f"max word length: {max_word_length}")
print(f"size of vocabulary: {len(chars)}")
number of examples: 10796
max word length: 50
size of vocabulary: 2272

划分训练/测试集

test_set_size = min(1000, int(len(words) * 0.1)) 
rp = torch.randperm(len(words)).tolist()
train_words = [words[i] for i in rp[:-test_set_size]]
test_words = [words[i] for i in rp[-test_set_size:]]
print(f"split up the dataset into {len(train_words)} training examples and {len(test_words)} test examples")
split up the dataset into 9796 training examples and 1000 test examples

构造字符数据集[tensor]

  • < BLANK> : 0
  • token seqs : [1, 2, 3, 4, 5, 6]
  • block_size : 3,上下文长度
  • x : [[0, 0, 0],[0, 0, 1],[0, 1, 2], [1, 2, 3], [2, 3, 4], [3, 4, 5], [4, 5, 6]
  • y : [1, 2, 3, 4, 5, 6, 0]
class CharDataset(Dataset):def __init__(self, words, chars, max_word_length, block_size=1):self.words = wordsself.chars = charsself.max_word_length = max_word_lengthself.block_size = block_size# char-->index-->charself.char2i = {ch:i+1 for i,ch in enumerate(chars)}self.i2char = {i:s for s,i in self.char2i.items()}    def __len__(self):return len(self.words)def contains(self, word):return word in self.wordsdef get_vocab_size(self):return len(self.chars) + 1      # add a special def get_output_length(self):return self.max_word_length + 1def encode(self, word):# char sequece ---> index sequenceix = torch.tensor([self.char2i[w] for w in word], dtype=torch.long)return ixdef decode(self, ix):# index sequence ---> char sequenceword = ''.join(self.i2char[i] for i in ix)return worddef __getitem__(self, idx):word = self.words[idx]ix = self.encode(word)x = torch.zeros(self.max_word_length + self.block_size, dtype=torch.long)y = torch.zeros(self.max_word_length, dtype=torch.long)x[self.block_size:len(ix)+self.block_size] = ixy[:len(ix)] = ix# len(ix)+1 y[len(ix)+1:] = -1 # index -1 will mask the loss at the inactive locationsif self.block_size > 1:xs = []for i in range(x.shape[0]-self.block_size):xs.append(x[i:i+self.block_size].unsqueeze(0))return torch.cat(xs), yelse:return x, y

数据加载器[DataLoader]

class InfiniteDataLoader:def __init__(self, dataset, **kwargs):train_sampler = torch.utils.data.RandomSampler(dataset, replacement=True, num_samples=int(1e10))self.train_loader = DataLoader(dataset, sampler=train_sampler, **kwargs)self.data_iter = iter(self.train_loader)def next(self):try:batch = next(self.data_iter)except StopIteration: # this will technically only happen after 1e10 samples... (i.e. basically never)self.data_iter = iter(self.train_loader)batch = next(self.data_iter)return batch

构建模型

context_tokens → \to embedding → \to concate feature vector → \to hidden layer → \to output layer

@dataclass
class ModelConfig:block_size: int = None # length of the input sequences vocab_size: int = None # size of vocabularyn_embed : int = Nonen_hidden: int = None
class MLP(nn.Module):"""takes the previous block_size tokens, encodes them with a lookup table,concatenates the vectors and predicts the next token with an MLP.Reference:Bengio et al. 2003 https://www.jmlr.org/papers/volume3/bengio03a/bengio03a.pdf"""def __init__(self, config):super().__init__()self.block_size = config.block_sizeself.vocab_size = config.vocab_sizeself.wte = nn.Embedding(config.vocab_size + 1, config.n_embed) # token embeddings table# +1 for a special tokenself.mlp = nn.Sequential(nn.Linear(self.block_size * config.n_embed, config.n_hidden),nn.Tanh(),nn.Linear(config.n_hidden, self.vocab_size))def get_block_size(self):return self.block_sizedef forward(self, idx, targets=None):# gather the word embeddings of the previous 3 wordsembs = []for k in range(self.block_size):tok_emb = self.wte(idx[:,:,k]) # token embeddings of shape (b, t, n_embd)#idx = torch.roll(idx, 1, 1)#idx[:, 0] = self.vocab_size # special <BLANK> tokenembs.append(tok_emb)# concat all of the embeddings together and pass through an MLPx = torch.cat(embs, -1) # (b, t, n_embd * block_size)logits = self.mlp(x)# if we are given some desired targets also calculate the lossloss = Noneif targets is not None:loss = F.cross_entropy(logits.view(-1, logits.size(-1)), targets.view(-1), ignore_index=-1)return logits, loss
@torch.inference_mode()
def evaluate(model, dataset, batch_size=10, max_batches=None):model.eval()loader = DataLoader(dataset, shuffle=True, batch_size=batch_size, num_workers=0)losses = []for i, batch in enumerate(loader):batch = [t.to('cuda') for t in batch]X, Y = batchlogits, loss = model(X, Y)losses.append(loss.item())if max_batches is not None and i >= max_batches:breakmean_loss = torch.tensor(losses).mean().item()model.train() # reset model back to training modereturn mean_loss

训练模型

环境初始化

torch.manual_seed(seed=12345)
torch.cuda.manual_seed_all(seed=12345)work_dir = "./Mlp_log"
os.makedirs(work_dir, exist_ok=True)
writer = SummaryWriter(log_dir=work_dir)
config = ModelConfig(vocab_size=train_dataset.get_vocab_size(),block_size=7,n_embed=64,n_hidden=128)

格式化数据

train_dataset = CharDataset(train_words, chars, max_word_length, block_size=config.block_size)
test_dataset = CharDataset(test_words, chars, max_word_length, block_size=config.block_size)train_dataset[0][0].shape, train_dataset[0][1].shape
(torch.Size([50, 7]), torch.Size([50]))

初始化模型

model = MLP(config)model.to('cuda')
MLP((wte): Embedding(2274, 64)(mlp): Sequential((0): Linear(in_features=448, out_features=128, bias=True)(1): Tanh()(2): Linear(in_features=128, out_features=2273, bias=True))
)
# init optimizer
optimizer = torch.optim.AdamW(model.parameters(), lr=5e-4, weight_decay=0.01, betas=(0.9, 0.99), eps=1e-8)
# init dataloader
batch_loader = InfiniteDataLoader(train_dataset, batch_size=64, pin_memory=True, num_workers=4)# training loop
best_loss = None
step = 0
train_losses, test_losses = [],[]
while True:t0 = time.time()# get the next batch, ship to device, and unpack it to input and targetbatch = batch_loader.next()batch = [t.to('cuda') for t in batch]X, Y = batch# feed into the modellogits, loss = model(X, Y)# calculate the gradient, update the weightsmodel.zero_grad(set_to_none=True)loss.backward()optimizer.step()# wait for all CUDA work on the GPU to finish then calculate iteration time takentorch.cuda.synchronize()t1 = time.time()# loggingif step % 1000 == 0:print(f"step {step} | loss {loss.item():.4f} | step time {(t1-t0)*1000:.2f}ms")# evaluate the modelif step > 0 and step % 100 == 0:train_loss = evaluate(model, train_dataset, batch_size=100, max_batches=10)test_loss  = evaluate(model, test_dataset,  batch_size=100, max_batches=10)train_losses.append(train_loss)test_losses.append(test_loss)# save the model to disk if it has improvedif best_loss is None or test_loss < best_loss:out_path = os.path.join(work_dir, "model.pt")print(f"test loss {test_loss} is the best so far, saving model to {out_path}")torch.save(model.state_dict(), out_path)best_loss = test_lossstep += 1# termination conditionsif step > 15100:break
step 0 | loss 7.7551 | step time 13.09ms
test loss 5.533482551574707 is the best so far, saving model to ./Mlp_log/model.pt
test loss 5.163593292236328 is the best so far, saving model to ./Mlp_log/model.pt
test loss 4.864410877227783 is the best so far, saving model to ./Mlp_log/model.pt
test loss 4.6439409255981445 is the best so far, saving model to ./Mlp_log/model.pt
test loss 4.482759475708008 is the best so far, saving model to ./Mlp_log/model.pt
test loss 4.350367069244385 is the best so far, saving model to ./Mlp_log/model.pt
test loss 4.250306129455566 is the best so far, saving model to ./Mlp_log/model.pt
test loss 4.16674280166626 is the best so far, saving model to ./Mlp_log/model.pt
test loss 4.0940842628479 is the best so far, saving model to ./Mlp_log/model.pt
.......................
step 6000 | loss 2.8038 | step time 6.44ms
step 7000 | loss 2.7815 | step time 11.88ms
step 8000 | loss 2.6511 | step time 5.93ms
step 9000 | loss 2.5898 | step time 5.00ms
step 10000 | loss 2.6600 | step time 6.12ms
step 11000 | loss 2.4634 | step time 5.94ms
step 12000 | loss 2.5373 | step time 7.75ms
step 13000 | loss 2.4050 | step time 6.29ms
step 14000 | loss 2.5434 | step time 7.77ms
step 15000 | loss 2.4084 | step time 7.10ms

测试:评论生成器

@torch.no_grad()
def generate(model, idx, max_new_tokens, temperature=1.0, do_sample=False, top_k=None):block_size = model.get_block_size()for _ in range(max_new_tokens):# if the sequence context is growing too long we must crop it at block_sizeidx_cond = idx if idx.size(2) <= block_size else idx[:, :,-block_size:]# forward the model to get the logits for the index in the sequencelogits, _ = model(idx_cond)# pluck the logits at the final step and scale by desired temperaturelogits = logits[:,-1,:] / temperature# optionally crop the logits to only the top k optionsif top_k is not None:v, _ = torch.topk(logits, top_k)logits[logits < v[:, [-1]]] = -float('Inf')# apply softmax to convert logits to (normalized) probabilitiesprobs = F.softmax(logits, dim=-1)# either sample from the distribution or take the most likely elementif do_sample:idx_next = torch.multinomial(probs, num_samples=1)else:_, idx_next = torch.topk(probs, k=1, dim=-1)# append sampled index to the running sequence and continueidx = torch.cat((idx, idx_next.unsqueeze(1)), dim=-1)return idx
def print_samples(num=13, block_size=3, top_k = None):# inital 0 tokensX_init = torch.zeros((num, 1, block_size), dtype=torch.long).to('cuda')steps = train_dataset.get_output_length() - 1 # -1 because we already start with <START> token (index 0)X_samp = generate(model, X_init, steps, top_k=top_k, do_sample=True).to('cuda')new_samples = []for i in range(X_samp.size(0)):# get the i'th row of sampled integers, as python listrow = X_samp[i, :, block_size:].tolist()[0] # note: we need to crop out the first <START> token# token 0 is the <END> token, so we crop the output sequence at that pointcrop_index = row.index(0) if 0 in row else len(row)row = row[:crop_index]word_samp = train_dataset.decode(row)new_samples.append(word_samp)return new_samples

不同上下文长度的生成效果

block_size=3

'送餐大叔叔风怎么第一次点的1迷就没有需减改进','送餐很快!菜品一般,送到都等到了都很在店里吃不出肥肉,第我地佩也不好意思了。第一次最爱付了凉面味道不','很不好进吧。。。。。这点一次都是卫生骑题!调菜油腻,真不太满意!','11点送到指定地形,不知道他由、奶茶类应盒子,幸好咸。。。','味道一般小份速度太难吃了。','快递小哥很贴心也吃不习惯。','非常慢。','为什么,4个盒子,反正订的有点干,送餐速度把面洒了不超值!很快!!!!!少菜分量不够吃了!味道很少餐','骑士剁疼倒还没给糖的','怎么吃,正好吃,便宜'

block_size=5

['味道不错,送餐大哥工,餐大哥应不错。','配送很不满意','土豆炒几次,一小时才没吃,幸太多','粥不好吃,没有病311小菜送到,吃完太差了','太咸了,很感谢到,对这次送餐员辛苦,服务很不好','真的很香菇沙,卷哪丝口气,无语了!','菜不怎么夹生若梦粥,小伙n丁也没有收到餐。。。','一点不脆1个多小时才送到。等了那个小时。','就是送的太慢。。。。一京酱肉丝卷太不点了了,大份小太爱,真心不难吃,最后我的平时面没有听说什么呢,就','慢能再提前的好,牛肉好吃而且感觉适合更能事,味道倒卷,送的也很快!']

block_size = 7

['味道还不错,但是酱也没给,一点餐不差','都是肥肉,有差劲儿大的,也太给了,那么好给这么多~后超难吃~','少了一个半小时才吃到了','商务还菜很好的','慢慢了~以后!点他家极支付30元分钟,送过用了呢。','就是没送到就给送王一袋儿食吃起来掉了,有点辣,这油还这抄套!','很好吃,就是送餐师傅不错','包装好的牛肉卷糊弄错酱,重面太少了,肉不新鲜就吃了','味道不错,送得太慢...','非常好非常快递小哥,态度极差,一点也好,菜和粥洒了一袋软,以先订过哈哈哈哈']

http://www.ppmy.cn/news/118902.html

相关文章

U盘移动硬盘变本地硬盘怎么办 ,移动硬盘变本地硬盘的恢复方法

这是分区逻辑损坏后最常见的表现。U盘移动硬盘变本地硬盘怎么办 &#xff0c;移动硬盘变本地硬盘的恢复方法有些用人到这种情况后首先会尝试使用Windows系统自带的硬盘修复工具chk命令进行修复&#xff0c;不过&#xff0c;这样操作并不能解决问题&#xff0c;往往会造成更严重…

华为开发者大会2023官宣,华为云在憋什么大招?

文丨智能相对论 作者丨沈浪 华为云也坐不住了。 在此之前&#xff0c;百度、阿里、商汤、科大讯飞等国内科技厂商以及微软、谷歌等国际巨头都已经发布了自家的大模型新品以及AIGC等相关应用。而华为云手握盘古大模型&#xff0c;却始终按兵不动&#xff0c;迟迟没有正式进场…

SOLIDWORKS安装使用说明网络版

安装准备 系统要求&#xff1a;参考https://www.solidworks.com/sw/support/SystemRequirements.htmlSolidWorks 2017 是最蕞后一个支持win server 2008 R2 sp1的软件。 SolidWorks 2018支持win server 2012及以上的系统&#xff0c;但不支持win server 2019 SolidWorks 2019…

Python中IO编程-文件读写

# (1)读文件f open(/Users/zhoujian/Desktop/zhoujian.txt, r) print(f.read()) f.close()print(----------------------1)#文件使用后必须关闭 try:f open(/Users/zhoujian/Desktop/zhoujian.txt, r)print(f.read()) finally:if f:f.close()print(----------------------2)#…

viper4android华为,【图片】大福利,ViPER4Android FX音效及超过200个精选脉冲样本(转)【华为荣耀3x畅玩版吧】_百度贴吧...

该楼层疑似违规已被系统折叠 隐藏此楼查看此楼 oppo部分&#xff1a; 除了01-04以外&#xff0c;其他的都请关闭V4A自带渲染效果进行体验(即只加载脉冲反馈样本而不开均衡器、场环绕、混响等) 不多说了&#xff0c;大家看编号也该知道录制的顺序有什么规律了吧&#xff0c;越到…

win32-x64-64\binding.node is not a valid Win32 application

1、错误描述 [编译scss/sass] 15:48:25.231 internal/modules/cjs/loader.js:717 [编译scss/sass] 15:48:25.231 return process.dlopen(module, path.toNamespacedPath(filename)); [编译scss/sass] 15:48:25.231 ^ [编译scss/sass] 15:48:25.231 Error: …

STM8S103x STM8S903x 都存在唯一ID

翻阅手册&#xff0c;发现这两款都是存在唯一ID的&#xff0c;可用于做加密功能。 网上之前搜索都是说指向0x48CD&#xff0c;不知道是不是针对其他系列的&#xff0c;反正STM8S103和903是肯定指向0x4865地址。 STM8S103x 手册截图&#xff1a; STM8S903x 手册描述&#xff…

CF903 D.Almost Difference

题目链接&#xff1a;http://codeforces.com/problemset/problem/903/D 题目大意&#xff1a;给你n个数a1&#xff0c;a2&#xff0c;…&#xff0c;an&#xff0c;然后在1到n范围内求函数d&#xff08;x&#xff0c;y&#xff09;的和。 这道题可以这么想&#xff0c;就是先不…