CS224N第一课作业--词向量与共现矩阵

这篇具有很好参考价值的文章主要介绍了CS224N第一课作业--词向量与共现矩阵。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

Word Vectors

1. import repos

from gensim.models import KeyedVectors
from gensim.test.utils import datapath
import matplotlib.pyplot as plt
plt.rcParams['figure.figsize'] = [10, 5]
import nltk
from nltk.corpus import reuters
import numpy as np
import random
import scipy as sp
from sklearn.decomposition import TruncatedSVD
from sklearn.decomposition import PCA

START_TOKEN = '<START>'
END_TOKEN = '<END>'

np.random.seed(0)
random.seed(0)
  1. Word VectorsWord Embeddings 通常可以互换使用
  2. 词向量通常用作下游NLP任务的基本组成部分,例如问题回答,文本生成,翻译等,因此对它们的优缺点进行一些直观了解非常重要。
  3. 在这里,您将探索两种类型的词向量: 从 co-occurrence matrices 派生的词向量,以及GloVe现成的词向量。gensim 是一个加载现成词向量的库, nltk 库可用于加载各种语料, 作为示例, 这里使用 reuters (路透社, 商业和金融新闻)语料库。
  4. 如下展示了一个 co-occurrence matrices 的示例, 窗口大小为 1, 对于一个文档内的某一个单词w(token), 我们每次都统计w周围 n 个单词(左边n个加上右边n个: [w-n, w+n])与之共同出现的次数.
Document 1: "all that glitters is not gold"
Document 2: "all is well that ends well"
* <START> all that glitters is not gold well ends <END>
<START> 0 2 0 0 0 0 0 0 0 0
all 2 0 1 0 1 0 0 0 0 0
that 0 1 0 1 0 0 0 1 1 0
glitters 0 0 1 0 1 0 0 0 0 0
is 0 1 0 1 0 1 0 1 0 0
not 0 0 0 0 1 0 1 0 0 0
gold 0 0 0 0 0 1 0 0 0 1
well 0 0 1 0 1 0 0 0 1 1
ends 0 0 1 0 0 0 0 1 0 0
<END> 0 0 0 0 0 0 1 1 0 0

这里, all<START> 共同出现的次数为2, 可以看到矩阵是对称而且稀疏的, 并且其大小为 V, V 是语料库中所有可能出现的单词数量. 注意: 在NLP中,我们经常添加 <START><END> 标记来表示句子,段落或文档的开头和结尾。在这种情况下,我们想象 <START><END> 标记封装每个文档,例如,“<START> All that glitters is not gold <END>”,并将这些 token 包括在我们的共现计数中。

2. Read corpus and calculate co-occurrence matrices

2-1 read_corpus

在这里,我们将使用路透社 (商业和金融新闻) 语料库。语料库由10,788个新闻文档组成,总计130万个单词。这些文档涵盖90个类别,分为train和test。有关详细信息,请参阅 https://www.nltk.org/book/ch02.html 我们在下面提供了一个 read_corpus 函数,该函数仅从 “黄金” 类别 (即有关黄金,采矿等的新闻文章) 中提取文章。该函数还向每个文档添加 <START><END> 标记,以及将单词转为小写。您不必执行任何其他类型的预处理。

def read_corpus(category="gold"):
    files = reuters.fileids(category)
    return [[START_TOKEN] + [w.lower() for w in list(reuters.words(f))] + [END_TOKEN] for f in files]
reuters_corpus = read_corpus()
print(reuters_corpus[1])
"""
['<START>', 'belgium', 'to', 'issue', 'gold', 'warrants', ',', 'sources', 'say', 'belgium', 'plans',
 'to', 'issue', 'swiss', 'franc', 'warrants', 'to', 'buy', 'gold', ',', 'with', 'credit', 'suisse',
 'as', 'lead', 'manager', ',', 'market', 'sources', 'said', '.', 'no', 'confirmation', 'or',
 'further', 'details', 'were', 'immediately', 'available', '.', '<END>']
"""
2-2 vocabulary

遍历语料库 reuters_corpus 统计所有出现的单词, 为它们排序, 返回词表和词表长度.

def distinct_words(corpus):
    corpus_words = []
    n_corpus_words = -1
    for doc in corpus:
        corpus_words += doc
    corpus_words = list(set(corpus_words))
    corpus_words.sort()
    n_corpus_words = len(corpus_words)
    return corpus_words, n_corpus_words
2-3 co occurrence matrices

为词汇表的每个词汇建立索引, 该索引对应于 co occurrence matrices 中单词的索引, 因此若词表的大小为 V, 则 co occurrence matrices 的大小为 (V, V).

def compute_co_occurrence_matrix(corpus, window_size=4):
    words, n_words = distinct_words(corpus)
    M = np.zeros((n_words, n_words))
    word2ind = {}
    # word index map
    for i, w in enumerate(words):
        word2ind[w]=i
    # second, fill the co-occurrence matrix. remind that <Start> and <End> are also included
    for doc in corpus:
        for wi in range(len(doc)):
            s = (wi-window_size) if (wi-window_size)>0 else 0
            e = (wi+window_size) if (wi+window_size)<len(doc) else len(doc)-1
            for i in range(s,e+1):
                if i != wi:
                    M[word2ind[doc[wi]]][word2ind[doc[i]]] += 1
    return M, word2ind
2-4. dimensionality reduction

通过 SVD 分解将共现矩阵降维至2维, 可以进行一定的可视化.

def reduce_to_k_dim(M, k=2):
    n_iters = 10
    print("Running Truncated SVD over %i words..." % (M.shape[0]))
    svd = TruncatedSVD(n_components=k, n_iter=n_iters)
    svd.fit(M)
    M_reduced = svd.transform(M)
    print("Done.")
    return M_reduced

def plot_embeddings(M_reduced, word2ind, words):
    for w in words:
        wi = word2ind[w]
        plt.scatter(M_reduced[wi,0], M_reduced[wi,1], marker='x', c='r')
        plt.text(M_reduced[wi,0], M_reduced[wi,1], w, alpha=0.8)
    plt.show()
    plt.close()
完整性检查

接下来读入词库, 生成词表, 计算共现矩阵, 最后执行降维, 挑选一部分词汇将他们可视化。

reuters_corpus = read_corpus()
M_co_occurrence, word2ind_co_occurrence = compute_co_occurrence_matrix(reuters_corpus)
M_reduced_co_occurrence = reduce_to_k_dim(M_co_occurrence, k=2)
# Rescale (normalize) the rows to make them each of unit-length
M_lengths = np.linalg.norm(M_reduced_co_occurrence, axis=1)
M_normalized = M_reduced_co_occurrence / M_lengths[:, np.newaxis] # broadcasting

words = ['value', 'gold', 'platinum', 'reserves', 'silver', 'metals', 'copper', 'belgium', 'australia', 'china', 'grammes', "mine"]

plot_embeddings(M_normalized, word2ind_co_occurrence, words)

CS224N第一课作业--词向量与共现矩阵,机器学习,nlp,python,word2vec

相似的聚集的单词:
第一组,copper和platinum,它们都属于金属矿物
第二组,belgium 和 australia,它们都属于国家名
类似的但没有聚集的单词:
第一组,gold 和 sliver
第二组,belgium 和china

3. Prediction-Based Word Vectors

加载 GloVe 词向量库, 它包含 400000 个单词的200维向量编码. 为了避免内存不足, 这里先随机采样 10000 个单词, 把它们合并为矩阵, 同时返回单词编号字典.

def get_matrix_of_vectors(wv_from_bin, required_words):
    import random
    words = list(wv_from_bin.index_to_key)
    print("Shuffling words ...")
    random.seed(225)
    random.shuffle(words)
    words = words[:10000]
    print("Putting %i words into word2ind and matrix M..." % len(words))
    word2ind = {}
    M = []
    curInd = 0
    for w in words:
        try:
            M.append(wv_from_bin.get_vector(w))
            word2ind[w] = curInd
            curInd += 1
        except KeyError:
            continue
    for w in required_words:
        if w in words:
            continue
        try:
            M.append(wv_from_bin.get_vector(w))
            word2ind[w] = curInd
            curInd += 1
        except KeyError:
            continue
    M = np.stack(M)
    print("Done.")
    return M, word2ind

随后, 我们同样应用降维到 M 矩阵, 并应用一些可视化分析.

M, word2ind = get_matrix_of_vectors(wv_from_bin, words)
M_reduced = reduce_to_k_dim(M, k=2)

# Rescale (normalize) the rows to make them each of unit-length
M_lengths = np.linalg.norm(M_reduced, axis=1)
M_reduced_normalized = M_reduced / M_lengths[:, np.newaxis] # broadcasting

words = ['value', 'gold', 'platinum', 'reserves', 'silver', 'metals', 'copper', 'belgium', 'australia', 'china', 'grammes', "mine"]

plot_embeddings(M_reduced_normalized, word2ind, words)

CS224N第一课作业--词向量与共现矩阵,机器学习,nlp,python,word2vec

余弦相似度

现在我们有了词向量,我们需要一种方法来量化单个词之间的相似性,根据这些向量。一种这样的度量是余弦相似性。我们将使用它来找到彼此 “接近” 和 “远离” 的单词。 s s s between two vectors p p p and q q q is defined as:
s = p ⋅ q ∣ ∣ p ∣ ∣ ∣ ∣ q ∣ ∣ ,  where  s ∈ [ − 1 , 1 ] s = \frac{p \cdot q}{||p|| ||q||}, \textrm{ where } s \in [-1, 1] s=∣∣p∣∣∣∣q∣∣pq, where s[1,1]

print(wv_from_bin.most_similar("light"))
"""
[('bright', 0.6242774724960327), ('dark', 0.6141002178192139), ('lights', 0.6013951897621155), ('lighter', 0.558175265789032), ('heavy', 0.5408364534378052), ('sunlight', 0.5362919569015503), ('blue', 0.5349379777908325), ('colored', 0.5282376408576965), ('sky', 0.5239452719688416), ('color', 0.513929009437561)]
"""

light 常见含义灯光、轻的、轻松的、点燃等等含义, 从输出来看, 这包含了它的同义词和反义词, 反义词例如 dark 是黑暗的, heavy 是重的, bule 忧郁的等等.

在考虑余弦相似性时,通常更方便地考虑余弦距离,即简单的1-余弦相似性。Find three words ( w 1 , w 2 , w 3 ) (w_1,w_2,w_3) (w1,w2,w3) where w 1 w_1 w1 and w 2 w_2 w2 are synonyms and w 1 w_1 w1 and w 3 w_3 w3 are antonyms, but Cosine Distance ( w 1 , w 3 ) < (w_1,w_3) < (w1,w3)< Cosine Distance ( w 1 , w 2 ) (w_1,w_2) (w1,w2).文章来源地址https://www.toymoban.com/news/detail-851443.html

w1 = "love"
w2 = "like"
w3 = "hate"
w1_w2_dist = wv_from_bin.distance(w1, w2)
w1_w3_dist = wv_from_bin.distance(w1, w3)

print("Synonyms {}, {} have cosine distance: {}".format(w1, w2, w1_w2_dist))
print("Antonyms {}, {} have cosine distance: {}".format(w1, w3, w1_w3_dist))

到了这里,关于CS224N第一课作业--词向量与共现矩阵的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • Python库第一课:基础Numpy知识(下):矩阵

            好的,我们今天继续来学习Numpy的基础,昨天,已经介绍完Numpy的成员之一——数组,今天,在接着介绍其另一大成员——矩阵,也是应用非常广泛的成员。         矩阵,在线性代数中是几乎贯穿全文的成员,因此,这里需要较高的线性代数的基础。在这里,默认

    2024年02月03日
    浏览(47)
  • CS224W4.2——计算PageRank

    在介绍了PageRank及其公式之后,我们讨论了求解PageRank的方法。我们提出了求解图的随机邻接矩阵(即PageRank)的主特征向量的幂次迭代方法。此外,我们在之前的PageRank实现中引入了两个问题:死角(dead ends)(没有外部链接的节点)和蜘蛛陷阱(spider traps)(没有外部链接的节点组

    2024年02月06日
    浏览(25)
  • Anaconda☀利用机器学习sklearn构建模型与实现丨第一课

    1️⃣了解什么是机器学习 2️⃣了解机器学习sklearn库的简介、安装 3️⃣掌握使用sklearn转换器处理数据的方法 4️⃣构建并评价线性回归模型 1.1机器学习👑 机器学习是人工智能 (AI) 和计算机科学的分支,专注于使用数据和算法来模仿人类学习的方式,逐渐提高其准确性。

    2024年02月05日
    浏览(39)
  • 机器学习 -- 矩阵和向量

    在之前的knn算法和余弦算法等算法中,都有很重要的概念,叫做矩阵和向量。这个是机器学习中很重要的概念。今天来深入学习一些矩阵和向量的一些知识。 向量是一个有序的数字列表,可以在几何中表示为从原点出发的箭头。在机器学习中,向量通常用于表示数据点或特征

    2024年02月21日
    浏览(73)
  • 【机器学习-03】矩阵方程与向量求导方法

      在铺垫了基础矩阵和线性代数的相关知识后,我们现在尝试将【机器学习-01】中提到的方程组表示形式转化为矩阵形式,并利用矩阵方法来求解相关方程。同时,在【机器学习-01】中,我们已经初步探讨了最小二乘法这一优化算法的基本思想。最小二乘法是一个基础而重

    2024年03月20日
    浏览(32)
  • 机器学习——线性代数中矩阵和向量的基本介绍

    矩阵的基本概念(这里不多说,应该都知道) 而向量就是一个特殊的矩阵,即向量只有一列,是个n*1的矩阵 注 :一般矩阵用大写字母表示,向量用小写字母表示 先从简单开始,即一个矩阵和一个向量相乘的运算 矩阵相乘的结果的维度为 m*k 矩阵乘法满足结合律不满足交换律

    2024年02月21日
    浏览(36)
  • AE第一课

    无脑HD1080 30 锚点是图层运动的中心点 涉及到的快捷键有:锚点回家ctrl+art+home 图层回家ctrl+home 锚点快捷键A shift 吸附一个轴移动 shift 等比例缩放 v选取工具 滚轮抓手工具 +缩放工具

    2024年02月16日
    浏览(35)
  • 网络第一课

    ✍ 如何理解局域网和广域网? ✍ 路由器和交换机是怎样工作的? ✍ 三层交换机能不能代替路由器? --  1.局域网  2. 广域网   -- -- 企业网络   运营商架构    数据中心架构 -- 局域网  - 内网 - 私网 -- 通过 交换机 连接的  转发 相同IP地址段 的数据 --  终端  --  电脑 

    2024年02月06日
    浏览(34)
  • 第一课:目标检测基础

    当今计算机视觉领域的一项重要任务是目标检测,而YOLO系列算法已经成为了该领域的重要代表。如果您想了解如何在实际应用中使用YOLO算法实现高效目标检测,那么本付费专栏合集将是您的不二选择。本专栏将由211硕士生我编写,其中包括YOLO算法的基础知识、高级技巧以及

    2023年04月22日
    浏览(36)
  • Word第一课

    文档格式指的是文件的扩展名,例如下图 对于该文件, .docx 就是文件扩展名,我们可以称这个文档为docx格式的文档。有的时候,如果我们没有设置显示文件扩展名,需要手动进行设置,从而让文件的扩展名显示出来。 对于win10系统,如下图进行设置,需要勾选中 文件扩展名

    2024年02月21日
    浏览(31)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包