word2vec

Python 结巴分词 + Word2Vec利用维基百科训练词向量

流过昼夜 提交于 2019-11-28 18:21:43
结巴分词 是一个跨语言的中文分词器,整体效果还算不错,功能也够用,这里直接用Python了,其他主流语言版本均有提供。 Word2Vec,起源于谷歌的一个项目,在我刚开始接触的时候就关注到了他的神奇,大致是通过深度神经网络把词映射到N维空间,处理成向量之后我们终于可以在自然语言处理上方便的使用它进行一些后续处理。 Python的 gensim 库中有 word2vec 包,我们使用这个就可以了,接下来我们就对维基百科进行处理,作为训练集去训练。(包地址: http://radimrehurek.com/gensim/models/word2vec.html ) 本文参考: http://www.52nlp.cn/中英文维基百科语料上的word2vec实验 处理 使用维基百科的数据很方便,一是Wiki给我们提供了现成的语料库(听说是实时更新的),虽然中文体积不大,但比起自己爬来方便了不少。 如果使用英文那就更棒了,非常适合作为语料库。 当然只是说在通用的情况下,在专业词汇上,经过测试效果比较一般(考虑到专业词库有专业wiki,以及中文词条本身也不太多)。 首先,我们把Wiki处理成Text格式待处理的文本,这一步在本文参考中有现成的代码。 process_wiki_data.py #!/usr/bin/env python # -*- coding: utf-8 -*- #

word2vec模型cbow与skip-gram的比较

ぃ、小莉子 提交于 2019-11-28 17:31:37
cbow和skip-gram都是在word2vec中用于将文本进行向量表示的实现方法,具体的算法实现细节可以去看word2vec的原理介绍文章。我们这里大体讲下两者的区别,尤其注意在使用当中的不同特点。 在 cbow方法中,是用周围词预测中心词 ,从而利用中心词的预测结果情况,使用GradientDesent方法,不断的去调整周围词的向量。当训练完成之后,每个词都会作为中心词,把周围词的词向量进行了调整,这样也就获得了整个文本里面所有词的词向量。 要注意的是, cbow的对周围词的调整是统一的:求出的gradient的值会同样的作用到每个周围词的词向量当中去。 可以看到,cbow预测行为的次数跟整个文本的词数几乎是相等的(每次预测行为才会进行一次backpropgation, 而往往这也是最耗时的部分),复杂度大概是O(V); 而 skip-gram是用中心词来预测周围的词 。在skip-gram中,会利用周围的词的预测结果情况,使用GradientDecent来不断的调整中心词的词向量,最终所有的文本遍历完毕之后,也就得到了文本所有词的词向量。 可以看出,skip-gram进行预测的次数是要多于cbow的:因为每个词在作为中心词时,都要使用周围词进行预测一次。这样相当于比cbow的方法多进行了K次(假设K为窗口大小),因此时间的复杂度为O(KV),训练时间要比cbow要长。

How to train Word2vec on very large datasets?

删除回忆录丶 提交于 2019-11-28 17:09:42
问题 I am thinking of training word2vec on huge large scale data of more than 10 TB+ in size on web crawl dump. I personally trained c implementation GoogleNews-2012 dump (1.5gb) on my iMac took about 3 hours to train and generate vectors (impressed with speed). I did not try python implementation though :( I read somewhere that generating vectors on wiki dump (11gb) of 300 vector length takes about 9 days to generate. How to speed up word2vec? Do i need to use distributed models or what type of

Doc2vec: How to get document vectors

 ̄綄美尐妖づ 提交于 2019-11-28 15:53:04
How to get document vectors of two text documents using Doc2vec? I am new to this, so it would be helpful if someone could point me in the right direction / help me with some tutorial I am using gensim. doc1=["This is a sentence","This is another sentence"] documents1=[doc.strip().split(" ") for doc in doc1 ] model = doc2vec.Doc2Vec(documents1, size = 100, window = 300, min_count = 10, workers=4) I get AttributeError: 'list' object has no attribute 'words' whenever I run this. If you want to train Doc2Vec model, your data set needs to contain lists of words (similar to Word2Vec format) and

Why are multiple model files created in gensim word2vec?

我与影子孤独终老i 提交于 2019-11-28 12:06:12
When I try to create a word2vec model (skipgram with negative sampling) I received 3 files as output as follows. word2vec (File) word2vec.syn1nef.npy (NPY file) word2vec.wv.syn0.npy (NPY file) I am just worried why this happens as for my previous test examples in word2vec I only received one model(no npy files). Please help me. Models with larger internal vector-arrays can't be saved via Python 'pickle' to a single file, so beyond a certain threshold, the gensim save() method will store subsidiary arrays in separate files, using the more-efficient raw format of numpy arrays ( .npy format). You

词向量技术原理及应用详解(二)

感情迁移 提交于 2019-11-28 11:10:04
当前文本向量化主流的方式是word2vec词向量技术,从基于统计的方法,到基于神经网络的方法,掌握word2vec词向量技术是学习文本向量化的最好的方式 下面是Tomas MIkolov的三篇有关word embedding的文章: 1、Efficient Estimation of Word Representation in Vector Space, 2013 2、Distributed Representations of Sentences and Documents, 2014 3、Enriching Word Vectors with Subword Information, 2016 因此严格意义上说,word2vec是一个2013年出现的新方法,15年以后逐渐流行起来。 Word2vec的几个关键点 什么是word2vec? word2vec是词的一种表示,它将词以固定维数的向量表示出来。是用来将一个个的词变成词向量的工具。例如:“我爱中国”这句话通过分词为 我/ 爱/ 中国。那么这时候三个词都将表示为n维的词向量。中国 = [x1,x2,…,xn] 为什么要用word2vec?word2vec有什么好处。 传统的基于词袋模型 one-hot representation在判定同义词,相似句子的时候很无力。 例如在一个只有两个词的词典中。快递被编码为v1 = [0

四步理解GloVe!(附代码实现)

两盒软妹~` 提交于 2019-11-28 09:52:32
1. 说说GloVe 正如GloVe论文的标题而言, GloVe的全称叫Global Vectors for Word Representation,它是一个基于全局词频统计(count-based & overall statistics)的词表征(word representation)工具,它可以把一个单词表达成一个由实数组成的向量,这些向量捕捉到了单词之间一些语义特性,比如相似性(similarity)、类比性(analogy)等。 我们通过对向量的运算,比如欧几里得距离或者cosine相似度,可以计算出两个单词之间的语义相似性。 2. GloVe的实现步骤 2.1 构建共现矩阵 什么是共现矩阵? 共现矩阵顾名思义就是共同出现的意思,词文档的共现矩阵主要用于发现主题(topic),用于主题模型,如LSA。 局域窗中的word-word共现矩阵可以挖掘语法和语义信息, 例如: I like deep learning. I like NLP. I enjoy flying 有以上三句话,设置滑窗为2,可以得到一个词典: {"I like","like deep","deep learning","like NLP","I enjoy","enjoy flying","I like"} 。 我们可以得到一个共现矩阵(对称矩阵):

Why are word embedding actually vectors?

回眸只為那壹抹淺笑 提交于 2019-11-28 09:30:39
I am sorry for my naivety, but I don't understand why word embeddings that are the result of NN training process (word2vec) are actually vectors. Embedding is the process of dimension reduction, during the training process NN reduces the 1/0 arrays of words into smaller size arrays, the process does nothing that applies vector arithmetic. So as result we got just arrays and not the vectors. Why should I think of these arrays as vectors? Even though, we got vectors, why does everyone depict them as vectors coming from the origin (0,0)? Again, I am sorry if my question looks stupid. What are

NLP系列文章:子词嵌入(fastText)的理解!(附代码)

耗尽温柔 提交于 2019-11-28 07:07:50
1. 什么是fastText 英语单词通常有其内部结构和形成⽅式。例如,我们可以从“dog”“dogs”和“dogcatcher”的字⾯上推测它们的关系。这些词都有同⼀个词根“dog”,但使⽤不同的后缀来改变词的含义。而且,这个关联可以推⼴⾄其他词汇。 在word2vec中,我们并没有直接利⽤构词学中的信息。⽆论是在跳字模型还是连续词袋模型中,我们都将形态不同的单词⽤不同的向量来表⽰。例如, “dog”和“dogs”分别⽤两个不同的向量表⽰,而模型中并未直接表达这两个向量之间的关系。鉴于此,fastText提出了⼦词嵌⼊(subword embedding)的⽅法,从而试图将构词信息引⼊word2vec中的CBOW。 这里有一点需要特别注意,一般情况下,使用fastText进行文本分类的同时也会产生词的embedding,即embedding是fastText分类的产物。除非你决定使用预训练的embedding来训练fastText分类模型,这另当别论。 2. n-gram表示单词 word2vec把语料库中的每个单词当成原子的,它会为每个单词生成一个向量。这忽略了单词内部的形态特征,比如:“book” 和“books”,“阿里巴巴”和“阿里”,这两个例子中,两个单词都有较多公共字符,即它们的内部形态类似,但是在传统的word2vec中

Convert word2vec bin file to text

梦想与她 提交于 2019-11-28 03:08:12
From the word2vec site I can download GoogleNews-vectors-negative300.bin.gz. The .bin file (about 3.4GB) is a binary format not useful to me. Tomas Mikolov assures us that "It should be fairly straightforward to convert the binary format to text format (though that will take more disk space). Check the code in the distance tool, it's rather trivial to read the binary file." Unfortunately, I don't know enough C to understand http://word2vec.googlecode.com/svn/trunk/distance.c . Supposedly gensim can do this also, but all the tutorials I've found seem to be about converting from text, not the