首页 > 编程知识 正文

pytorch实例,Cbow

时间:2023-05-04 21:04:33 阅读:25499 作者:4808

导入torch from torch import nn, optimfromtorch.autogradimportvariableimporttorch.nn.functionalasfcontext _ size=2raw _ text=' weareabouttostudythe gsthatinhabitcomputers.astheyevolve, processesmanipulateotherabstractthingscalleddata.theevolutionofaprocessisdirectedbyapatternofrulescalledaprogram.peoplecrecrecrer . ineffect,weconjurethespiritsofthecomputerwithourspells.'.split (' ) Vocab=set )将raw_tthe #语句中的所有单词封装到set类中wordinenumerate(vocab ) }data=[]forIinrange ) context_size,len(raw_text(-context_size ) : context=target () classcbow(nn.module ) :def_init_(self,n_word,n_dim,context_size ) 3360super ) )。 self(_init_ ) self.embedding=nn.embedding (n _ word,n_dim ) ) nn.embedding (n _ word,n_dim ) ) elf n_ n_dim为他们提供了维度空间# https://blog.csdn.net/a 845717607/article/details/104752736 self.linear1=nn.linear (2 x ) : x=self.embedding(x ) x ) #将x放在构建的空间中x=x.view(1,-1) x.view ) ) x,b )其中“-1”的值取决于另一项context _ size (if torch.cuda.is _ available ) : model=model.cuda ) GPUcriterion=nn.crossentropyyon lr=1e-3 ) forepochinrange(1000 ) : print (epoch { }.format (epoch ) ) ) ** 10 ) running_loss=)。target=word context=variable (torch.long tensor ) [word_to_idx[I]forIincontext] ) 为了实现自动求导if torch.cuda.is _ available (: context=context.cuda ) target=target.cuda ) ouda (运行) . item ) )向量的元素值optimizer.zero_grad ) ) loss.backward ) ) optimizer.step './model _ state ' (打印(loss.backward )

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。