首页 >> 大全

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

2023-12-12 大全 25 作者:考证青年

自然语言处理NLP星空智能对话机器人系列:深入理解自然语言处理 位置编码()

目录

NLTK自然语言工具包

NLTK是构建程序以处理人类语言数据的领先平台。它为50多个语料库和词汇资源(如)提供了易于使用的界面,以及一套用于分类、标记、词干、标记、解析和语义推理的文本处理库、工业级NLP库包装器和一个活跃的讨论论坛。NLTK适合语言学家、工程师、学生、教育工作者、研究人员和行业用户。NLTK可用于、Mac OS X和Linux。最重要的是,NLTK是一个免费、开源、社区驱动的项目。NLTK被称为“使用进行计算语言学教学和工作的最佳工具”,以及“使用自然语言的最佳库”

的自然语言处理提供了语言处理编程的实用介绍。由NLTK的创作者编写,它指导读者完成编写程序、使用语料库、对文本进行分类、分析语言结构等基础知识。该书的在线版本已经针对 3和NLTK 3进行了更新。(的原始版本在)

with

— Text with the

Bird, Ewan Klein, and Loper

NLTK数据集

本文案例需使用数据集,数据集可以通过以下几种方式下载:

方法一:从nltk官网直接下载:登录nltk官网,单击到数据下载链接

登录到nltk数据下载链接,单击链接下载数据 。

方法二:通过nltk下载工具进行下载

在的交互提示符中输入命令 nltk 及 nltk.(),在系统中会弹出 NLTK 工具,设置 Index的链接地址,选择数据集进行下载。

(base) C:\Users\admin>python
Python 3.8.8 (default, Apr 13 2021, 15:08:03) [MSC v.1916 64 bit (AMD64)] :: Anaconda, Inc. on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import nltk
>>> nltk.download()

在 中下载nltk数据集 。

!pip install gensim==3.8.3
import torch
import nltk
nltk.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: <urlopen error [Errno 11004]
[nltk_data]     getaddrinfo failed>

方法三:已从网上收集.zip及.zip,放到网盘里面,读者可以从网盘下载nltk数据集。

从AI 环境收集nltk的数据集

aistudio@jupyter-112853-2339160:~$ 
aistudio@jupyter-112853-2339160:~$ python
Python 3.7.4 (default, Aug 13 2019, 20:35:49) 
[GCC 7.3.0] :: Anaconda, Inc. on linux
Type "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 nltk
nltk.word_tokenize("A pivot is the pin or the central point on which something balances or turns")

提示如下:

---------------------------------------------------------------------------
LookupError                               Traceback (most recent call last)
<ipython-input-2-1fa24f4409cc> in <module>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 874     if protocol is None or protocol.lower() == "nltk":
--> 875         return find(path_, path + [""]).open()876     elif 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)581     sep = "*" * 70582     resource_not_found = "\n%s\n%s\n%s\n" % (sep, msg, sep)
--> 583     raise 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 math
t=[]
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 math
t=[]
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()

深入理解计算机系统pdf__星空语言艺术

自己测试一下各种组合方法,按奇数 偶数 分别使用正弦、余弦计算

import math
t=[]
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 math
t=[]
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-01
9.87046242e-01 -1.60435960e-01 9.99164224e-01 -4.08766568e-02
9.97479975e-01 7.09482506e-02 9.84703004e-01 1.74241230e-01
9.63226616e-01 2.68690288e-01 9.35118318e-01 3.54335666e-01
9.02130723e-01 4.31462824e-01 8.65725577e-01 5.00518918e-01
8.27103794e-01 5.62049210e-01 7.87237823e-01 6.16649508e-01
7.46903539e-01 6.64932430e-01 7.06710517e-01 7.07502782e-015.47683925e-08 1.00000000e+00 5.09659337e-08 1.00000000e+00
4.74274735e-08 1.00000000e+00 4.41346799e-08 1.00000000e+00
4.10704999e-08 1.00000000e+00 3.82190599e-08 1.00000000e+00
3.55655878e-08 1.00000000e+00 3.30963417e-08 1.00000000e+00
3.07985317e-08 1.00000000e+00 2.86602511e-08 1.00000000e+00
2.66704294e-08 1.00000000e+00 2.48187551e-08 1.00000000e+00
2.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-01
6.92634165e-01 -7.21289039e-01 9.79174793e-01 -2.03019097e-01
9.37632740e-01 3.47627431e-01 6.40478015e-01 7.67976522e-01
2.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-012.73841977e-07 1.00000000e+00 2.54829672e-07 1.00000000e+00
2.37137371e-07 1.00000000e+00 2.20673414e-07 1.00000000e+00
2.05352507e-07 1.00000000e+00 1.91095296e-07 1.00000000e+00
1.77827943e-07 1.00000000e+00 1.65481708e-07 1.00000000e+00
1.53992659e-07 1.00000000e+00 1.43301250e-07 1.00000000e+00
1.33352145e-07 1.00000000e+00 1.24093773e-07 1.00000000e+00
1.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

的作者找到了一种简单的方法,只需添加位置编码向量到单词嵌入向量:

星空智能对话机器人系列博客

关于我们

最火推荐

小编推荐

联系我们


版权声明:本站内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 88@qq.com 举报,一经查实,本站将立刻删除。备案号:桂ICP备2021009421号
Powered By Z-BlogPHP.
复制成功
微信号:
我知道了