当前位置:网站首页>NER中BiLSTM-CRF解读Forward_algorithm
NER中BiLSTM-CRF解读Forward_algorithm
2022-06-28 00:41:00 【365JHWZGo】
如果你对下面的内容有疑惑,可能需要看一下我前一篇写的对BiLSTM-CRF的讲解
CRF+BiLSTM代码分步骤解读
讲解
之前也讲过forward_algorithm的是用来求解所有路径得分之和的函数,下面将用一个具体的例子来讲解一下这个函数实现的流程。
先随机初始化一个发射矩阵e_score (batch_size, seq_len, tags_size)

再随机初始化一个发射矩阵t_score (tags_size, tags_size)

创建一个init_matrix,然后再复制一份给pre_matrix,这里为了方便理解将模型竖起来 (batch_size, 1, tags_size)

这里仅展示当时刻为0,状态为’B’时的计算过程

下面都是在log_sum_exp中的临时变量


代码
def forward_algorithm(self, e_matrix):
# matrix 是在当前状态下总路径之和
init_matrix = torch.full((BATCH_SIZE, 1, tags_size), -10000.0)
init_matrix[:, 0, self.s2i[START_TAG]] = 0.
# 前一步的最优值
pre_matrix = init_matrix
# 循环时间
for i in range(SEQ_LEN):
# 保存当前时间步的的路径值
matrix_value = []
# 循环状态
for s in range(tags_size):
# 计算发射分数, (BATCH_SIZE, 1, tags_size)
e_score = e_matrix[:, i, s].view(BATCH_SIZE, 1, -1).expand(BATCH_SIZE, 1, tags_size)
# 计算转移分数 (1,tags_size)
t_score = self.t_score[s, :].view(1, -1)
# 下一步的得分 (BATCH_SIZE, 1, tags_size)
next_matrix = pre_matrix + e_score + t_score
# self.log_sum_exp(next_matrix) (BATCH_SIZE, 1)
matrix_value.append(self.log_sum_exp(next_matrix))
# 在把其记录到pre_matrix变量中
pre_matrix = torch.cat(matrix_value, dim=-1).view(BATCH_SIZE, 1, -1)
# 最终的变量:之前的得分+转移到终点的得分 (BATCH_SIZE, 1, tags_size)
terminal_var = pre_matrix + self.t_score[self.s2i[STOP_TAG], :]
alpha = self.log_sum_exp(terminal_var)
# (BATCH_SIZE,1)
return alpha
可看到这里也不太明白为什么这样做可以得到所有路径之和,其实,这样做无非是为了简化运算,但这样计算的不足在于使用了很多遍logsumexp,这就和原先的值其实有一些差距。
理想值
s c o r e i d e a l = l o g ( e S 1 + e S 2 + . . . + e S N ) (1) score_{ideal} = log(e^{S_1}+e^{S_2}+...+e^{S_N})\tag1 scoreideal=log(eS1+eS2+...+eSN)(1)
现实值
s c o r e r e a l i t y = l o g ( ∑ e p r e + t ) = l o g ( ∑ e l o g ( ∑ e p r e + t + e s ) + t ) = . . . (2) \begin{aligned} score_{reality} &= log(\sum e^{pre+t})\\ &= log(\sum e^{log(\sum e^{pre+t+es})+t})\\ &=... \end{aligned}\tag2 scorereality=log(∑epre+t)=log(∑elog(∑epre+t+es)+t)=...(2)
t->t_score
es->e_score
pre->pre_matrix

如上图所示,*球处已经计算了从<START>到"我",前一步所有状态到B的全部路径得分S1,求logsumexp(S1)记录到*球处,同理球处则是前两步所有路径到达"爱",并且所有状态转移至B的全部路径得分S2,求logsumexp(S2)记录到球处。
至此,你学废了吗?
边栏推荐
- 【历史上的今天】6 月 23 日:图灵诞生日;互联网奠基人出生;Reddit 上线
- A set of sai2 brushes is finally finished! Share with everyone!
- Skills in schematic merging
- Figure out the difference between MIT, BSD and Apache open source protocols
- Jenkins - Copy Artifact 插件 Build 之间数据共享传递
- Differences between cesium polygon extrudedheight and height
- 低代码DSL里面在数仓中的实践
- stm32f1中断介绍
- 【历史上的今天】6 月 11 日:蒙特卡罗方法的共同发明者出生;谷歌推出 Google 地球;谷歌收购 Waze
- Low code solution - a low code solution for digital after-sales service covering the whole process of work order, maintenance and Finance
猜你喜欢

Jenkins - Pipeline concept and creation method

The system administrator has set the system policy to prohibit this installation. Solution

Mysql大合集,你要内容的这里全都有

SQL 注入绕过(二)

图灵机启动顺序

Jenkins - email notification plug-in

CVPR22收录论文|基于标签关系树的层级残差多粒度分类网络

Embedded must learn! Detailed explanation of hardware resource interface - based on arm am335x development board (Part 2)

【历史上的今天】6 月 2 日:苹果推出了 Swift 编程语言;电信收购联通 C 网;OS X Yosemite 发布

Jenkins - Groovy Postbuild 插件丰富 Build History 信息
随机推荐
【历史上的今天】6 月 23 日:图灵诞生日;互联网奠基人出生;Reddit 上线
Scoped attribute and lang attribute in style
SQL injection bypass (V)
Jenkins - 内置变量访问
Learn pickle
技术人员如何成为技术领域专家
Based on am335x development board arm cortex-a8 -- acontis EtherCAT master station development case
fiddle如何使用代理
Explanation of OSI layer 7 model (easy to understand in Chinese)
系统管理员设置了系统策略,禁止进行此安装。解决方案
Appium automation test foundation - Supplement: app package name and appactivity
Ti am3352/54/59 industrial core board hardware specification
数智学习 | 流批一体实时数仓建设路径探索
High reliability application knowledge map of Architecture -- the path of architecture evolution
【历史上的今天】6 月 1 日:Napster 成立;MS-DOS 原作者出生;谷歌出售 Google SketchUp
The system administrator has set the system policy to prohibit this installation. Solution
SQL 注入绕过(二)
mysql面试百题集
How to use metauniverse technology to create a better real world
Cesium color color (assignment) random color