sent2vec

2021SC@SDUSC

环境配置

EmbedRank在标准数据集上实现了比基于图形的最先进系统更高的 F分数,适用于实时处理大量Web数据。利用EmbedRank,我们还为新短语引入了基于嵌入的最大边际相关性(MMR),从而显式地增加了所选关键词的覆盖率和多样性。一项包括200多张选票的用户研究表明,虽然减少短语的语义重叠不会导致F分的增加,但我们的高度多样性选择是人类更喜欢的。
保证了关键词的两个最具挑战性的性质:由候选短语与整个文档的嵌入距离获得的信息性;由候选短语之间的距离表示的多样性。


相关信息

采用了公开可用的预先训练的Sent2Vec3和Doc2ve4模型。

装置

1、下载完整的斯坦福CoreNLP Tagger版本3.8.0
2、安装sent2vec

TLDR:该库提供单词、短文本或句子的数字表示(特征),可作为任何机器学习任务的输入。
代码建立在Facebook的FastText库的基础上

在这里插入图片描述

本地安装

安装要求:

This is the implementation of the following paper: https://arxiv.org/abs/1801.04470

# Installation

## Local Installation

1. Download full Stanford CoreNLP Tagger version 3.8.0
http://nlp.stanford.edu/software/stanford-corenlp-full-2018-02-27.zip

2. Install sent2vec from 
https://github.com/epfml/sent2vec
    * Clone/Download the directory
    * go to sent2vec directory
    * git checkout f827d014a473aa22b2fef28d9e29211d50808d48
    * make
    * pip install cython
    * inside the src folder 
        * ``python setup.py build_ext``
        * ``pip install . ``
        * (In OSX) If the setup.py throws an **error** (ignore warnings), open setup.py and add '-stdlib=libc++' in the compile_opts list.        
    * Download a pre-trained model (see readme of Sent2Vec repo) , for example wiki_bigrams.bin

在这里插入图片描述

sent2vec安装

要编译库,只需运行’make’命令。
Cython模块允许您在推断句子嵌入时将模型保存在内存中。要编译和安装模块,请从项目根文件夹运行以下命令:
pip install .
因为安装了sent2vec 所以命令如下:
pip install sent2vec

代码如下(示例):

# Setup and Requirements
Our code builds upon [Facebook's FastText library](https://github.com/facebookresearch/fastText), see also their nice documentation and python interfaces.
To compile the library, simply run a `make` command.
A Cython module allows you to keep the model in memory while inferring sentence embeddings. In order to compile and install the module, run the following from the project root folder:
pip install .
## Note -  
if you install sent2vec using
$ pip install sent2vec
then you'll get the wrong package. Please follow the instructions in the README.md to install it correctly.

Sentence Embeddings

为了生成句子表示,采用sent2vec方法,并需要了解相关的代码和模型。可以将其视为FastText的无监督版本,以及word2vec(CBOW)对句子的扩展。
该方法使用一个简单但有效的无监督目标来训练句子的分布式表示。该算法在大多数基准任务上优于最先进的无监督模型,在许多任务上甚至优于有监督模型,突出了生成的句子嵌入的鲁棒性。

Generating Features from Pre-Trained Models

安装Cython模块,则可以推断句子嵌入,同时将模型保留在内存中:

import sent2vec
model = sent2vec.Sent2vecModel()
model.load_model('model.bin')
emb = model.embed_sentence("once upon a time .") 
embs = model.embed_sentences(["first sentence .", "another sentence"])

文本预处理(标记化和小写)不由模块处理,需要检查wikiTokenize.py以使用NLTK和Stanford NLP进行标记化。

Cython模块的另一种替代方法是使用从预先训练的模型笔记本中获取句子嵌入的python代码。它处理标记化,可以给它原始的句子,但不将模型保存在内存中。

多进程运行推理

Cython模块中有一种用于加载模型的“推理”模式,该模式将模型的输入矩阵加载到共享内存段中,而不加载推理不需要的输出矩阵。这是对使用多个独立进程运行推理的用例的优化,否则每个进程都需要将模型的副本加载到它们的地址空间中。要使用它:

model.load_model('model.bin', inference_mode=True)

模型将加载到以模型名称命名的共享内存段中。模型将保留在内存中,直到明确删除共享内存段。要从Python执行此操作,执行以下操作:

model.release_shared_mem('model.bin')

使用命令行界面
给定一个预先训练过的model.bin(下载链接见下文),下面是如何为输入文本生成句子特征。要生成特征,请使用“打印句子向量”命令,输入文本文件需要以每行一句的形式提供:

./fasttext print-sentence-vectors model.bin < text.txt

这将输出句子向量(每个输入句子的特征)到标准输出,每行一个向量。这也可用于管道:

cat text.txt | ./fasttext print-sentence-vectors model.bin

从预训练模型中提取单词嵌入

安装Cython包装器,一些功能将会允许使用从sent2vec或cbow-c+w-ngrams获得的单词嵌入:

import sent2vec
model = sent2vec.Sent2vecModel()
model.load_model('model.bin') # The model can be sent2vec or cbow-c+w-ngrams
vocab = model.get_vocabulary() # Return a dictionary with words and their frequency in the corpus
uni_embs, vocab = model.get_unigram_embeddings() # Return the full unigram embedding matrix
uni_embs = model.embed_unigrams(['dog', 'cat']) # Return unigram embeddings given a list of unigrams
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值