defread_time_machine(): # 这里假设你已经有了 txt 文件,或者直接从网络下载 withopen('timemachine.txt', 'r') as f: lines = f.readlines() return [re.sub('[^A-Za-z]+', ' ', line).strip().lower() for line in lines]
1 2 3 4 5 6 7 8 9 10 11 12 13 14
classVocab: def__init__(self,tokens): counter=collections.Counter(tokens) self.idx_to_token=['<unk>']+[token for (token,freq) in counter.items()] self.token_to_idx={token:idx for (idx,token) inenumerate(self.idx_to_token)}
def__len__(self): returnlen(self.idx_to_token) def__getitem__(self, key): ifnotisinstance(key,(list,tuple)): returnself.token_to_idx.get(key,0) else: return [self.__getitem__(token) for token in key]
1 2 3 4
lines=read_time_machine() tokens=[token for line in lines for token in line] vocab=Vocab(tokens) corpus=vocab[tokens]
for e inrange(epochs): net.train() tloss=0 for X,y in train_loader: X,y=X.to(device),y.to(device) state=net.begin_state(batch_size,device) y_pred,_=net(X,state) l=loss(y_pred,y.reshape(-1).long()) optimizer.zero_grad() l.backward() nn.utils.clip_grad_norm_(net.parameters(),max_norm=1) optimizer.step() tloss+=l.item()
scheduler.step() print(f'epoch {e + 1}, loss {tloss / len(train_loader):.4f}')
epoch 1, loss 1.6600
epoch 2, loss 1.1736
epoch 3, loss 0.9350
epoch 4, loss 0.7585
epoch 5, loss 0.6386
epoch 6, loss 0.5594
epoch 7, loss 0.5061
epoch 8, loss 0.4689
epoch 9, loss 0.4426
epoch 10, loss 0.4229
epoch 11, loss 0.4076
epoch 12, loss 0.3953
epoch 13, loss 0.3854
epoch 14, loss 0.3770
epoch 15, loss 0.3701
epoch 16, loss 0.3644
epoch 17, loss 0.3598
epoch 18, loss 0.3562
epoch 19, loss 0.3536
epoch 20, loss 0.3520
defget_last_token(): return torch.tensor([outputs[-1]],device=device,dtype=torch.long).reshape((1,1)) #warmup for i inrange(1,len(prefix)): _,state=net(get_last_token(),state) outputs.append(vocab[prefix[i]]) #predict for j inrange(num_preds): y_hat,state=net(get_last_token(),state) outputs.append(y_hat.argmax(dim=1).item())
return''.join([vocab.idx_to_token[i] for i in outputs])
1
predict('the',200,net,vocab,device)
'the sun grow larger and dullerin the westward sky and the life of the old earth ebb away atlast more than thirty million years hence the huge red hot dome ofthe sun had come to obscure nearly a tenth par'