简单神经网络(2)

简单神经网络(2)

1.词袋模型(bow : 离散、高维、稀疏)

在自然语言处理和文本分析的问题中,词袋(Bag of Words, BOW)和词向量(Word Embedding)是两种最常用的模型。更准确地说,词向量只能表征单个词,如果要表示文本,需要做一些额外的处理。所谓BOW,就是将文本/Query看作是一系列词的集合。

  • 离散:无法衡量词向量之间的关系, 比如酒店、宾馆、旅社 三者都只在某一个固定的位置为 1 ,所以找不到三者的关系,各种度量(与或非、距离)都不合适,即太稀疏,很难捕捉到文本的含义。
  • 高维:词表维度随着语料库增长膨胀,n-gram 序列随语料库膨胀更快。

  • 数据稀疏: 数据都没有特征多,数据有 100 条,特征有 1000 个

举个例子:
文本1:苏宁易购/是/国内/著名/的/B2C/电商/之一
这是一个短文本。“/”作为词与词之间的分割。从中我们可以看到这个文本包含“苏宁易购”,“B2C”,“电商”等词。换句话说,该文本的的词袋由“苏宁易购”,“电商”等词构成。就像这样:

但计算机不认识字,只认识数字,那在计算机中怎么表示词袋模型呢?其实很简单,给每个词一个位置/索引就可以了。例如,我们令“苏宁易购”的索引为0,“电商”的索引为1,其他以此类推。则该文本的词袋就变成了:

词袋是在词集的基础上增加了频率的维度,词集只关注有和没有,词袋还要关注有几个。

假设我们要对一篇文章进行特征化,最常见的方式就是词袋。

假如现在有1000篇新闻文档,把这些文档拆成一个个的字,去重后得到3000个字,然后把这3000个字作为字典,进行文本表示的模型,叫做词袋模型。这种模型的特点是字典中的字没有特定的顺序,句子的总体结构也被舍弃了.

  • 2.分布式表示(连续,稠密,低维)

分布式(distributed)描述的是把
信息分布式地存储在向量的各个维度中,与之相对的是局部表示(local
representation),如词的独热表示(one-hot representation),在高维向量中
只有一个维度描述了词的语义。一般来说,通过矩阵降维或神经网络降维
可以将语义分散存储到向量的各个维度中,因此,这类方法得到的低维向 量一般都可以称作分布式表示。

2.word2vec代码实现

3.bow2vec + TFIDF模型

主要内容为:
拆分句子为单词颗粒,记号化;
生成词典;
生成稀疏文档矩阵

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
documents = ["Human machine interface for lab abc computer applications",
"A survey of user opinion of computer system response time",
"The EPS user interface management system",
"System and human system engineering testing of EPS",
"Relation of user perceived response time to error measurement",
"The generation of random binary unordered trees",
"The intersection graph of paths in trees",
"Graph minors IV Widths of trees and well quasi ordering",
"Graph minors A survey"]

# 分词并根据词频剔除
# remove common words and tokenize
stoplist = set('for a of the and to in'.split())
texts = [[word for word in document.lower().split() if word not in stoplist]
for document in documents]

#分词并根据词频剔除
#将句子划分为词
stoplist=set('for a of the end to in'.split())
texts=[[word for word in document.lower().split() if word not in stoplist] for document in documents]
print(texts)

#生成词典
from gensim import corpora
import os
dictionary=corpora.Dictionary(texts)
dictionary.save(os.path.join('C:/Users/wuxun/Desktop/','deerwester.dict'))#存储词典#路径拼接函数
print(dictionary)
print(dictionary.token2id)
tfidf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#生成词袋
new_doc="Human computer interaction Human"
new_vec=dictionary.doc2bow(new_doc.lower().split())
print(new_vec)
# the word "interaction" does not appear in the dictionary and is ignored
# [(0, 1), (1, 1)] ,词典(dictionary)中第0个词,出现的频数为1(当前句子),
# 第1个词,出现的频数为1
#多句bow生成
[dictionary.doc2bow(text) for text in texts]
#print(texts)

from gensim import corpora,models,similarities
corpus = [[(0, 1.0), (1, 1.0), (2, 1.0)],
[(2, 1.0), (3, 1.0), (4, 1.0), (5, 1.0), (6, 1.0), (8, 1.0)],
[(1, 1.0), (3, 1.0), (4, 1.0), (7, 1.0)],
[(0, 1.0), (4, 2.0), (7, 1.0)],
[(3, 1.0), (5, 1.0), (6, 1.0)],
[(9, 1.0)],
[(9, 1.0), (10, 1.0)],
[(9, 1.0), (10, 1.0), (11, 1.0)],
[(8, 1.0), (10, 1.0), (11, 1.0)]]
tfidf=models.TfidfModel(corpus)

#词袋模型,实践
vec=[(0,1),(4,1),(9,1)]
print(tfidf[vec])
#查找vec中,0,4,9号三个词的TFIDF值。同时进行转化,把之前的文档矩阵中的词频变成了TFIDF值。
#利用tfidf求相似:

index=similarities.SparseMatrixSimilarity(tfidf[corpus],num_features=12)
vec=[(0,1),(4,1),(9,1)]
sims=index[tfidf[vec]]
#对corpus中的九个文档进行文档级别索引,vec是新文档的词袋,sim就是该vec向量对corpus中九个文档的相似性
print(list(enumerate(sims)))