自然语言处理NLP星空智能对话机器人系列:深入理解Transformer自然语言

自然语言处理NLP星空智能对话机器人系列:深入理解自然语言处理 位置编码()
目录
NLTK自然语言工具包
NLTK是构建程序以处理人类语言数据的领先平台 。它为50多个语料库和词汇资源(如)提供了易于使用的界面,以及一套用于分类、标记、词干、标记、解析和语义推理的文本处理库、工业级NLP库包装器和一个活跃的讨论论坛 。NLTK适合语言学家、工程师、学生、教育工作者、研究人员和行业用户 。NLTK可用于、Mac OS X和Linux 。最重要的是,NLTK是一个免费、开源、社区驱动的项目 。NLTK被称为“使用进行计算语言学教学和工作的最佳工具”,以及“使用自然语言的最佳库”
的自然语言处理提供了语言处理编程的实用介绍 。由NLTK的创作者编写,它指导读者完成编写程序、使用语料库、对文本进行分类、分析语言结构等基础知识 。该书的在线版本已经针对 3和NLTK 3进行了更新 。(的原始版本在)
with
—Text with the
Bird, Ewan Klein, andLoper
NLTK数据集
本文案例需使用数据集,数据集可以通过以下几种方式下载:
方法一:从nltk官网直接下载:登录nltk官网,单击到数据下载链接
登录到nltk数据下载链接,单击链接下载数据。
方法二:通过nltk下载工具进行下载
在的交互提示符中输入命令 nltk 及 nltk.(),在系统中会弹出 NLTK 工具,设置 Index的链接地址,选择数据集进行下载 。
(base) C:\Users\admin>pythonPython 3.8.8 (default, Apr 13 2021, 15:08:03) [MSC v.1916 64 bit (AMD64)] :: Anaconda, Inc. on win32Type "help", "copyright", "credits" or "license" for more information.>>> import nltk>>> nltk.download()
在 中下载nltk数据集。
【自然语言处理NLP星空智能对话机器人系列:深入理解Transformer自然语言】!pip install gensim==3.8.3import torchimport nltknltk.download('punkt')
Requirement already satisfied: gensim==3.8.3 in e:\anaconda3\envs\my_star_space\lib\site-packages (3.8.3)Requirement already satisfied: six>=1.5.0 in e:\anaconda3\envs\my_star_space\lib\site-packages (from gensim==3.8.3) (1.15.0)Requirement already satisfied: smart-open>=1.8.1 in e:\anaconda3\envs\my_star_space\lib\site-packages (from gensim==3.8.3) (5.2.1)Requirement already satisfied: scipy>=0.18.1 in e:\anaconda3\envs\my_star_space\lib\site-packages (from gensim==3.8.3) (1.5.2)Requirement already satisfied: Cython==0.29.14 in e:\anaconda3\envs\my_star_space\lib\site-packages (from gensim==3.8.3) (0.29.14)Requirement already satisfied: numpy>=1.11.3 in e:\anaconda3\envs\my_star_space\lib\site-packages (from gensim==3.8.3) (1.19.5)[nltk_data] Error loading punkt:
方法三:已从网上收集.zip及.zip,放到网盘里面,读者可以从网盘下载nltk数据集 。
从AI 环境收集nltk的数据集
aistudio@jupyter-112853-2339160:~$ aistudio@jupyter-112853-2339160:~$ pythonPython 3.7.4 (default, Aug 13 2019, 20:35:49) [GCC 7.3.0] :: Anaconda, Inc. on linuxType "help", "copyright", "credits" or "license" for more information.>>> import nltk >>> nltk.download('sentiwordnet')[nltk_data] Downloading package sentiwordnet to[nltk_data]/home/aistudio/nltk_data...[nltk_data]Unzipping corpora/sentiwordnet.zip.True
网盘数据集 下载
链接:
提取码:nfls
将数据集下载到本地电脑,需确定数据集存放的文件目录,可以通过以下代码,从报错提示中查询加载数据的目录信息,将下载的nltk数据集放到相应的目录就可以 。
import nltknltk.word_tokenize("A pivot is the pin or the central point on which something balances or turns")
提示如下:
---------------------------------------------------------------------------LookupErrorTraceback (most recent call last) in 1 import nltk----> 2 nltk.word_tokenize("A pivot is the pin or the central point on which something balances or turns")e:\anaconda3\envs\my_star_space\lib\site-packages\nltk\tokenize\__init__.py in word_tokenize(text, language, preserve_line)128:type preserve_line: bool129""".......e:\anaconda3\envs\my_star_space\lib\site-packages\nltk\data.py in _open(resource_url)873 874if protocol is None or protocol.lower() == "nltk":--> 875return find(path_, path + [""]).open()876elif protocol.lower() == "file":877# urllib might not use mode='rb', so handle this one ourselves:e:\anaconda3\envs\my_star_space\lib\site-packages\nltk\data.py in find(resource_name, paths)581sep = "*" * 70582resource_not_found = "\n%s\n%s\n%s\n" % (sep, msg, sep)--> 583raise LookupError(resource_not_found)584 585 LookupError: **********************************************************************Resource punkt not found.Please use the NLTK Downloader to obtain the resource:>>> import nltk>>> nltk.download('punkt')For more information see: https://www.nltk.org/data.htmlAttempted to load tokenizers/punkt/english.pickleSearched in:- 'C:\\Users\\admin/nltk_data'- 'e:\\anaconda3\\envs\\my_star_space\\nltk_data'- 'e:\\anaconda3\\envs\\my_star_space\\share\\nltk_data'- 'e:\\anaconda3\\envs\\my_star_space\\lib\\nltk_data'- 'C:\\Users\\admin\\AppData\\Roaming\\nltk_data'- 'C:\\nltk_data'- 'D:\\nltk_data'- 'E:\\nltk_data'- ''**********************************************************************
将解压文件放到相应的目录E:\\envs\\
位置编码( )
我们输入的位置编码函数时不知道单词在序列中的位置:
The black cat sat on the couch and the brown dog slept on the rug.
def positional_encoding(pos,pe):for i in range(0, 512,2):pe[0][i] = math.sin(pos / (10000 ** ((2 * i)/d_model)))pe[0][i+1] = math.cos(pos / (10000 ** ((2 * i)/d_model)))return pe
在中使用正弦函数,测试一下官网的实例
import matht=[]m=[]for x in range(-100, 100,1):#print(x)t.append(x)y=math.sin(2/10000**(2*x/512)) m.append(y) plt.plot(t,m,'-r')plt.show()
pos=2运行结果如下:
pos=10的运行结果
import matht=[]m=[]for x in range(-100, 100,1):#print(x)t.append(x)y=math.sin(10/10000**(2*x/512)) m.append(y) plt.plot(t,m,'-r')plt.show()

自然语言处理NLP星空智能对话机器人系列:深入理解Transformer自然语言

文章插图
自己测试一下各种组合方法,按奇数 偶数 分别使用正弦、余弦计算
import matht=[]m=[]for x in range(0, 100,2):#print(x)t.append(x)t.append(x+1) y= math.sin(2 / (10000 ** ((2 * x)/512)))y_1 = math.cos(2 / (10000 ** ((2 * x)/512))) m.append(y)m.append(y_1)plt.plot(t,m,'-r')plt.show()
打印结果如下:
测试一下只使用正弦函数的情况
import matht=[]m=[]for x in range(0, 100,2):#print(x)t.append(x)t.append(x+1) y= math.sin(2 / (10000 ** ((2 * x)/512))) #y_1 = math.cos(2 / (10000 ** ((2 * a/512))) m.append(y)m.append(y_1)plt.plot(t,m,'-r')plt.show()
运行结果如下
回到本文中分析的句子,我们可以看到黑色(black)是在位置2处,棕色(brown)在位置10处:
The black cat sat on the couch and the brown dog slept on the rug.
如果我们将正弦和余弦函数应用于pos=2,则得到的大小为512位置编码向量:
PE(2)=[[ 9.09297407e-01 -4.16146845e-01 9.58144367e-01 -2.86285430e-019.87046242e-01 -1.60435960e-01 9.99164224e-01 -4.08766568e-029.97479975e-01 7.09482506e-02 9.84703004e-01 1.74241230e-019.63226616e-01 2.68690288e-01 9.35118318e-01 3.54335666e-019.02130723e-01 4.31462824e-01 8.65725577e-01 5.00518918e-018.27103794e-01 5.62049210e-01 7.87237823e-01 6.16649508e-017.46903539e-01 6.64932430e-01 7.06710517e-01 7.07502782e-01…5.47683925e-08 1.00000000e+00 5.09659337e-08 1.00000000e+004.74274735e-08 1.00000000e+00 4.41346799e-08 1.00000000e+004.10704999e-08 1.00000000e+00 3.82190599e-08 1.00000000e+003.55655878e-08 1.00000000e+00 3.30963417e-08 1.00000000e+003.07985317e-08 1.00000000e+00 2.86602511e-08 1.00000000e+002.66704294e-08 1.00000000e+00 2.48187551e-08 1.00000000e+002.30956392e-08 1.00000000e+00 2.14921574e-08 1.00000000e+00]]
也可获得位置10的位置编码向量 size=512,位置=10:
PE(10)=[[-5.44021130e-01 -8.39071512e-01 1.18776485e-01 -9.92920995e-016.92634165e-01 -7.21289039e-01 9.79174793e-01 -2.03019097e-019.37632740e-01 3.47627431e-01 6.40478015e-01 7.67976522e-012.09077001e-01 9.77899194e-01 -2.37917677e-01 9.71285343e-01-6.12936735e-01 7.90131986e-01 -8.67519796e-01 4.97402608e-01-9.87655997e-01 1.56638563e-01 -9.83699203e-01 -1.79821849e-01…2.73841977e-07 1.00000000e+00 2.54829672e-07 1.00000000e+002.37137371e-07 1.00000000e+00 2.20673414e-07 1.00000000e+002.05352507e-07 1.00000000e+00 1.91095296e-07 1.00000000e+001.77827943e-07 1.00000000e+00 1.65481708e-07 1.00000000e+001.53992659e-07 1.00000000e+00 1.43301250e-07 1.00000000e+001.33352145e-07 1.00000000e+00 1.24093773e-07 1.00000000e+001.15478201e-07 1.00000000e+00 1.07460785e-07 1.00000000e+00]]
用于单词嵌入的余弦相似函数非常方便,更好地显示位置的接近程度
cosine_similarity(pos(2), pos(10)= [[0.8600013]]
black和brown两个词的位置相似性与词汇的相似性(组合在一起的词组) 不同:
cosine_similarity(black, brown)= [[0.9998901]]
位置的编码显示出比单词更低的相似度值,位置编码将这些单词分开,单词嵌入的词向量会因用于训练它们的语料库而不同 。现在的问题是如何将位置编码添加到单词嵌入向量中 。
to the
的作者找到了一种简单的方法,只需添加位置编码向量到单词嵌入向量:
星空智能对话机器人系列博客