前言
原文地址:https://blog.csdn.net/m0_48742971/article/details/123305342,严禁转载。
MarkupLM是微软最近开源的适用于网页信息抽取的深度学习预训练模型。
代码:https://github.com/microsoft/unilm/
本文是针对数据预处理部分的源代码的学习笔记。重点解析Xpath嵌入的生成原理。
以下图片来自MarkupLM的Github仓库
将 XPath 转换为 Embedding 向量的示例可参见上图。对于一个 XPath 表达式,模型首先将其按层级切分,得到不同深度上的 XPath 单元,而每个单元又包含了标签的名称及下标。对于无下标的单元,则统一将其下标设置为0。在每层深度上,XPath Embedding模块均含有一个独立的标签嵌入表与下标嵌入表。因此每个 XPath 单元均会产生两个向量,分别表示名称与下标,随后两个向量相加即可得到各 XPath 单元的表征。为了保留单元之间的层次信息,该模块随后会将所有单元的表示按原有位置进行拼接操作,得到整个 XPath 表达式的表示向量。最后,考虑到该表示向量与其他原有 Embedding 向量之间的维度不一致,模型采用了一个前馈神经网络(FFN)层将该向量的维度进行转换,从而保持统一,该过程中引入的非线性激活函数也进一步增强了模型的表达能力。
(这段文字摘录自https://zhuanlan.zhihu.com/p/438072814)
上面这段文字已经把原理解释得很清楚了,是源代码最好的注释,看过源码之后再读,会更有收获。
1. 源码
class XPathEmbeddings(nn.Module):
"""Construct the embddings from xpath -- tag and subscript"""
# we drop tree-id in this version, as its info can be covered by xpath
def __init__(self, config):
super(XPathEmbeddings, self).__init__()
# 默认配置,来自config MarkupLMConfig
# max_xpath_tag_unit_embeddings=256,
# max_xpath_subs_unit_embeddings=1024,
# xpath_unit_hidden_size=32,
# max_depth=50,
self.max_depth = config.max_depth
# nn.Linear
#
self.xpath_unitseq2_embeddings = nn.Linear(
config.xpath_unit_hidden_size * self.max_depth, config.hidden_size)
self.dropout = nn.Dropout(config.hidden_dropout_prob)
self.activation = nn.ReLU()
self.xpath_unitseq2_inner = nn.Linear(config.xpath_unit_hidden_size * self.max_depth, 4 * config.hidden_size)
self.inner2emb = nn.Linear(4 * config.hidden_size, config.hidden_size)
self.xpath_tag_sub_embeddings = nn.