用pytorch编写的一个简单的crf模块。实施是基于https://github.com/allenai/allennlp/blob/master/allennlp/modules/conditional\u random\u field.py

pytorch-text-crf的Python项目详细描述


Pythorch文本CRF

这个包包含一个用于使用条件随机字段(CRF)的简单包装器。这个代码是基于优秀的Allen NLP实现的CRF。在

安装

pip install pytorch-text-crf

使用

^{pr2}$

LSTM CRF实施

有关完整的工作实现,请参阅https://github.com/iamsimha/pytorch-text-crf/blob/master/examples/pos_tagging/train.ipynb。在

fromcrf.crfimportConditionalRandomFieldclassLSTMCRF:"""    An Example implementation for using a CRF model on top of LSTM.    """def__init__(self):......# Initilize the conditional CRF modelself.crf=ConditionalRandomField(n_class,# Number of tagslabel_encoding="BIO",# Label encoding formatidx2tag=idx2tag# Dict mapping index to a tag)defforward(self,inputs,tags):logits=self.lstm(inputs)# logits dim:(batch_size, seq_length, num_tags)mask=inputs!="<pad token>"# mask for ignoring pad tokens. mask dim: (batch_size, seq_length)log_likelihood=self.crf(logits,tags,mask)loss=-log_likelihood# Log likelihood is not normalized (It is not divided by the batch size).# To obtain the best sequence using viterbi decodingbest_tag_sequence=self.crf.best_viterbi_tag(logits,mask)# To obtain output similar to the lstm prediction we can use the below codeclass_probabilities=out*0.0fori,instance_tagsinenumerate(best_tag_sequence):forj,tag_idinenumerate(instance_tags[0][0]):class_probabilities[i,j,int(tag_id)]=1return{"loss":loss,"class_probabilities":class_probabilities}# Traininglstm_crf=LSTMCRF()output=lstm_crf(sentences,tags)loss=output["loss"]loss.backward()optimizer.step()

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
由于java的原因,maven无法运行代码。lang.NoClassDefFoundError:com/fasterxml/jackson/annotation/JsonMerge   Android项目中的java Creative SDK图像编辑器UI   java如何在Android Studio中使用DataOutputStream上传文件并将其他参数传递到web服务器   java倒计时服务打开时崩溃   java将RubyonRails项目转换为JRubyonRails项目   java我的图库意图是不显示图像?为什么?   java如何在春季启动时跳过mongodb/   java@Autowired在Spring中是如何实现的   甲骨文Akka java。util。同时发生的timeoutexception线程池频繁超时   java maven依赖项对spring启动应用程序有何影响?   java Firestore执行复合查询,未截获事件“已修改”   java ItemStreamException:未能初始化读取器,原因是:IllegalStateException:流已初始化。重新开放前关闭   java将空标记解组到集合的新实例中   使用AspectJ的java新手:无法调用aspect   java查找棋类游戏的所有组合   你为什么要这样做and==与Java中的equals方法不一样吗?   如何对使用JavaUUID的代码进行单元测试?