如何训练一个模型来计算两个新闻标题之间的相似度得分?

How to train a model that will result in the similarity score between two news titles?

我正在尝试构建一个假新闻分类器,我在这个领域还很陌生。我有一个标题为假新闻的专栏 "title_1_en" 和另一个名为 "title_2_en" 的专栏。有3个目标标签; "agreed"、"disagreed" 和 "unrelated" 如果第 "title_2_en" 栏中的新闻标题与第一栏中的标题一致、不一致或无关。

在将句子的单词转换为向量后,我尝试计算两个标题之间的基本余弦相似度。这导致了余弦相似度得分,但这需要大量改进,因为根本没有考虑同义词和语义关系。

def L2(vector):
    norm_value = np.linalg.norm(vector)
    return norm_value

def Cosine(fr1, fr2):
    cos = np.dot(fr1, fr2)/(L2(fr1)*L2(fr2))
    return cos

这里最重要的是如何将这两个句子转换为向量。有多种方法可以做到这一点,最天真的方法是:

  • 将每个单词转换为向量 - 这可以使用标准的预训练向量(例如 word2vec 或 GloVe)来完成。
  • 现在每个句子都只是一袋词向量。这需要转换为单个向量,即,将完整的句子文本映射到向量。也有很多方法可以做到这一点。首先,只需取句子中向量袋的平均值。
  • 计算两个句子向量之间的余弦相似度。

Spacy's similarity 是开始平均技术的好地方。来自文档:

By default, spaCy uses an average-of-vectors algorithm, using pre-trained vectors if available (e.g. the en_core_web_lg model). If not, the doc.tensor attribute is used, which is produced by the tagger, parser and entity recognizer. This is how the en_core_web_sm model provides similarities. Usually the .tensor-based similarities will be more structural, while the word vector similarities will be more topical. You can also customize the .similarity() method, to provide your own similarity function, which can be trained using supervised techniques.