fastNLP源码分析

这篇博客深入探讨了fastNLP库中的fastHAN、CNNCharEmbedding和BertEmbedding的实现细节,包括代码分析和作者的创新思考。文章还介绍了ConditionalRandomField的正向传播和计算似然值的过程,以及在实际操作中结合字符和词进行文本分类的应用。最后,博主提出尝试使用attention和CNN改进charCNN的意向。
摘要由CSDN通过智能技术生成

fastHAN

self.label_vocab
Out[6]: 
{
   'POS': Vocabulary(['S-root', 'B-NR', 'M-NR', 'E-NR', 'B-NN']...),
 'CWS': Vocabulary(['S', 'B', 'E', 'M']...),
 'NER': Vocabulary(['O', 'B-NT', 'M-NT', 'E-NT', 'B-NR']...),
 'Parsing': Vocabulary(['APP', 'nn', 'nsubj', 'rcmod', 'cpm']...),
 'pos': Vocabulary(['root', 'NR', 'NN', 'VV', 'DEC']...)}
self.char_vocab
Out[10]: Vocabulary(['[unused12]', '有', '的', '厂', '长']...)
len(self.char_vocab)
Out[11]: 8675

内容太多,感觉有点干不动了,先挂起这条线

https://zhuanlan.zhihu.com/p/67106791

CNNCharEmbedding

看代码

txt = ["中华 人民 共和国",
       "中央 人民 政府"]
words
Out[2]: 
tensor([[3, 2, 4],
        [5, 2, 6]])

根据words_to_chars_embedding这个lookup-table转chars

chars.shape
Out[3]: torch.Size([2, 3, 7])
# 2 batch 3 words 7 chars

割最大长度

说实话我不知道+2怎么来的

max_word_len = word_lengths.max()
chars = chars[:, :, :max_word_len]
chars.shape
Out[4]: torch.Size([2, 3, 5])

做Embedding

chars = self.char_embedding(chars) 
chars.shape
Out[6]: torch.Size([2, 3, 5, 50])

字符batch合并为batch

reshaped_chars = chars.reshape(batch_size * max_len, max_word_len, -1)
reshaped_chars.shape
Out[5]: torch.Size([6, 5, 50</
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值