ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

PyTorch学习笔记之CBOW模型实践

2019-06-24 14:54:12  阅读:568  来源: 互联网

标签:word target text self torch 笔记 PyTorch context CBOW



复制代码

 1 import torch
 2 from torch import nn, optim
 3 from torch.autograd import Variable
 4 import torch.nn.functional as F
 5 
 6 CONTEXT_SIZE = 2  # 2 words to the left, 2 to the right
 7 raw_text = "We are about to study the idea of a computational process. Computational processes are abstract beings that inhabit computers. As they evolve, processes manipulate other abstract things called data. The evolution of a process is directed by a pattern of rules called a program. People create programs to direct processes. In effect, we conjure the spirits of the computer with our spells.".split(' ')
 8 
 9 vocab = set(raw_text)
10 word_to_idx = {word: i for i, word in enumerate(vocab)}
11 
12 data = []
13 for i in range(CONTEXT_SIZE, len(raw_text)-CONTEXT_SIZE):
14     context = [raw_text[i-2], raw_text[i-1], raw_text[i+1], raw_text[i+2]]
15     target = raw_text[i]
16     data.append((context, target))
17 
18 
19 class CBOW(nn.Module):
20     def __init__(self, n_word, n_dim, context_size):
21         super(CBOW, self).__init__()
22         self.embedding = nn.Embedding(n_word, n_dim)
23         self.linear1 = nn.Linear(2*context_size*n_dim, 128)
24         self.linear2 = nn.Linear(128, n_word)
25 
26     def forward(self, x):
27         x = self.embedding(x)
28         x = x.view(1, -1)
29         x = self.linear1(x)
30         x = F.relu(x, inplace=True)
31         x = self.linear2(x)
32         x = F.log_softmax(x)
33         return x
34 
35 
36 model = CBOW(len(word_to_idx), 100, CONTEXT_SIZE)
37 if torch.cuda.is_available():
38     model = model.cuda()
39 
40 criterion = nn.CrossEntropyLoss()
41 optimizer = optim.SGD(model.parameters(), lr=1e-3)
42 
43 for epoch in range(100):
44     print('epoch {}'.format(epoch))
45     print('*'*10)
46     running_loss = 0
47     for word in data:
48         context, target = word
49         context = Variable(torch.LongTensor([word_to_idx[i] for i in context]))
50         target = Variable(torch.LongTensor([word_to_idx[target]]))
51         if torch.cuda.is_available():
52             context = context.cuda()
53             target = target.cuda()
54         # forward
55         out = model(context)
56         loss = criterion(out, target)
57         running_loss += loss.data[0]
58         # backward
59         optimizer.zero_grad()
60         loss.backward()
61         optimizer.step()
62     print('loss: {:.6f}'.format(running_loss / len(data)))

标签:word,target,text,self,torch,笔记,PyTorch,context,CBOW
来源: https://www.cnblogs.com/jfdwd/p/11076977.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有