C1W3.Assignment: Hello Vectors

news2024/9/19 20:47:56

理论课:C1W3.Vector Space Models

文章目录

  • Importing the data
  • 生成word_embeddings_subset(optional)
  • Predict relationships among words
    • Cosine Similarity
    • Euclidean distance
  • Finding the country of each capital
  • Model Accuracy
  • Plotting the vectors using PCA

理论课: C1W3.Vector Space Models

本次作业的目的:

  • 预测单词之间的类比,例如:男人vs女人,相当于:国王vs??
  • 使用 PCA 降低词嵌入的维度,并将其绘制成二维图。
  • 使用相似度量(余弦相似度)比较词嵌入。
  • 了解这些向量空间模型的工作原理。

Predict the Countries from Capitals任务说明:要求从一个国家中找出该国的首都来说明单词类比,编写一个程序,让它能根据国家,给出其对应首都。

Importing the data

导入包,没装的pip install 安装

# Run this cell to import packages.
import pickle
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import w3_unittest

from utils import get_vectors

数据集以 Pandas DataFrame 的形式加载。阿如果数据量较大,这可能需要几分钟时间、这可能需要几分钟时间。

data = pd.read_csv('./data/capitals.txt', delimiter=' ')
data.columns = ['city1', 'country1', 'city2', 'country2']

# print first five elements in the DataFrame
data.head(5)

在这里插入图片描述

生成word_embeddings_subset(optional)

由于原始的谷歌新闻单词嵌入数据集约为 3.64 G,有条件的同学可以自行下载该数据集,提取出将在本作业中分析的单词样本,并将其保存在名为word_embeddings_subset.p的 pickle 文件中。
自己提取单词词向量看下面:

  • 从这里下载已经训练好的谷歌新闻单词嵌入数据集。
  • 在页面中搜索 "GoogleNews-vectors-negative300.bin.gz "并点击链接下载。
  • 需要解压该文件。
import nltk
from gensim.models import KeyedVectors


embeddings = KeyedVectors.load_word2vec_format('./GoogleNews-vectors-negative300.bin', binary = True)
f = open('capitals.txt', 'r').read()
set_words = set(nltk.word_tokenize(f))
select_words = words = ['king', 'queen', 'oil', 'gas', 'happy', 'sad', 'city', 'town', 'village', 'country', 'continent', 'petroleum', 'joyful']
for w in select_words:
    set_words.add(w)

def get_word_embeddings(embeddings):

    word_embeddings = {}
    for word in embeddings.vocab:
        if word in set_words:
            word_embeddings[word] = embeddings[word]
    return word_embeddings


# Testing your function
word_embeddings = get_word_embeddings(embeddings)
print(len(word_embeddings))
pickle.dump( word_embeddings, open( "word_embeddings_subset.p", "wb" ) )

也可以从绑定资源中直接下载word_embeddings_subset.p,并保存到data目录下,然后加载为 dictionary

word_embeddings = pickle.load(open("./data/word_embeddings_subset.p", "rb"))
len(word_embeddings)  # there should be 243 words that will be used in this assignment

结果:
243
每个单词是300维的:

print("dimension: {}".format(word_embeddings['Spain'].shape[0]))

结果:
dimension: 300

Predict relationships among words

接下来编写一个函数,利用单词嵌入来预测单词之间的关系。

  • 函数将吃三个单词。
  • 前两个词相互关联。
  • 它将预测第四个单词,该单词与第三个单词的关系与前两个单词的关系类似。
  • 例如,“雅典之于希腊,就像曼谷之于 ______”?
  • 编写一个能够找到第四个单词的程序。

Cosine Similarity

余弦相似度公式为:
cos ⁡ ( θ ) = A ⋅ B ∥ A ∥ ∥ B ∥ = ∑ i = 1 n A i B i ∑ i = 1 n A i 2 ∑ i = 1 n B i 2 (1) \cos (\theta)=\frac{\mathbf{A} \cdot \mathbf{B}}{\|\mathbf{A}\|\|\mathbf{B}\|}=\frac{\sum_{i=1}^{n} A_{i} B_{i}}{\sqrt{\sum_{i=1}^{n} A_{i}^{2}} \sqrt{\sum_{i=1}^{n} B_{i}^{2}}}\tag{1} cos(θ)=A∥∥BAB=i=1nAi2 i=1nBi2 i=1nAiBi(1)
A A A B B B 代表词向量, A i A_i Ai B i B_i Bi 代表该向量的索引 i。

  • 如果 A A A B B B 完全相同,则 c o s ( θ ) = 1 cos(\theta)=1 cos(θ)=1
  • 否则,如果它们完全相反,即 A = − B A=-B A=B,那么你将得到 c o s ( θ ) = − 1 cos(\theta) =-1 cos(θ)=1
  • 如果得到 c o s ( θ ) = 0 cos(\theta)=0 cos(θ)=0,则表示它们正交(或垂直)。
  • 0 和 1 之间的数字表示相似度得分。
  • 介于-1和0之间的数字表示非相似度得分。
# UNQ_C1 GRADED FUNCTION: cosine_similarity

def cosine_similarity(A, B):
    '''
    Input:
        A: a numpy array which corresponds to a word vector
        B: A numpy array which corresponds to a word vector
    Output:
        cos: numerical number representing the cosine similarity between A and B.
    '''

    ### START CODE HERE ###
    dot = np.dot(A,B)
    norma = np.sqrt(np.dot(A,A))
    normb = np.sqrt(np.dot(B,B))
    cos = dot/(norma*normb)

    ### END CODE HERE ###
    return cos

测试:

# feel free to try different words
king = word_embeddings['king']
queen = word_embeddings['queen']

cosine_similarity(king, queen)

结果:
0.6510957

Euclidean distance

欧氏距离公式如下:
d ( A , B ) = d ( B , A ) = ( A 1 − B 1 ) 2 + ( A 2 − B 2 ) 2 + ⋯ + ( A n − B n ) 2 = ∑ i = 1 n ( A i − B i ) 2 \begin{aligned} d(\mathbf{A}, \mathbf{B})=d(\mathbf{B}, \mathbf{A}) &=\sqrt{\left(A_{1}-B_{1}\right)^{2}+\left(A_{2}-B_{2}\right)^{2}+\cdots+\left(A_{n}-B_{n}\right)^{2}} \\ &=\sqrt{\sum_{i=1}^{n}\left(A_{i}-B_{i}\right)^{2}} \end{aligned} d(A,B)=d(B,A)=(A1B1)2+(A2B2)2++(AnBn)2 =i=1n(AiBi)2
其中:

  • n n n 是向量中元素的个数
  • A A A B B B 是相应的单词向量。
  • 词语越相似,欧氏距离越有可能接近 0。
# UNQ_C2 GRADED FUNCTION: euclidean

def euclidean(A, B):
    """
    Input:
        A: a numpy array which corresponds to a word vector
        B: A numpy array which corresponds to a word vector
    Output:
        d: numerical number representing the Euclidean distance between A and B.
    """

    ### START CODE HERE ###

    # euclidean distance    
    d = np.sqrt(np.sum((A-B)**2))

    ### END CODE HERE ###

    return d

测试:

# Test your function
euclidean(king, queen)

结果:
2.4796925

Finding the country of each capital

使用上面实现的函数计算向量之间的相似性,并利用这些相似性找出各国的首都。需要编写一个函数接收三个单词和词向量的字典。任务是找出各国首都。例如,给定以下单词:
1: Athens 2: Greece 3: Baghdad,
预测结果应该为:4: Iraq
函数编写时:
1.您可需要参考上述国王 - 男人 + 女人 = 皇后的示例,并使用单词嵌入和相似度函数将该方案转化为数学函数。
2.在词向量词典中迭代,计算向量和当前词嵌入之间的余弦相似度得分。
3.需要确保返回的单词与输入函数的单词不重复。
4.返回得分最高的单词。

# UNQ_C3 GRADED FUNCTION: get_country

def get_country(city1, country1, city2, embeddings, cosine_similarity=cosine_similarity):
    """
    Input:
        city1: a string (the capital city of country1)
        country1: a string (the country of capital1)
        city2: a string (the capital city of country2)
        embeddings: a dictionary where the keys are words and
    Output:
        countries: a dictionary with the most likely country and its similarity score
    """
    ### START CODE HERE ###

    # store the city1, country 1, and city 2 in a set called group
    group = set((city1, country1, city2))

    # get embeddings of city 1
    city1_emb = embeddings[city1]

    # get embedding of country 1
    country1_emb = embeddings[country1]

    # get embedding of city 2
    city2_emb = embeddings[city2]

    # get embedding of country 2 (it's a combination of the embeddings of country 1, city 1 and city 2)
    # Remember: King - Man + Woman = None
    vec = country1_emb-city1_emb+city2_emb

    # Initialize the similarity to -1 (it will be replaced by a similarities that are closer to +1)
    similarity = -1

    # initialize country to an empty string
    country = ''

    # loop through all words in the embeddings dictionary
    for word in embeddings.keys():

        # first check that the word is not already in the 'group'
        if word not in group:

            # get the word embedding
            word_emb = embeddings[word]

            # calculate cosine similarity between embedding of country 2 and the word in the embeddings dictionary
            cur_similarity = cosine_similarity(vec,word_emb)

            # if the cosine similarity is more similar than the previously best similarity...
            if cur_similarity > similarity:

                # update the similarity to the new, better similarity
                similarity = cur_similarity

                # store the country as a tuple, which contains the word and the similarity
                country = (word,similarity)

    ### END CODE HERE ###

    return country

测试:

# Testing your function, note to make it more robust you can return the 5 most similar words.
get_country('Athens', 'Greece', 'Cairo', word_embeddings)

结果:
(‘Egypt’, 0.7626821)

Model Accuracy

正确率计算公式为:
Accuracy = Correct # of predictions Total # of predictions \text{Accuracy}=\frac{\text{Correct \# of predictions}}{\text{Total \# of predictions}} Accuracy=Total # of predictionsCorrect # of predictions
借助上面的get_country函数,遍历每一个单词,计算出准确率。

# UNQ_C4 GRADED FUNCTION: get_accuracy

def get_accuracy(word_embeddings, data, get_country=get_country):
    '''
    Input:
        word_embeddings: a dictionary where the key is a word and the value is its embedding
        data: a pandas data frame as

    '''

    ### START CODE HERE ###
    # initialize num correct to zero
    num_correct = 0

    # loop through the rows of the dataframe
    for i, row in data.iterrows():

        # get city1
        city1 = row['city1']

        # get country1
        country1 = row['country1']

        # get city2
        city2 = row['city2']
        
        # get country2
        country2 = row['country2']

        # use get_country to find the predicted country2
        predicted_country2, _ = get_country(city1,country1,city2,word_embeddings)

        # if the predicted country2 is the same as the actual country2...
        if predicted_country2 == country2:
            # increment the number of correct by 1
            num_correct += 1

    # get the number of rows in the data dataframe (length of dataframe)
    m = len(data)

    # calculate the accuracy by dividing the number correct by m
    accuracy = num_correct /m

    ### END CODE HERE ###
    return accuracy

测试:

accuracy = get_accuracy(word_embeddings, data)
print(f"Accuracy is {accuracy:.2f}")

结果:
Accuracy is 0.92

Plotting the vectors using PCA

接下来使用主成分分析法principal component analysis (PCA)来探索降低词向量维度后它们之间的距离。现在的词向量是300维的,难以使用可视化的方式显示这些词向量,因此我们使用PCA将向量投射到一个维度更小的空间中,并尽量保持原始信息不丢失。可视化后相似的单词会相互聚集在一起。例如,“悲伤”、"快乐 "和 "喜悦 “都是描述情绪的词语,在绘制时应该相互靠近。这些词 石油”、"天然气 "和 "石油 “都是描述自然资源的词语。城市”、“村庄”、"城镇 "等词可视为同义词,描述的是类似的事物。
大概步骤如下:

  1. 对数据进行均值归一化处理
  2. 计算数据的协方差矩阵( Σ \Sigma Σ)。
  3. 计算协方差矩阵的特征向量和特征值
  4. 将前 K 个特征向量与归一化数据相乘。结果如下:

在这里插入图片描述

# UNQ_C5 GRADED FUNCTION: compute_pca


def compute_pca(X, n_components=2):
    """
    Input:
        X: of dimension (m,n) where each row corresponds to a word vector
        n_components: Number of components you want to keep.
    Output:
        X_reduced: data transformed in 2 dims/columns + regenerated original data
    pass in: data as 2D NumPy array
    """

    ### START CODE HERE ###
    # mean center the data
    X_demeaned = X - np.mean(X,axis=0)

    # calculate the covariance matrix
    covariance_matrix = np.cov(X_demeaned, rowvar=False)

    # calculate eigenvectors & eigenvalues of the covariance matrix
    eigen_vals, eigen_vecs = np.linalg.eigh(covariance_matrix, UPLO='L')

    # sort eigenvalue in increasing order (get the indices from the sort)
    idx_sorted = np.argsort(eigen_vals)
    
    # reverse the order so that it's from highest to lowest.
    idx_sorted_decreasing = idx_sorted[::-1]

    # sort the eigen values by idx_sorted_decreasing
    eigen_vals_sorted = eigen_vals[idx_sorted_decreasing]

    # sort eigenvectors using the idx_sorted_decreasing indices
    eigen_vecs_sorted = eigen_vecs[:,idx_sorted_decreasing]

    # select the first n eigenvectors (n is desired dimension
    # of rescaled data array, or dims_rescaled_data)
    eigen_vecs_subset = eigen_vecs_sorted[:,0:n_components]

    # transform the data by multiplying the transpose of the eigenvectors with the transpose of the de-meaned data
    # Then take the transpose of that product.
    X_reduced = np.dot(eigen_vecs_subset.transpose(),X_demeaned.transpose()).transpose()

    ### END CODE HERE ###

    return X_reduced

测试:

# Testing your function
np.random.seed(1)
X = np.random.rand(3, 10)
X_reduced = compute_pca(X, n_components=2)
print("Your original matrix was " + str(X.shape) + " and it became:")
print(X_reduced)

结果:
Your original matrix was (3, 10) and it became:
[[ 0.43437323 0.49820384]
[ 0.42077249 -0.50351448]
[-0.85514571 0.00531064]]

接下来挑选11个单词,然后观察他们PCA降维后的结果:

words = ['oil', 'gas', 'happy', 'sad', 'city', 'town',
         'village', 'country', 'continent', 'petroleum', 'joyful']

# given a list of words and the embeddings, it returns a matrix with all the embeddings
X = get_vectors(word_embeddings, words)

print('You have 11 words each of 300 dimensions thus X.shape is:', X.shape)

结果:
You have 11 words each of 300 dimensions thus X.shape is: (11, 300)
降维:

# We have done the plotting for you. Just run this cell.
result = compute_pca(X, 2)
plt.scatter(result[:, 0], result[:, 1])
for i, word in enumerate(words):
    plt.annotate(word, xy=(result[i, 0] - 0.05, result[i, 1] + 0.1))

plt.show()

结果:
在这里插入图片描述

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1929459.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

性能测试学习-woniusales

1、性能测试分类:服务器,客户端 服务器端: 代码级多线程协议级多线程 客户端:JavaScript, Android ,PC客户端 性能测试容易出现瓶颈的点:带宽,代码优化(例,等差,等比…

hbase命令行操作

1.进入命令行 hbase shell 2.基础命令 查看帮助信息help --查看版本信息--version --查看hbase集群状态--status --查看当前用户--whoami 3.命名空间命令 -查看所有命名空间--list_namespace --查询指定命名空间的表 --list_namespace_tables 命名空间名 --创建命名空间 --cre…

字节抖音电商 后端开发岗位 一面

笔者整理答案,以供参考 自我介绍 项目(20分钟) RocketMQ延时消息的底层实现 回答: 延时消息的实现主要依赖于RocketMQ中的定时任务机制。消息被发送到Broker时,会先存储在一个特定的延时消息队列中。Broker会定时扫…

怎么选流量套餐最划算呢,这篇文章建议收藏!

据小编了解,现在大多数用户手上都不止一张SIM卡,大部分都是双卡,甚至三卡了,那么,这些卡槽你真的利用对了吗? 这篇文章就告诉大家,如何更好的利用这两个卡槽,让你即省钱&#xff0c…

Day11: 110.平衡二叉树 257. 二叉树的所有路径 404.左叶子之和 222.完全二叉树的节点个数

题目110. 平衡二叉树 - 力扣(LeetCode) /*** Definition for a binary tree node.* struct TreeNode {* int val;* TreeNode *left;* TreeNode *right;* TreeNode() : val(0), left(nullptr), right(nullptr) {}* TreeNode(int x) …

元服务体验-服务管理与分享

服务管理 通过桌面、负一屏、应用市场、元服务等场景对元服务进行添加、收藏、移除等管理操作。 服务分享 元服务与服务卡片支持近场与远场分享,可流转给设备也可以分享给联系人。 收到他人分享的元服务,可无需安装直接打开使用,或添加至负…

PEST分析法

PEST分析法是一种用于企业战略规划的工具,它通过对企业所处的宏观环境进行分析,帮助企业识别出影响其战略决策的关键因素。PEST分别代表政治(Political)、经济(Economic)、社会(Sociocultural&a…

架构设计-NX的二次开发API架构设计介绍

1.与整体的关系 2.API设计目标 能够允许用户访问NX的所有UI工具组件,二次开发用户能够编写外观和运行行为类似NX的应用程序。能够允许用户直接访问NX数据模型即使底层数据结构和功能实现发生很大变化,API接口保持稳定,不会影响上层用户。 3…

数码暴龙机(电波暴龙机)彩色复刻版!!| 使用Python、PySide6、pixilart自制windows桌面宠物

一、前言 数码暴龙机(电波暴龙机)是万代公司发售的一系列与《数码兽》系列相关的液晶玩具商品。这些产品融合了养成和对战元素,为玩家提供了一种虚拟养成和战斗的娱乐体验。也是很多人的童年回忆。最近在B站刷到讲解暴龙通关的教程和视频&…

java——Junit单元测试

测试分类 黑盒测试:不输入代码,给输入值,看程序能够给出期望的值。 白盒测试:写代码,关注程序具体执行流程。 JUnit单元测试 一个测试框架,供java开发人员编写单元测试。 是程序员测试,即白…

qt 创建一个可以拖拽的矩形,简单实践

1.概要 需求&#xff0c;一个可以拖拽的矩形&#xff0c;鼠标接近边线点击变成可拖拽形状。 2.代码 #include <QApplication> #include <QGraphicsView> #include <QGraphicsScene> #include <QGraphicsRectItem> #include <QMouseEvent> #in…

教大家如何下载保存腾讯会议的视频到本地

引言&#xff1a; 大家好&#xff0c;今天教大家如何下载保存腾讯会议的视频到本地&#xff0c;我们这边是地方网络技术&#xff01; 很多时候&#xff0c;腾讯会议的视频是无法直接下载保存的。今天我们就教大家如何下载保存腾讯会议的视频到本地。方法非常简单一&#xff0…

基于Faster R-CNN的安全帽目标检测

基于Faster R-CNN的安全帽目标检测项目通常旨在解决工作场所&#xff0c;特别是建筑工地的安全监管问题。这类项目使用计算机视觉技术&#xff0c;特别是深度学习中的Faster R-CNN算法&#xff0c;来自动检测工人是否正确佩戴了安全帽&#xff0c;从而确保遵守安全规定并减少事…

iOS ------ 消息传递和消息转发

一&#xff0c;消息传递 在OC中&#xff0c;传递消息就是在对象上调用方法。 相对于C语言的方法就“静态绑定”的函数&#xff0c;在编译器就决定了运行时所要调用的函数。在OC中&#xff0c;如果向某对象传递消息&#xff0c;就会使用动态绑定机制来决定需要调用那个方法。调…

网易天音:网易云音乐推出的一站式AI音乐创作工具

网易天音是一款由网易云音乐推出的AI音乐创作工具&#xff0c;它为音乐爱好者和专业歌手提供了一个便捷高效的创作平台。用户可以通过输入灵感&#xff0c;利用AI技术辅助完成作词、作曲、编曲和演唱&#xff0c;生成初稿后还可以进行词曲协同调整&#xff0c;以满足个性化的音…

ScanImage介绍

ScanImage 是由 Howard Hughes Medical Institute (HHMI) 的 Janelia Research Campus 开发的一款开源显微镜控制软件&#xff0c;专门用于双光子显微镜和其他高端显微镜系统的控制和数据采集。它被广泛应用于神经科学和生物医学研究领域&#xff0c;以其高效的成像性能和灵活的…

CSA笔记2-文件管理命令

tree 以树状图显示多级目录 示例&#xff1a; [rootlocalhost ~]# tree haha/ haha/ └── 111 └── 222 2 directories, 0 files [rootlocalhost ~]# tree -L 1 haha/haha/ └── 111 echo > >> < << 示例&#xff1a; [rootxxx ~]#…

vs中的代码有红色波浪线怎么办?

vs中的代码有红色波浪线怎么办? 问题分析问题解决 问题分析 环境没有错,代码语法也没有错,程序可正常运行,但是程序在vs中出现了红色波浪线.(俗称:vs抽风了怎么办?) 问题解决 项目->重新扫描解决方案

工业物联网智能网关重要性及功能特点应用探索-天拓四方

随着物联网技术的飞速发展&#xff0c;工业物联网智能网关作为连接物理世界与数字世界的桥梁&#xff0c;正逐渐成为工业4.0时代不可或缺的核心组件。本文将结合实际使用案例&#xff0c;探讨工业物联网智能网关的重要性、功能特点以及在实际应用中的效果与价值。 一、工业物联…

基于jeecgboot-vue3的Flowable流程支持bpmn流程设计器与仿钉钉流程设计器-编辑多版本处理

因为这个项目license问题无法开源&#xff0c;更多技术支持与服务请加入我的知识星球。 1、前端编辑带有仿钉钉流程的处理 /** 编辑流程设计弹窗页面 */const handleLoadXml (row) > {console.log("handleLoadXml row",row)const params {flowKey: row.key,ver…