doc2vec介绍和实践

article/2025/8/23 16:21:09

简介

与其他方法的比较

bag of words (BOW):不会考虑词语出现的先后顺序。

Latent Dirichlet Allocation (LDA):更偏向于从文中提取关键词和核心思想extracting topics/keywords out of texts,但是非常难调参数并且难以评价模型的好坏。

基石:word2vec

Word2vec 是一种计算效率特别高的预测模型,用于学习原始文本中的字词嵌入。它分为两种类型:连续词袋模型 (CBOW) 和 Skip-Gram 模型。从算法上看,这些模型比较相似,只是 CBOW 从源上下文字词(“the cat sits on the”)中预测目标字词(例如“mat”),而 skip-gram 从目标字词中预测源上下文字词。这种调换似乎是一种随意的选择,但从统计学上来看,它有助于 CBOW 整理很多分布信息(通过将整个上下文视为一个观察对象)。在大多数情况下,这对于小型数据集来说是很有用的。但是,skip-gram 将每个上下文-目标对视为一个新的观察对象,当我们使用大型数据集时,skip-gram 似乎能发挥更好的效果。

CBOW: Continuous bag of words creates a sliding window around current word, to predict it from “context” — the surrounding words. Each word is represented as a feature vector. After training, these vectors become the word vectors.

Doc2Vec形成

word2vec + document-unique feature vector = when training the word vectors W, the document vector D is trained as well, and in the end of training, it holds a numeric representation of the document.

Distributed Memory version of Paragraph Vector (PV-DM). It acts as a memory that remembers what is missing from the current context — or as the topic of the paragraph. While the word vectors represent the concept of a word, the document vector intends to represent the concept of a document.

another algorithm, which is similar to skip-gram may be used Distributed Bag of Words version of Paragraph Vector (PV-DBOW). 

参数

Parameters:
  • documents (iterable of list of TaggedDocument, optional) – Input corpus, can be simply a list of elements, but for larger corpora,consider an iterable that streams the documents directly from disk/network. If you don’t supply documents (or corpus_file), the model is left uninitialized – use if you plan to initialize it in some other way.
  • corpus_file (stroptional) – Path to a corpus file in LineSentence format. You may use this argument instead of documents to get performance boost. Only one of documents or corpus_file arguments need to be passed (or none of them, in that case, the model is left uninitialized). Documents’ tags are assigned automatically and are equal to line number, as in TaggedLineDocument.
  • dm ({1,0}optional) – Defines the training algorithm. If dm=1, ‘distributed memory’ (PV-DM) is used. Otherwise, distributed bag of words (PV-DBOW) is employed.
  • vector_size (intoptional) – Dimensionality of the feature vectors.
  • window (intoptional) – The maximum distance between the current and predicted word within a sentence.
  • alpha (floatoptional) – The initial learning rate.
  • min_alpha (floatoptional) – Learning rate will linearly drop to min_alpha as training progresses.
  • seed (intoptional) – Seed for the random number generator. Initial vectors for each word are seeded with a hash of the concatenation of word + str(seed). Note that for a fully deterministically-reproducible run, you must also limit the model to a single worker thread (workers=1), to eliminate ordering jitter from OS thread scheduling. In Python 3, reproducibility between interpreter launches also requires use of the PYTHONHASHSEED environment variable to control hash randomization.
  • min_count (intoptional) – Ignores all words with total frequency lower than this.
  • max_vocab_size (intoptional) – Limits the RAM during vocabulary building; if there are more unique words than this, then prune the infrequent ones. Every 10 million word types need about 1GB of RAM. Set to None for no limit.
  • sample (floatoptional) – The threshold for configuring which higher-frequency words are randomly downsampled, useful range is (0, 1e-5).
  • workers (intoptional) – Use these many worker threads to train the model (=faster training with multicore machines).
  • epochs (intoptional) – Number of iterations (epochs) over the corpus.
  • hs ({1,0}optional) – If 1, hierarchical softmax will be used for model training. If set to 0, and negative is non-zero, negative sampling will be used.
  • negative (intoptional) – If > 0, negative sampling will be used, the int for negative specifies how many “noise words” should be drawn (usually between 5-20). If set to 0, no negative sampling is used.
  • ns_exponent (floatoptional) – The exponent used to shape the negative sampling distribution. A value of 1.0 samples exactly in proportion to the frequencies, 0.0 samples all words equally, while a negative value samples low-frequency words more than high-frequency words. The popular default value of 0.75 was chosen by the original Word2Vec paper. More recently, in https://arxiv.org/abs/1804.04212, Caselles-Dupré, Lesaint, & Royo-Letelier suggest that other values may perform better for recommendation applications.
  • dm_mean ({1,0}optional) – If 0 , use the sum of the context word vectors. If 1, use the mean. Only applies when dm is used in non-concatenative mode.
  • dm_concat ({1,0}optional) – If 1, use concatenation of context vectors rather than sum/average; Note concatenation results in a much-larger model, as the input is no longer the size of one (sampled or arithmetically combined) word vector, but the size of the tag(s) and all words in the context strung together.
  • dm_tag_count (intoptional) – Expected constant number of document tags per document, when using dm_concat mode.
  • dbow_words ({1,0}optional) – If set to 1 trains word-vectors (in skip-gram fashion) simultaneous with DBOW doc-vector training; If 0, only trains doc-vectors (faster).
  • trim_rule (functionoptional) –

    Vocabulary trimming rule, specifies whether certain words should remain in the vocabulary, be trimmed away, or handled using the default (discard if word count < min_count). Can be None (min_count will be used, look to keep_vocab_item()), or a callable that accepts parameters (word, count, min_count) and returns either gensim.utils.RULE_DISCARDgensim.utils.RULE_KEEP or gensim.utils.RULE_DEFAULT. The rule, if given, is only used to prune vocabulary during current method call and is not stored as part of the model.

    The input parameters are of the following types:

    • word (str) - the word we are examining
    • count (int) - the word’s frequency count in the corpus
    • min_count (int) - the minimum count threshold.
  • callbacks – List of callbacks that need to be executed/run at specific stages during training.

 

踩坑记录

在最开始使用doc2vec的时候我只label了测试集和验证集的数据(因为这两个是已经被标注的数据分离出来的train_test_split)。结果在预测真正的测试集的数据时就出现了问题。doc2vec目前只能预测他已经见过的词,那些文本样例可以不需要标注,但是必须见过。个人认为这是doc2vec一个比较大的缺点。所以只能再将测试集数据label一遍。我这里说的label可不是对数据分类(不是该文本样例属于哪一个class),具体label代码见下。

from gensim.models import doc2vecdef label_sentences(corpus, label_type):"""Gensim's Doc2Vec implementation requires each document/paragraph to have a label associated with it.We do this by using the TaggedDocument method. The format will be like "TRAIN_i" where "i" isa dummy index of the complaint narrative."""labeled = []for i, v in enumerate(corpus):label = label_type + '_' + str(i)labeled.append(doc2vec.TaggedDocument(v.split(), [label]))return labeled

接着,doc2vec处理文本之后拿到了一个二维的数组。后面接了一个普通的神经网络进行预测。但是发现效果并不好。就想着要不然把这个二维的数组喂给GNU/LSTM看看。当时GNU报错说expected dim=3但是只拿到了dim=2,于是我在参考了网上一些文档之后把x(文本样例),y(分类的标注)reshape分别成了(shape[0], shape[1], 1), 其实就是为了凑够三个维度。模型倒是能跑了。不过我发现loss根本就不下降。

其实doc2vec处理文本以后根本就不可以接GNU/LSTM。因为这两个是循环神经网络,需要序列性的模型,而doc2vec处理文本之后已经没有序列性了。

 

 

 

 

 

 

参考:

  • https://medium.com/scaleabout/a-gentle-introduction-to-doc2vec-db3e8c0cce5e

 


http://chatgpt.dhexx.cn/article/hKa16woC.shtml

相关文章

doc2vec java_word2vec和doc2vec

word2vec基本思想 通过训练每个词映射成k维实数向量(k一般为模型中的超参数)&#xff0c;通过词之间的距离来判断语义相似度。 word2vec采用一个三层的神经网络。 训练的时候按照词频将每个词语Huffman编码&#xff0c;词频越高的词语对应的编码越短。这三层的神经网络本身是对…

Doc2vec

目录 一&#xff1a;背景 二&#xff1a;基本原理 2.1:PV-DM 2.2:PV-DBOW 2.3:和word2vec区别 2.4:预测新文本的向量 三&#xff1a;代码实战 3.1:接口介绍 3.2:主要代码 一&#xff1a;背景 之前总结了Word2vec训练词向量的细节&#xff0c;讲解了一个词是如何通过wor…

关于doc2vec

原文地址&#xff1a;https://blog.csdn.net/john_xyz/article/details/79208564 1.“句向量”简介 word2vec提供了高质量的词向量&#xff0c;并在一些任务中表现良好。 关于word2vec的原理可以参考这几篇论文&#xff1a; https://arxiv.org/pdf/1310.4546.pdfhttps://arx…

doc2vec java_doc2vec

gensim 是处理文本的很强大的工具包&#xff0c;基于python环境下&#xff1a; 1.gensim可以做什么&#xff1f; 它可以完成的任务&#xff0c;参加gensim 主页API中给出的介绍&#xff0c;链接如下&#xff1a; http://radimrehurek.com/gensim/apiref.html 2.word2vec的使用 …

Doc2Vec的简介及应用(gensim)

作者:Gidi Shperber 在本文中&#xff0c;你将学习什么是doc2vec&#xff0c;它是如何构建的&#xff0c;它与word2vec有什么关系&#xff0c;你能用它做什么&#xff0c;并且没有复杂的数学公式。 介绍 文本文档的量化表示在机器学习中是一项具有挑战性的任务。很多应用都…

Doc2Vec模型介绍及使用

Doc2Vec模型 Doc2Vec模型 摘要背景段落向量 PV-DM模型PV-DBOW模型gensim实现Doc2Vec说明参考文献摘要 通过本文,你将了解到: Doc2Vec模型是如何产生的Doc2Vec模型细节Doc2Vec模型的特点Doc2Vec的使用及代码(gensim)背景 Doc2Vec模型的产生要从词向量表示(论文word2vec模型)开…

Doc2Vec - 计算文档之间的相似性

本文旨在向您介绍 Doc2Vec 模型&#xff0c;以及它在计算文档之间的相似性时如何提供帮助。 目录 前言 一、Word2Vec 1.Skip-Gram 2.Continuous Bag-of-Words (CBOW) 二、Doc2Vec 1.Distributed Memory version of Paragraph Vector (PV-DM) 2.Words version of Paragra…

Doc2Vec模型的介绍与gensim中Doc2Vec的使用

文章目录 一、Doc2Vec模型1 、PV-DM2 、PV-DBOW 二、gensim实现1、gensim实现Doc2Vec&#xff08;IMDB数据集&#xff09;2、gensim实现Doc2Vec&#xff08;中文数据集&#xff09; 三、总结四、程序编写时遇到的错误&#xff1a;gensim包中相关函数说明&#xff1a; 参考资料&…

如何自学游戏引擎的开发?

PS&#xff1a;题猪分得清游戏和游戏引擎的区别&#xff0c;所以各位答主不需要劳神解释两者的区别关系什么的了 PS&#xff1a;这里的游戏引擎暂时指图形模块&#xff0c;其他的声音&#xff0c;物理&#xff0c;网络&#xff0c;UI等等模块暂时不考虑 题猪一直自学编程&#…

游戏开发完整学习路线(各个版本都有)

来自&#xff1a;微浪科技 作者&#xff1a;若朝若曦 在软件开发中&#xff0c;游戏开发这个方向看起来目标很明确&#xff0c;但其实是个领域很广的方向&#xff0c;入门的时候如果得不到指点一二&#xff0c;很容易误入歧途&#xff0c;相反&#xff0c;如果走这条路之前能…

智力开发小游戏集含游戏过程中数据存取-C#入门教学程序

对于初学C#程序开发的学员&#xff0c;一般进行采取开发小游戏程序&#xff0c;这样做首先不会让学员失去学习的兴趣&#xff0c;其次可以将C#中基本的控件与类的写法整合到这些游戏程序中&#xff0c;再次将对数据库的操作也教给学员。通过几年的观察这样的教学有它的好处。所…

游戏开发所需要的知识

从放弃求职回家已经一个半月了&#xff0c;一直都在备考事业编。发现这玩意比游戏开发简单太多了&#xff0c;没有人刁难&#xff0c;没有人催促&#xff0c;几个月举办一次&#xff0c;一天只需要学习3-4个小时&#xff0c;其余时间都是自由安排&#xff0c;太舒服了。考上编后…

零基础游戏开发笔记1——游戏开发流程

万事开头难&#xff0c;多学多练习&#xff0c;熟悉游戏开发的主要流程&#xff0c;莫要强行记忆。 首先&#xff0c;我们来了解一下游戏的开发流程。 第一就是立案&#xff0c;建立策划案。 策划案包含很多东西&#xff0c;包括游戏介绍、游戏内容、游戏模型介绍、游戏数值、…

游戏开发流程之完整指南

“现在&#xff0c;是时候改进您的游戏开发流程了。在这里&#xff0c;无论您是在独立的初创公司亦或大型游戏工作室中&#xff0c;我们都可以调度资源&#xff0c;使您的工作室的开发和设计工作晋升一个层次。” 您可以把本指引当做游戏开发流程改进的参考 我们将覆盖所有您…

游戏开发笔记(二)——开发流程和项目管理

前一篇说到分工&#xff0c;这里再说说流程和开发管理。 组织形式 从公司角度来看一个游戏工作室是一个业务比较独立的研发部门&#xff0c;研发方面的大小事务&#xff08;除了立项&#xff09;拥有高度自治权。而从一个工作室角度来看&#xff0c;通常内部又由多个项目组组成…

游戏开发 - 开发流程 - 收集

1.应用场景 主要用于了解&#xff0c;掌握游戏开发的整个流程。 2.学习/操作 1.文档阅读 复习课 | 带你梳理客户端开发的三个重点-极客时间 2.整理输出 2.1 游戏开发流程 -- 参考 按照游戏开发中的三大模块策划、程序、美术&#xff0c;画了一个图。 开发游戏的时候&#xf…

游戏开发完整流程

1. 立项 一个项目立项的原因可能性非常多&#xff0c;有可能是公司拿到一个好的IP&#xff0c;也有可能是几个负责人有个很棒的idea&#xff0c;亦或是老板的梦想是做一个XX类型的游戏&#xff0c;这边不做过多的讨论。 立项过程中应该包含市场调查和产品定位&#xff0c…

如何开发一款游戏?【游戏开发所需技能和开发流程】

开发一款游戏需要的技能包括&#xff1a;编程、设计、音效和项目管理。每个角色都需要掌握其特定领域的知识和技能&#xff0c;并与其他团队成员合作。在本文中&#xff0c;我们将深入探讨如何开发一款游戏。 1. 游戏开发流程 游戏开发流程可以分为以下几个阶段&#xff1a; …

如何开发一款游戏:游戏开发流程及所需工具

本文来自作者 goto先生 在 GitChat 上分享 「如何开发一款游戏&#xff1a;游戏开发流程及所需工具」 编辑 | 哈比 游戏作为娱乐生活的一个方面&#xff0c;参与其中的人越来越多&#xff0c;而大部分参与其中的人都是以玩家的身份。 他们热爱一款游戏&#xff0c;或是被游戏的…

文本识别CRNN模型介绍以及pytorch代码实现

提示&#xff1a;文章写完后&#xff0c;目录可以自动生成&#xff0c;如何生成可参考右边的帮助文档 文章目录 前言一、CRNN模型介绍1.模型结构2.CTCLossbeam search 二、使用pytorch实现crnn数据集 前言 文本识别是图像领域的一个常见任务&#xff0c;场景文字识别OCR任务中…