pytorch+lstm实现的pos示例
学了几天终于大概明白pytorch怎么用了
这个是直接搬运的官方文档的代码
之后会自己试着实现其他nlp的任务
#Author:RobertGuthrie importtorch importtorch.autogradasautograd importtorch.nnasnn importtorch.nn.functionalasF importtorch.optimasoptim torch.manual_seed(1) lstm=nn.LSTM(3,3)#Inputdimis3,outputdimis3 inputs=[autograd.Variable(torch.randn((1,3))) for_inrange(5)]#makeasequenceoflength5 #initializethehiddenstate. hidden=(autograd.Variable(torch.randn(1,1,3)), autograd.Variable(torch.randn((1,1,3)))) foriininputs: #Stepthroughthesequenceoneelementatatime. #aftereachstep,hiddencontainsthehiddenstate. out,hidden=lstm(i.view(1,1,-1),hidden) #alternatively,wecandotheentiresequenceallatonce. #thefirstvaluereturnedbyLSTMisallofthehiddenstatesthroughout #thesequence.thesecondisjustthemostrecenthiddenstate #(comparethelastsliceof"out"with"hidden"below,theyarethesame) #Thereasonforthisisthat: #"out"willgiveyouaccesstoallhiddenstatesinthesequence #"hidden"willallowyoutocontinuethesequenceandbackpropagate, #bypassingitasanargumenttothelstmatalatertime #Addtheextra2nddimension inputs=torch.cat(inputs).view(len(inputs),1,-1) hidden=(autograd.Variable(torch.randn(1,1,3)),autograd.Variable( torch.randn((1,1,3))))#cleanouthiddenstate out,hidden=lstm(inputs,hidden) #print(out) #print(hidden) #准备数据 defprepare_sequence(seq,to_ix): idxs=[to_ix[w]forwinseq] tensor=torch.LongTensor(idxs) returnautograd.Variable(tensor) training_data=[ ("Thedogatetheapple".split(),["DET","NN","V","DET","NN"]), ("Everybodyreadthatbook".split(),["NN","V","DET","NN"]) ] word_to_ix={} forsent,tagsintraining_data: forwordinsent: ifwordnotinword_to_ix: word_to_ix[word]=len(word_to_ix) print(word_to_ix) tag_to_ix={"DET":0,"NN":1,"V":2} #Thesewillusuallybemorelike32or64dimensional. #Wewillkeepthemsmall,sowecanseehowtheweightschangeaswetrain. EMBEDDING_DIM=6 HIDDEN_DIM=6 #继承自nn.module classLSTMTagger(nn.Module): def__init__(self,embedding_dim,hidden_dim,vocab_size,tagset_size): super(LSTMTagger,self).__init__() self.hidden_dim=hidden_dim #一个单词数量到embedding维数的矩阵 self.word_embeddings=nn.Embedding(vocab_size,embedding_dim) #传入两个维度参数 #TheLSTMtakeswordembeddingsasinputs,andoutputshiddenstates #withdimensionalityhidden_dim. self.lstm=nn.LSTM(embedding_dim,hidden_dim) #线性layer从隐藏状态空间映射到tag便签 #Thelinearlayerthatmapsfromhiddenstatespacetotagspace self.hidden2tag=nn.Linear(hidden_dim,tagset_size) self.hidden=self.init_hidden() definit_hidden(self): #Beforewe'vedoneanything,wedonthaveanyhiddenstate. #RefertothePytorchdocumentationtoseeexactly #whytheyhavethisdimensionality. #Theaxessemanticsare(num_layers,minibatch_size,hidden_dim) return(autograd.Variable(torch.zeros(1,1,self.hidden_dim)), autograd.Variable(torch.zeros(1,1,self.hidden_dim))) defforward(self,sentence): embeds=self.word_embeddings(sentence) lstm_out,self.hidden=self.lstm(embeds.view(len(sentence),1,-1),self.hidden) tag_space=self.hidden2tag(lstm_out.view(len(sentence),-1)) tag_scores=F.log_softmax(tag_space) returntag_scores #embedding维度,hidden维度,词语数量,标签数量 model=LSTMTagger(EMBEDDING_DIM,HIDDEN_DIM,len(word_to_ix),len(tag_to_ix)) #optim中存了各种优化算法 loss_function=nn.NLLLoss() optimizer=optim.SGD(model.parameters(),lr=0.1) #Seewhatthescoresarebeforetraining #Notethatelementi,joftheoutputisthescorefortagjforwordi. inputs=prepare_sequence(training_data[0][0],word_to_ix) tag_scores=model(inputs) print(tag_scores) forepochinrange(300):#again,normallyyouwouldNOTdo300epochs,itistoydata forsentence,tagsintraining_data: #Step1.RememberthatPytorchaccumulatesgradients. #Weneedtoclearthemoutbeforeeachinstance model.zero_grad() #Also,weneedtoclearoutthehiddenstateoftheLSTM, #detachingitfromitshistoryonthelastinstance. model.hidden=model.init_hidden() #Step2.Getourinputsreadyforthenetwork,thatis,turntheminto #Variablesofwordindices. sentence_in=prepare_sequence(sentence,word_to_ix) targets=prepare_sequence(tags,tag_to_ix) #Step3.Runourforwardpass. tag_scores=model(sentence_in) #Step4.Computetheloss,gradients,andupdatetheparametersby #callingoptimizer.step() loss=loss_function(tag_scores,targets) loss.backward() optimizer.step() #Seewhatthescoresareaftertraining inputs=prepare_sequence(training_data[0][0],word_to_ix) tag_scores=model(inputs) #Thesentenceis"thedogatetheapple".i,jcorrespondstoscorefortagj #forwordi.Thepredictedtagisthemaximumscoringtag. #Here,wecanseethepredictedsequencebelowis01201 #since0isindexofthemaximumvalueofrow1, #1istheindexofmaximumvalueofrow2,etc. #WhichisDETNOUNVERBDETNOUN,thecorrectsequence! print(tag_scores)
以上这篇pytorch+lstm实现的pos示例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持毛票票。
声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:czq8825#qq.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。