Giter VIP home page Giter VIP logo

transformerdssm's Introduction

基于Transformer的语义相似度计算模型DSSM

该模型在DSSM模型的基础上,将模型的表示层使用基于Transformer的Encoder部分来实现,匹配层将通过表示层得到问题query和答案answer的特征表示进行余弦相似度计算,由于问题i除了与答案i相匹配以外,其余答案均为问题i的负样本,因此需要对每一个问题进行负采样。

模型调用方式

模型的调用代码位于目录:/NlpModel/SimNet/TransformerDSSM/Debug.py,其调用方式主要分为以下三种。

1.模型训练

TransformerDSSM模型的训练通过调用文件中的函数dssm_model_train实现,该函数以两个参数作为输入:

(1)faq_dict,该参数是一个问答对组成的列表,列表中的每一个元素均为一个问答对字典;

(2)embedding_dict,该参数是一个字典,字典中的每一个key是一个字符,value是该字符对应的字向量。字向量的提供位于目录:MOdel/CharactersEmbedding.json

2.模型推理

TransformerDSSM模型的推理通过调用文件中的函数dssm_model_infer实现,该函数以五个参数作为输入,需要注意的是,模型的推理返回结果,是输入答案的位置索引:

(1)queries,该参数是一系列需要去匹配的问题组成的列表,列表中的每一个元素是一个问题字符串;

(2)answer_embedding,该参数是由一系列待匹配的答案经过表示层所提取的特征向量组成的列表,列表中的每一个元素是一个答案对应的特征向量,之所以用特征向量直接作为待匹配答案的输入,是为了减少数据经过表示层的计算时间,提高匹配效率;

(3)embedding_dict,该参数是一个字典,字典中的每一个key是一个字符,value是该字符对应的字向量。

(4)top_k,该参数表示当输入一个问题时,需要从待匹配的答案中返回top_k个候选答案,默认时,该参数的值为1;

(4)threshold,该参数通过设置语义相似度计算的阈值,当待匹配的答案其相似度低于给定阈值时,则不返回,高于则返回。

3.表示层特征向量提取

TransformerDSSM模型的表示层特征向量提取通过调用文件中的函数dssm_model_extract_t_pre实现,该函数以两个参数作为输入:

(1)faq_dict,该参数是答案候选列表;

(2)embedding_dict,该参数是一个字典,字典中的每一个key是一个字符,value是该字符对应的字向量。

模型训练数据

本模块提供的训练数据,是作为预训练模型的训练数据,主要分为以下两种,其中SameFAQ表示问题,答案指向同一句子,各问答对间的语义完全独立,可用于进行语义空间划分,SimFAQ中的问答对则是语义相近的,用于语义相似度训练,该训练数据位于目录:/TrainData/

已训练模型库

经过实验发现,4块RTX-2080Ti 训练大约2个小时左右即可收敛。效果最好的预训练模型为经过SimFAQ训练后的模型。模型的参数为:hidden_num=256attention_num=512。可增量的模型位于/readyModel/SimFAQ/

transformerdssm's People

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.