短文本语义相似度模型实际输出与模型定义不一致
Created by: IceFlameWorm
短文本语义相匹配(models/PaddleNLP/similarity_net/)中预训练了一个SimNet-BOW-Pairwise语义匹配模型,通过源码(models/PaddleNLP/similarity_net/run_classifier.py)追溯到用到的模型定义是models/PaddleNLP/models/matching/bow.py中的BOW,BOW的定义如下:
class BOW(object):
"""
BOW
"""
def __init__(self, conf_dict):
"""
initialize
"""
self.dict_size = conf_dict["dict_size"]
self.task_mode = conf_dict["task_mode"]
self.emb_dim = conf_dict["net"]["emb_dim"]
self.bow_dim = conf_dict["net"]["bow_dim"]
def predict(self, left, right):
"""
Forward network
"""
# embedding layer
emb_layer = layers.EmbeddingLayer(self.dict_size, self.emb_dim, "emb")
left_emb = emb_layer.ops(left)
right_emb = emb_layer.ops(right)
# Presentation context
pool_layer = layers.SequencePoolLayer("sum")
left_pool = pool_layer.ops(left_emb)
right_pool = pool_layer.ops(right_emb)
softsign_layer = layers.SoftsignLayer()
left_soft = softsign_layer.ops(left_pool)
right_soft = softsign_layer.ops(right_pool)
# matching layer
if self.task_mode == "pairwise":
bow_layer = layers.FCLayer(self.bow_dim, "relu", "fc")
left_bow = bow_layer.ops(left_soft) # 不会有负值**
right_bow = bow_layer.ops(right_soft)
cos_sim_layer = layers.CosSimLayer()
pred = cos_sim_layer.ops(left_bow, right_bow)
return left_bow, pred
else:
concat_layer = layers.ConcatLayer(1)
concat = concat_layer.ops([left_soft, right_soft])
bow_layer = layers.FCLayer(self.bow_dim, "relu", "fc")
concat_fc = bow_layer.ops(concat)
softmax_layer = layers.FCLayer(2, "softmax", "cos_sim")
pred = softmax_layer.ops(concat_fc)
return left_soft, pred
结合models/PaddleNLP/models/matching/bow.py和models/PaddleNLP/similarity_net/run_classifier.py可知,在pairwise模式下,模型会对外输出left_bow和pred,根据模型定义可知,left_bow是由left_bow = bow_layer.ops(left_soft)得到,而bow_layer = layers.FCLayer(self.bow_dim, "relu", "fc")是一个接了‘relu’的全连接层,所以left_bow中不应该有负值。
在尝试运行 bash run.sh infer 的过程中(实际上运行的是run_classifer.py),如果在run_classifier.py的infer函数中打印 output[0](对应BOW模型输出的left_bow),会发现里面包含负值,这与模型定义的left_bow的输出(relu不会有负值出现)不一致。