NLP深度学习速成课程。
在7天内为您的文本数据项目带来深度学习方法。
我们被文本淹没,来自书籍、论文、博客、推文、新闻,以及越来越多来自口语的文本。
处理文本很困难,因为它需要借鉴语言学、机器学习、统计方法以及如今的深度学习等不同领域的知识。
在一些具有单一、更简单模型的挑战性自然语言处理问题上,深度学习方法开始超越传统的和统计学的方法。
在本速成课程中,您将发现如何在7天内开始并自信地使用Python开发用于自然语言处理问题的深度学习。
这是一篇内容丰富且重要的文章。您可能想把它加入书签。
通过我的新书《NLP深度学习》,开始您的项目,书中包含分步教程和所有示例的Python源代码文件。
让我们开始吧。
- 2020年1月更新:更新了Keras 2.3和TensorFlow 2.0的API。
- 2020 年 8 月更新:更新了电影评论数据集的链接。

如何开始使用深度学习处理自然语言
照片作者:Daniel R. Blume,保留部分权利。
本速成课程适合谁?
在开始之前,让我们确保您来对了地方。
以下列表提供了一些关于本课程设计对象的一般性指导。
如果您不完全符合这些要点,请不要惊慌,您可能只需要在某个领域稍作补充即可跟上。
您需要了解
- 您需要熟悉基本的Python、NumPy和Keras进行深度学习。
您不需要知道:
- 您不需要是数学天才!
- 您无需成为深度学习专家!
- 您不需要成为语言学家!
本速成课程将帮助您从一名了解一点机器学习的开发人员,成长为能够将深度学习方法应用于自己自然语言处理项目的开发人员。
注意:本速成课程假定您有一个可用的Python 2或3 SciPy环境,并安装了至少NumPy、Pandas、scikit-learn和Keras 2。如果您在环境设置方面需要帮助,可以参考此处的分步教程。
速成课程概览
本速成课程分为7节课。
您可以每天完成一节课(推荐),或者在一天内完成所有课程(硬核)。这真的取决于您的可用时间和热情程度。
以下是7个课程,将帮助您开始并熟练掌握Python的自然语言处理深度学习。
- 第一课:深度学习与自然语言
- 第二课:清理文本数据
- 第三课:词袋模型
- 第四课:词嵌入表示
- 第五课:学习到的词嵌入
- 第六课:文本分类
- 第七课:电影评论情感分析项目
每节课可能需要60秒到30分钟不等。请慢慢来,按照自己的节奏完成课程。在下面的评论中提问,甚至发布结果。
这些课程期望您能够自行查找如何完成各项任务。我会给您提示,但每门课程的一部分目的就是强迫您学习到何处去查找关于深度学习、自然语言处理以及Python中最优秀工具的帮助(提示:我所有的答案都在这个博客上,请使用搜索框)。
我确实以相关文章链接的形式提供了更多帮助,因为我希望您建立一些信心和动力。
在评论中发布您的结果,我会为您加油!
坚持住,不要放弃。
注意:这只是一个速成课程。如需更多详细信息和30个完善的教程,请参阅我关于该主题的书籍《NLP深度学习》。
需要深度学习处理文本数据的帮助吗?
立即参加我的免费7天电子邮件速成课程(附代码)。
点击注册,同时获得该课程的免费PDF电子书版本。
第一课:深度学习与自然语言
在本课中,您将了解自然语言、深度学习的简洁定义以及深度学习在处理文本数据方面的潜力。
自然语言处理
自然语言处理,简称NLP,通常被定义为软件对自然语言(如语音和文本)的自动处理。
自然语言处理的研究已经有50多年的历史了,它起源于语言学领域,随着计算机的兴起而发展。
理解文本的问题尚未解决,而且可能永远不会解决,这主要是因为语言是混乱的。规则很少。然而,我们大多数时候都能轻松地理解彼此。
深度学习
深度学习是机器学习的一个子领域,涉及受大脑结构和功能启发的算法,称为人工神经网络。
深度学习的一个特性是,这类模型的性能可以通过用更多示例训练它们,增加它们的深度或表示能力来提高。
除了可扩展性,深度学习模型的另一个经常被提及的好处是它们能够从原始数据中进行自动特征提取,也称为特征学习。
深度学习在NLP领域的潜力
深度学习方法在自然语言领域很受欢迎,主要是因为它们正在兑现其承诺。
深度学习能力的一些首次大型演示是在自然语言处理领域,特别是语音识别。最近则是在机器翻译领域。
深度学习在自然语言处理领域的3个关键承诺如下:
- 特征学习的承诺。也就是说,深度学习方法可以从自然语言中学习模型所需的特征,而无需专家指定和提取特征。
- 持续改进的承诺。也就是说,深度学习在自然语言处理中的性能是基于真实结果的,并且改进似乎正在持续甚至加速。
- 端到端模型的承诺。也就是说,大型端到端深度学习模型可以适用于自然语言问题,提供更通用和性能更好的方法。
自然语言处理并未被“解决”,但要在该领域许多具有挑战性的问题上达到最先进的水平,深度学习是必需的。
您的任务
在本课中,您必须研究并列出深度学习方法在自然语言处理领域10个令人印象深刻的应用。如果您能链接到演示示例的研究论文,将获得加分。
请在下面的评论中发布您的答案。我很想看看您会发现什么。
更多信息
在下一课中,您将学习如何清理文本数据,使其为建模做好准备。
第二课:清理文本数据
在本课中,您将学习如何加载和清理文本数据,使其为建模做好准备,使用手动方法和NLTK Python库。
文本是混乱的
您不能直接从原始文本进入机器学习或深度学习模型的拟合。
您必须先清理您的文本,这意味着将其分割成单词,并规范化诸如以下方面的问题:
- 大写和小写字母。
- 单词内外的标点符号。
- 数字,如金额和日期。
- 拼写错误和地区性差异。
- Unicode字符
- 以及更多…
手动分词
通常,我们将将原始文本转换为可建模内容的这个过程称为“分词”,这时我们就得到一个单词或“标记”列表。
我们可以手动开发Python代码来清理文本,这通常是一个好方法,因为每个文本数据集都需要以独特的方式进行分词。
例如,下面的代码片段将加载一个文本文件,按空格分割单词,并将每个单词转换为小写。
1 2 3 4 5 6 7 8 |
filename = '...' file = open(filename, 'rt') text = file.read() 文件.close() # 按空格分割成单词 words = text.split() # 转换为小写 words = [word.lower() for word in words] |
您可以想象如何扩展此片段来处理和规范化Unicode字符,删除标点符号等。
NLTK分词
许多分词原始文本的最佳实践已被捕获并打包到一个名为自然语言工具包(Natural Language Toolkit)的Python库中,简称NLTK。
您可以通过在命令行中输入以下命令来安装此库:
1 |
sudo pip install -U nltk |
安装后,您还必须安装该库使用的数据集,可以通过Python脚本
1 2 |
import nltk nltk.download() |
或通过命令行
1 |
python -m nltk.downloader all |
安装完成后,您可以使用API进行文本分词。例如,以下代码片段将加载和分词一个ASCII文本文件。
1 2 3 4 5 6 7 8 |
# 加载数据 filename = '...' file = open(filename, 'rt') text = file.read() 文件.close() # 分割成单词 从 nltk.分词 导入 word_tokenize tokens = word_tokenize(text) |
此库中有许多工具,您还可以使用自己的手动方法进一步细化清理后的标记,例如删除标点符号、删除停用词、词干提取等等。
您的任务
您的任务是找到Project Gutenberg网站上一本免费的经典书籍,下载该书的ASCII版本,对其文本进行分词,并将结果保存到新文件中。探索手动和NLTK方法将获得加分。
请在下面的评论中发布您的代码。我很想看看您选择了哪本书以及您是如何对其进行分词的。
更多信息
在下一课中,您将了解词袋模型。
第三课:词袋模型
在本课中,您将了解词袋模型以及如何使用该模型对文本进行编码,以便您可以使用scikit-learn和Keras Python库来训练模型。
词袋
词袋模型是一种在用机器学习算法建模文本时表示文本数据的方式。
该方法非常简单且灵活,并且可以以多种方式用于从文档中提取特征。
词袋是一种文本表示,它描述了单词在文档中出现的次数。
选择一个词汇表,其中可能丢弃一些不常用的词。然后,使用一个向量来表示给定的文本文档,该向量在词汇表的每个单词处都有一个位置,并为文档中出现的(或未出现的)已知单词提供一个分数。
它被称为“词袋”,因为文档中单词的顺序或结构信息都被丢弃了。该模型只关注已知单词是否出现在文档中,而不关心它们出现在文档的哪个位置。
使用scikit-learn的词袋
scikit-learn机器学习Python库提供了用于对文档进行词袋模型编码的工具。
可以创建一个编码器的实例,用文本文档语料库进行训练,然后反复用于编码训练、测试、验证和任何需要为模型编码的新数据。
有一个编码器可以根据词频对单词进行评分,称为CountVectorizer;一个使用每个单词的哈希函数来缩短向量长度的编码器,称为HashingVectorizer;还有一个使用词语在文档中出现频率以及在所有文档中反向出现频率来评分的编码器,称为TfidfVectorizer。
以下代码片段展示了如何训练TfidfVectorizer词袋编码器并使用它来编码多个小型文本文档。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
from sklearn.feature_extraction.text import TfidfVectorizer # 文本文档列表 text = ["The quick brown fox jumped over the lazy dog.", "The dog.", "The fox"] # 创建转换器 vectorizer = TfidfVectorizer() # 分词并构建词汇表 vectorizer.fit(text) # 总结 print(vectorizer.vocabulary_) print(vectorizer.idf_) # 编码文档 vector = vectorizer.transform([text[0]]) # 总结编码向量 print(vector.shape) print(vector.toarray()) |
使用Keras的词袋
Keras深度学习Python库还在Tokenizer类中提供了使用词袋模型对文本进行编码的工具。
与上面一样,编码器必须用源文档进行训练,然后才能用于对训练数据、测试数据以及未来任何其他数据进行编码。该API还有一个好处,即在编码单词之前执行基本的文本分词。
以下代码片段演示了如何使用Keras API和单词的“count”类型评分来训练和编码一些小型文本文档。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
from keras.preprocessing.text import Tokenizer # 定义5个文档 文档 = ['做得好!', '好工作', '巨大的努力', '不错的工作', 'Excellent!'] # 创建分词器 t = 分词器() # 在文档上拟合分词器 t.fit_on_texts(文档) # 总结所学内容 print(t.word_counts) print(t.document_count) print(t.word_index) print(t.word_docs) # 整数编码文档 encoded_docs = t.texts_to_matrix(docs, mode='count') 打印(encoded_docs) |
您的任务
本课的任务是试验scikit-learn和Keras方法,为词袋模型编码小型构造的文本文档。如果您使用标准文本数据集来练习并进行数据清理作为准备工作,将获得加分。
请在下面的评论中发布您的代码。我很想看看您探索和演示了哪些API。
更多信息
在下一课中,您将了解词嵌入。
第四课:词嵌入表示
在本课中,您将了解词嵌入分布式表示,以及如何使用Gensim Python库开发词嵌入。
词嵌入
词嵌入是一种词语表示,它允许意义相似的词语具有相似的表示。
它们是文本的一种分布式表示,可能是深度学习方法在处理具有挑战性的自然语言处理问题时取得优异性能的关键突破之一。
词嵌入方法从文本语料库中为预定义的固定大小的词汇表学习一个实值向量表示。
训练词嵌入
您可以使用Gensim Python库来训练词嵌入分布式表示,用于主题建模。
Gensim提供了word2vec算法的实现,该算法由Google开发,用于从文本文档快速训练词嵌入表示,
您可以通过在命令行中输入以下命令来安装Gensim:
1 |
pip install -U gensim |
以下代码片段展示了如何定义几个构造的句子并在Gensim中训练词嵌入表示。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
from gensim.models import Word2Vec # 定义训练数据 sentences = [['this', 'is', 'the', 'first', 'sentence', 'for', 'word2vec'], ['this', 'is', 'the', 'second', 'sentence'], ['yet', 'another', 'sentence'], ['one', 'more', 'sentence'], ['and', 'the', 'final', 'sentence']] # 训练模型 model = Word2Vec(sentences, min_count=1) # 总结加载的模型 print(model) # 总结词汇表 words = list(model.wv.vocab) print(words) # 访问一个单词的向量 print(model['sentence']) |
使用词嵌入
训练完成后,可以将嵌入保存到文件中,作为另一个模型(例如深度学习模型的前端)的一部分使用。
您还可以绘制词语分布式表示的投影,以了解模型认为词语是如何关联的。您可以使用的一种常见的投影技术是主成分分析(PCA),它在scikit-learn中可用。
以下代码片段展示了如何训练词嵌入模型,然后绘制词汇表中所有单词的二维投影。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
from gensim.models import Word2Vec 从 sklearn.分解 导入 PCA from matplotlib import pyplot # 定义训练数据 sentences = [['this', 'is', 'the', 'first', 'sentence', 'for', 'word2vec'], ['this', 'is', 'the', 'second', 'sentence'], ['yet', 'another', 'sentence'], ['one', 'more', 'sentence'], ['and', 'the', 'final', 'sentence']] # 训练模型 model = Word2Vec(sentences, min_count=1) # 拟合一个2D PCA模型到向量 X = model[model.wv.vocab] pca = PCA(n_components=2) result = pca.fit_transform(X) # 创建投影的散点图 pyplot.scatter(result[:, 0], result[:, 1]) words = list(model.wv.vocab) for i, word in enumerate(words): pyplot.annotate(word, xy=(result[i, 0], result[i, 1])) pyplot.show() |
您的任务
本课的任务是使用Gensim在文本文档(例如Project Gutenberg的一本书)上训练词嵌入。如果您能生成常见词的绘图,将获得加分。
请在下面的评论中发布您的代码。我很想看看您选择了哪本书以及您学到的嵌入的任何细节。
更多信息
在下一课中,您将了解如何开发用于文本分类的深度学习模型。
第五课:学习到的词嵌入
在本课中,您将学习如何学习单词的词嵌入分布式表示,作为拟合深度学习模型的一部分。
嵌入层
Keras提供了一个Embedding层,可用于神经网络处理文本数据。
它要求输入数据被整数编码,以便每个单词由一个唯一的整数表示。这个数据准备步骤可以使用Keras提供的Tokenizer API来完成。
Embedding层用随机权重初始化,并将学习训练数据集中所有单词的嵌入。您必须指定input_dim(词汇表的大小)、output_dim(嵌入向量空间的大小)以及可选的input_length(输入序列中的单词数)。
1 |
layer = Embedding(input_dim, output_dim, input_length=??) |
或者,更具体地说,一个包含200个单词的词汇表,一个32维的分布式表示,以及一个50个单词的输入长度。
1 |
layer = Embedding(200, 32, input_length=50) |
模型中的嵌入
Embedding层可以用作深度学习模型的前端,为单词提供丰富的分布式表示,并且重要的是,这种表示可以在深度学习模型训练的过程中进行学习。
例如,以下代码片段将定义和编译一个神经网络,其中包含一个嵌入输入层和一个用于文档分类问题的密集输出层。
当模型在填充的文档示例及其关联的输出标签上进行训练时,网络权重和分布式表示都将被调整以适应特定数据。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
from keras.models import Sequential from keras.layers import Dense from keras.layers import Flatten from keras.layers.embeddings import Embedding # 定义问题 vocab_size = 100 max_length = 32 # 定义模型 model = Sequential() model.add(Embedding(vocab_size, 8, input_length=max_length)) model.add(Flatten()) model.add(Dense(1, activation='sigmoid')) # 编译模型 model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy']) # 总结模型 print(model.summary()) |
您也可以使用预训练的权重(例如Gensim准备的权重)来初始化Embedding层,并配置该层为不可训练。如果有一个非常大的文本语料库用于预训练词嵌入,这种方法可能会很有用。
您的任务
本课的任务是设计一个小型文档分类问题,包含10篇每篇只有一个句子的文档以及相应的正面和负面结果标签,并在这些数据上训练一个带有词嵌入的网络。请注意,每句话在模型使用Keras的pad_sequences()函数进行训练之前都需要填充到相同的最大长度。如果您加载一个使用Gensim预训练的词嵌入,将获得加分。
请在下面的评论中发布您的代码。我很想看看您构造了哪些句子以及您的模型的技能。
更多信息
在下一课中,您将了解如何开发用于文本分类的深度学习模型。
第六课:文本分类
在本课中,您将了解用于文本分类的标准深度学习模型,该模型可用于情感分析等问题。
文档分类
文本分类描述了一类通用问题,例如预测推文和电影评论的情感,以及将电子邮件分类为垃圾邮件或非垃圾邮件。
它是自然语言处理的一个重要领域,也是使用深度学习技术处理文本数据的绝佳起点。
深度学习方法在文本分类方面表现出色,在各种标准的学术基准问题上取得了最先进的结果。
嵌入 + CNN
文本分类的模式涉及使用词嵌入来表示单词,并使用卷积神经网络(CNN)来学习如何在分类问题中区分文档。
该架构由三个关键部分组成:
- 词嵌入模型:单词的分布式表示,其中意义相似(基于其用法)的单词也具有相似的表示。
- 卷积模型:一个特征提取模型,它学习从使用词嵌入表示的文档中提取重要的特征。
- 全连接模型:根据预测输出来解释提取的特征。
这种类型的模型可以在Keras Python深度学习库中定义。以下代码片段展示了一个用于将文本文档分类为两个类别之一的深度学习模型示例。
1 2 3 4 5 6 7 8 9 10 11 12 |
# 定义问题 vocab_size = 100 max_length = 200 # 定义模型 model = Sequential() model.add(Embedding(vocab_size, 100, input_length=max_length)) model.add(Conv1D(filters=32, kernel_size=8, activation='relu')) model.add(MaxPooling1D(pool_size=2)) model.add(Flatten()) model.add(Dense(10, activation='relu')) model.add(Dense(1, activation='sigmoid')) print(model.summary()) |
您的任务
您在此课程中的任务是研究深度学习方法中Embedding + CNN组合在文本分类中的应用,并报告模型配置的示例或最佳实践,例如层数、卷积核大小、词汇量大小等。
如果您能找到并描述通过改变卷积核大小来支持n-gram或多组词作为输入的变体,将获得额外加分。
在下面的评论中发布您的发现。我很想知道您的发现。
更多信息
在下一课中,您将学习如何处理情感分析预测问题。
课程 07:电影评论情感分析项目
在本课中,您将学习如何准备文本数据,开发和评估深度学习模型来预测电影评论的情感。
我希望您能将您在本速成课程中学到的一切联系起来,并端到端地解决一个真实世界的问题。
电影评论数据集
电影评论数据集是由Bo Pang和Lillian Lee在2000年代初从imdb.com网站收集的电影评论集合。这些评论被收集并作为他们自然语言处理研究的一部分提供。
您可以从此处下载数据集
- 电影评论极性数据集 (review_polarity.tar.gz, 3MB)
您将使用此数据集开发一个情感分析深度学习模型,以预测给定的电影评论是积极还是消极。
您的任务
您在此课程中的任务是基于电影评论数据集开发和评估深度学习模型。
- 下载并检查数据集。
- 清理和分词文本,并将结果保存到新文件中。
- 将清理后的数据分割为训练集和测试集。
- 在训练集上开发Embedding + CNN模型。
- 在测试集上评估模型。
如果您能通过对新的、人为的或真实的电影评论进行预测来演示您的模型,将获得额外加分。如果您能将您的模型与神经词袋模型进行比较,将获得额外的加分。
请在下面的评论中发布您的代码和模型技能。我很想看看您能想出什么。更简单的模型更受欢迎,但也请尝试深入研究,看看会发生什么。
更多信息
结束!
(看看您已经走了多远)
您做到了。干得好!
花点时间回顾一下您已经走了多远。
您发现了:
- 什么是自然语言处理,以及深度学习在该领域所带来的前景和影响。
- 如何手动清理和分词原始文本数据,并使用NLTK使其准备好进行建模。
- 如何使用scikit-learn和Keras库中的词袋模型对文本进行编码。
- 如何使用Gensim库训练词嵌入的词语分布式表示。
- 如何学习词嵌入的词语分布式表示,作为拟合深度学习模型的一部分。
- 如何将词嵌入与卷积神经网络用于文本分类问题。
- 如何使用深度学习方法端到端地解决真实世界的情感分析问题。
这仅仅是您深度学习自然语言处理之旅的开始。请继续练习和发展您的技能。
采取下一步行动,查看我的NLP深度学习书籍。
总结
您对这个迷你课程感觉如何?
您喜欢这个速成课程吗?
您有任何问题吗?有没有遇到什么难点?
告诉我。在下面留言。
嗨,感谢有用的博客。
这里有10个使用深度学习方法的令人印象深刻的NLP应用
1. 机器翻译
使用RNN编码器-解码器学习短语表示用于统计机器翻译
https://arxiv.org/abs/1406.1078
2. 情感分析
使用分层深度学习进行方面特定情感分析
https://fb56552f-a-62cb3a1a-s-sites.googlegroups.com/site/deeplearningworkshopnips2014/58.pdf
3. 文本分类
用于文本分类的卷积神经网络。
http://www.aaai.org/ocs/index.php/AAAI/AAAI15/paper/download/9745/9552
4. 命名实体识别
命名实体识别的神经网络架构
https://arxiv.org/abs/1603.01360
5. 阅读理解
Squad:100,000+ 个用于机器文本理解的问题
https://arxiv.org/abs/1606.05250
6. 词语分割
深度学习用于中文词语分割和词性标注。
http://www.aclweb.org/old_anthology/D/D13/D13-1061.pdf
7. 词性标注
通过双向lstm-cnns-crf进行端到端序列标注
https://arxiv.org/abs/1603.01354
8. 意图检测
使用卷积神经网络进行查询意图检测
http://people.cs.pitt.edu/~hashemi/papers/QRUMS2016_HBHashemi.pdf
9. 垃圾邮件检测
基于深度学习的推特垃圾邮件检测
http://dl.acm.org/citation.cfm?id=3014815
10. 摘要生成
具有注意力机制的循环神经网络的抽象摘要生成
http://www.aclweb.org/anthology/N16-1012
非常好!
1. LyreBird https://www.descript.com/lyrebird-ai >> 这是用于语音生成和合成的自然语言处理。给定一个字符串,它能够正确地合成人类的声音来朗读,包括语调。语调的使用是使计算机生成的语音比普通的平淡的“读写”声音更自然的主要改进。如果用户不知道它是计算机生成的,这可以通过图灵测试。
2. https://talktotransformer.com/ >> 您可以输入任何一段文本,无论是圣经还是星球大战的引言,它都能自动开始写故事,而且既清晰又具有语境准确性。
3. YouTube 自动字幕生成
4. Facebook Messenger / iMessage:当您键入一个预格式化的字符串(如电话号码、电子邮件或日期)时,它会识别出来并自动提供建议,例如将其添加到您的联系人或日历中。
5. Alexa 能够详细描述许多科学概念
6. 亚马逊客服:您几乎不需要与真人交谈即可处理大多数客服请求,因为基于语音的聊天机器人能够处理您的请求。
7. Google 语音搜索
8. Google 翻译
9. https://openai.com/blog/musenet/ >> 能够以任何可能的艺术家的风格生成自己的音乐…… Bon Jovi 的钢琴曲…… 莫扎特的电吉他曲,并融合不同艺术家的风格(仅限音符)。
10. https://openai.com/blog/jukebox/ >> 以各种艺术家的风格创作自己的音乐并撰写自己的歌词。
干得好!
你好,先生,
我是NLP、机器学习和其他AI方面的新手。我知道这将对我有帮助。我总是喜欢发推特分享您的文章。 sir,您做得很好。我将尽我最大的努力向您汇报我的学习情况,因为我还在经营一家公司,所以可能会有点忙 :)。无论如何,非常感谢您的教程。
谢谢,坚持下去!
Jason您好……我是NLP新手。我有一个数据集,包含5000条记录/样本及对应的目标。每10条记录中有一个目标/类别。换句话说,有500个目标/类别。此外,每个目标/类别由多个单词组成(类似于简单的句子)。我非常希望您能就如何使用此类数据构建(NLP)模型提出建议/帮助。谢谢。
也许可以从这里描述的一些简单的文本分类模型开始
https://machinelearning.org.cn/start-here/#nlp
在我休息之前,这里有一篇我发现的关于NLP的有趣文章
也许我学习它的最大原因:训练一个网络来检测假新闻。
https://medium.com/@Genyunus/detecting-fake-news-with-nlp-c893ec31dee8
谢谢 Danh。
你好 Jason,
您很快会出版一本NLP方面的书吗?
我非常期待它。
是的,我希望在2周左右发布它。
你好 Jason,
一如既往,感谢您精彩、鼓舞人心且精炼的教程。我几乎买了我所有的书,并期待深入阅读您关于NLP的新书。
我想把英语翻译成一种罕见的、书面资源稀少的语言,以便母语人士能够从互联网上获取口头或书面内容。
请在您未来的博客中提供建议或进行关于特定方法的教程。
我希望有很多关于NMT的教程。
我建议尽可能多地获取配对的示例,并考虑使用增强方法来重复使用您已有的配对来训练模型。
这是用来分词 Ray Bradbury 的《一段小旅程》的代码。Ray Bradbury 是我最喜欢的经典科幻作家之一。不幸的是,Project Gutenberg 没有 Orwell 的作品,所以我选择了 Bradbury。我使用的是最基础的分词方式,但我会在此基础上添加更多内容。代码库中包含了包含分词文本的文本文件。
https://github.com/NguyenDa18/DeepLearningNLP/blob/master/Lesson2Tokenize.py
很棒 Danh,谢谢分享!
做得好,付诸行动是成功者和失败者之间最大的区别之一!
非常感谢您 Jason。这非常简洁清晰。希望能听到更多。
谢谢 Ravi。
大家好。
这里有30个使用深度学习方法的令人印象深刻的NLP应用。
这是一个带链接的地图。下载这个并在XMind上打开。
http://www.xmind.net/m/AEYf
太棒了,谢谢分享 Gilles!
嘿 Jason。我只想说谢谢您的精彩教程。它提供了完成一个实际、可行的机器学习项目的灵感和帮助。诚然,我的模型还可以改进。但我确实能够按照上述步骤操作,创建和评估一个模型,然后实际使用它对未见过的、未用于训练或测试的数据进行预测。现在,我想进一步提高它的准确性!再次感谢,继续保持出色的工作!
做得好 Kyle!
嗨,Jason,
这是我正在尝试为整个教程实现的的代码。我已经完成了分词任务。我最喜欢的书之一是 Charles Dickens 的《双城记》。
https://github.com/hussainliyaqatdar/pythonprojects/blob/master/NLTK_Tale_of_Two_Cities_Deep_Learning.py
谢谢,
Hussain
干得好!
嗨,Jason,
感谢您提供如此精彩的教程,它非常有帮助。我通过您的教程和您提供的额外信息学到了很多。
在第 2 课中,我能够看到手动方法和 NLTK 方法之间的区别。NLTK 方法产生了大量的词元。在手动方法中,符号被视为前面词的一部分。这是我的分析。
在第 3 课中,我使用了不同版本的 betty botter 绕口令,并成功清理了数据。由于我遵循了您提供的步骤,因此很容易实现。
在第 4 课中,我处理了 Project Gutenberg 的 Ray Bradbury 的《一段小旅程》的词嵌入,并能够绘制常用词。由于它们都有些拥挤,我不得不放大图表才能看到单词。
在第 5 课中,我实现了与您的代码类似的任务,使用了我自己的十个文档并提供了标签。我在这方面没有遇到任何麻烦。
在第 6 课中,我达到了 85% 的准确率!我想我可能在哪里犯了个错误,我正在尝试修复它。
第 7 课:我仍在进行中。我已经完成了清理部分。下一步是实现深度学习模型。
我将在完成所有工作后分享我的学习成果。非常感谢您提供清晰简洁的材料。阅读和实施您的教程确实是一种享受。
此致,
Arjun
干得好!
Jason,感谢您的教程和迷你课程。应您的要求,
– 代码已放置在 https://github.com/dr-riz/nlp_mini_course;
– 我选择了 dickens_94_tale_of_two_cities.txt。
干得好!
深度学习在自然语言处理中的十种应用包括:
1. 预测下一个词
2. 语言翻译
3. 音乐生成 (https://towardsdatascience.com/deep-learning-with-tensorflow-part-3-music-and-text-generation-8a3fbfdc5e9b)
4. 文本分类 (https://machinelearnings.co/text-classification-using-neural-networks-f5cd7b8765c6)
5. 自动邮件回复 (https://towardsdatascience.com/deep-learning-with-tensorflow-part-3-music-and-text-generation-8a3fbfdc5e9b)
6. 文本摘要 (https://arxiv.org/abs/1708.04439)
7. 语音识别
8. 信息检索
9. 文本聚类
10. 文本生成
谢谢分享 Andrew!
你好,
非常感谢您精彩的帖子。
除了词袋模型和词嵌入,还有其他模型吗?我想为特定应用找到一个合适的模型,所以我认为我需要探索可能的模型。
我想提取命名实体(NER)(例如文章中的作者姓名)。这可以作为信息提取的一个子任务。如果您有任何建议/帖子,能否请您指引给我?
我确定有,但这两个是最受欢迎和最成功的。
抱歉,我没有关于 NER 的材料。我希望将来会涵盖它。
我完成了所有内容!我经常开始这些事情却无法完成。感谢这个机会。
谢谢 Josh!
NLP 中的 10 个深度学习应用
1. 文本分类和分类
用于文本分类的卷积神经网络(https://www.aaai.org/ocs/index.php/AAAI/AAAI15/paper/view/9745/9552)
2. 命名实体识别 (NER)
命名实体识别的神经网络架构(http://www.aclweb.org/anthology/N16-1030)
3. 释义检测
检测在线用户论坛中语义等价的问题(https://www.aclweb.org/anthology/K15-1013)
4. 拼写检查
使用神经网络进行个性化拼写检查(http://www.cs.umb.edu/~marc/pubs/garaas_xiao_pomplun_HCII2007.pdf)
5. 序列标注/标记
序列标注是一种模式识别任务,涉及为序列中的每个观测值分配一个分类标签。
带双向语言模型的半监督序列标注(http://ai2-website.s3.amazonaws.com/publications/semi-supervised-sequence.pdf)
6. 语义角色标注
具有自注意力的深度语义角色标注(https://arxiv.org/pdf/1712.01586.pdf)
7. 语义解析和问答
通过阶段性查询图生成进行语义解析,知识库问答(http://www.aclweb.org/anthology/P15-1128)
8. 语言生成和多文档摘要
使用循环神经网络进行自然语言生成、释义和用户评论摘要(http://www.meanotek.ru/files/TarasovDS%282%292015-Dialogue.pdf)
9. 情感分析
使用深度卷积神经网络进行推特情感分析(https://www.researchgate.net/publication/279208470_Twitter_Sentiment_Analysis_Using_Deep_Convolutional_Neural_Network)
10. 自动摘要
使用无监督深度学习进行文本摘要(http://web.science.mq.edu.au/~len/preprint/yousefiazar-eswa-2016-preprint.pdf)
干得漂亮!
嗨,Jason,
很棒的教程,结构清晰。我是一名新手,正在学习您的教程。
我下载了 Project Gutenberg 的文本文件,并完成了您在第 2 课中教授的所有清理工作,现在我有了分词后的文本,去除了停用词和标点符号。
在第 3 课中,我正尝试在上述步骤预处理的数据上实现向量化器。Stemmed 是我的预处理数据。
vectorizer = CountVectorizer()
vectorizer.fit(Stemmed)
print(vectorizer.vocabulary)
当我打印时,输出是 None。
我们可以对词元进行向量化吗?
我尝试了 tfidf 向量化器,并且能够获得词汇量和 idf,但当我打印 vector.to arry 时,数组中全是 0。
文件 = ‘C:/Study/Machine Learning/Dataset/NLP_Data_s.txt’
text = open(file,’rt’)
words = text.read()
text.close()
lower = str.lower(words)# 将所有单词转换为小写
tokens = word_tokenize(lower)# 分词
table = str.maketrans(“”,””,string.punctuation)# 移除标点符号
tokens
remove_punct = [w.translate(table) for w in tokens]# 移除标点符号
tokens
stop_words = set(stopwords.words(‘english’))
remove_stop = [word for word in remove_punct if not word in stop_words]#
移除停用词
porter = PorterStemmer()
Stemmed = [porter.stem(word) for word in remove_stop]# 词干提取
vectorizer = TfidfVectorizer()
vectorizer.fit(Stemmed)
print(vectorizer.get_feature_names())
print(vectorizer.vocabulary_)
print(vectorizer.idf_)
vector= vectorizer.transform(Stemmed)
print(vector.shape)
print(type(vector))
print(vector.toarray())
在将数据输入模型之前,是否可以检查一下数据的内容,以确认一切都按预期工作?
也许这篇文章会有所帮助
https://machinelearning.org.cn/prepare-text-data-machine-learning-scikit-learn/
我现在在数组中得到值了,
之前我使用word_tokenize对整个数据(3个段落)进行了分词。
现在,我使用sent_tokenize进行了分词。
所以,我无法用分词后的单词创建tfid向量吗?
可以,你可能只需要先将标记转换回带空格的字符串。
谢谢 Jason
F10-SGD:用于文本分类和命名实体识别的弹性网络线性模型的快速训练
https://arxiv.org/abs/1902.10649
======================================================================
关于图灵机和其他机器模型接受的语言的密度
https://arxiv.org/abs/1903.03018
======================================================================
在没有语音或有噪声语音的情况下进行语音识别
https://arxiv.org/abs/1903.00739
======================================================================
从字幕生成动作
https://arxiv.org/abs/1902.11109
======================================================================
大规模多语言神经机器翻译
https://arxiv.org/abs/1903.00089
======================================================================
使用联合上下文驱动注意力机制的神经相关工作总结
https://arxiv.org/abs/1901.09492
======================================================================
用于多项选择阅读理解的选项比较网络
https://arxiv.org/abs/1903.03033
干得好!
我不是电脑专家。有没有更简单的方法来设置Python环境?
是的,请尝试这个循序渐进的教程
https://machinelearning.org.cn/setup-python-environment-machine-learning-deep-learning-anaconda/
1. 文本分类和分类
2. 命名实体识别 (NER)
3. 词性标注
4. 语义解析与问答
5. 段落检测
6. 语言生成与多文档摘要
7. 机器翻译
8. 语音识别
9. 字符识别
10. 拼写检查
干得好!
非常感谢您的解释,您能给我解释一下什么是
矩阵嵌入吗?
是的,也许从这里开始
https://machinelearning.org.cn/?s=embedding&post_type=post&submit=Search
1. 文本转语音
2. 语音识别
3. 多标签图像分类 (https://www.analyticsvidhya.com/blog/2019/04/build-first-multi-label-image-classification-model-python/?utm_source=blog&utm_medium=7-innovative-machine-learning-github-projects-in-python)
4. 多任务学习(前一项的泛化)(https://thetalkingmachines.com/sites/default/files/2018-12/unified_nlp.pdf)
5.用于词嵌入的知识驱动深度学习 (https://www.semanticscholar.org/paper/Knowledge-Powered-Deep-Learning-for-Word-Embedding-Bian-Gao/6f60f5ab9774646552f2c8e9239b0f45838bfecb)
6. 语义自然语言处理 (https://arxiv.org/pdf/1803.07640.pdf)
7. 字符识别
8. 拼写检查
9. 情感分析
感谢分享!
谢谢你,Jason。
通过您在每个步骤中的详细阐述,我学到了很多。
谢谢!
1) “计算机辅助翻译中的风格计量学:对巴比伦塔木德的实验” (http://ceur-ws.org/Vol-2006/paper051.pdf) 是关于使用 NLP 来支持专家将一本具有困难解释问题的书籍从古希伯来语翻译成现代语言,该系统学习与风格相关的特征,使其能够消除单词、短语和句子的歧义,并为翻译专家团队提供建议,并从他们的决定中学习。
2) “用于情感极性和讽刺分类的深度神经网络中的多任务学习” (http://ceur-ws.org/Vol-2244/paper_07.pdf) 是关于多任务情感分析,它集成了以下任务:极性检测(积极、消极)和讽刺检测,并认为这种集成方法可以增强情感分析。
3) “SuperAgent:电子商务网站的客户服务聊天机器人” (https://www.aclweb.org/anthology/P17-4017) 是关于使用产品描述以及用户提供的内容和问题的语料库来训练一个用于有效问答任务的聊天机器人,与潜在客户进行交互。
4) “用于聊天机器人的深度学习” (https://scholarworks.sjsu.edu/cgi/viewcontent.cgi?article=1645&context=etd_projects) 是关于深度学习 NLP 以提高聊天机器人与人类对话性能的论文。
5) 用于将美国手语翻译成文本及反之的AI(视觉和NLP)“https://news.developer.nvidia.com/ai-can-interpret-and-translate-american-sign-language-sentences/”
6) “CSI:一种用于虚假新闻检测的混合深度模型” ( https://arxiv.org/pdf/1703.06959.pdf )
7) “深度语义角色标注:有效方法与未来展望” (https://kentonl.com/pub/hllz-acl.2017.pdf) DL-NLP 用于文本挖掘任务
8) DL-NLP 用于智能制造:增强技术文档的抽象和抽取式摘要,通过增强现实设备支持人工维护任务
9) DL-NLP 在医疗领域的应用:增强医疗文档和临床分析结果的抽象和抽取式摘要,以支持人工诊断任务
10) 通过 DL-NLP 增强的文本挖掘进行本体合成
干得好!
对于 Day2 任务(使用卡夫卡的《变形记》文本)
1- 通过空格分割进行清理
[‘One’, ‘morning,’, ‘when’, ‘Gregor’, ‘Samsa’, ‘woke’, ‘from’, ‘troubled’, ‘dreams,’, ‘he’, ‘found’, ‘himself’, ‘transformed’, ‘in’, ‘his’, ‘bed’, ‘into’, ‘a’, ‘horrible’, ‘vermin.’, ‘He’, ‘lay’, ‘on’, ‘his’, ‘armour-like’, ‘back,’, ‘and’, ‘if’, ‘he’, ‘lifted’, ‘his’, ‘head’, ‘a’, ‘little’, ‘he’, ‘could’, ‘see’, ‘his’, ‘brown’, ‘belly,’, ‘slightly’, ‘domed’, ‘and’, ‘divided’, ‘by’, ‘arches’, ‘into’, ‘stiff’, ‘sections.’, ‘The’, ‘bedding’, ‘was’, ‘hardly’, ‘able’, ‘to’, ‘cover’, ‘it’, ‘and’, ‘seemed’, ‘ready’, ‘to’, ‘slide’, ‘off’, ‘any’, ‘moment.’, ‘His’, ‘many’, ‘legs,’, ‘pitifully’, ‘thin’, ‘compared’, ‘with’, ‘the’, ‘size’, ‘of’, ‘the’, ‘rest’, ‘of’, ‘him,’, ‘waved’, ‘about’, ‘helplessly’, ‘as’, ‘he’, ‘looked.’, ‘”What\’s’, ‘happened’, ‘to’, ‘me?”‘, ‘he’, ‘thought.’, ‘It’, “wasn’t”, ‘a’, ‘dream.’, ‘His’, ‘room,’, ‘a’, ‘proper’, ‘human’]
2 – 使用正则表达式进行清理
[‘One’, ‘morning’, ‘when’, ‘Gregor’, ‘Samsa’, ‘woke’, ‘from’, ‘troubled’, ‘dreams’, ‘he’, ‘found’, ‘himself’, ‘transformed’, ‘in’, ‘his’, ‘bed’, ‘into’, ‘a’, ‘horrible’, ‘vermin’, ‘He’, ‘lay’, ‘on’, ‘his’, ‘armour’, ‘like’, ‘back’, ‘and’, ‘if’, ‘he’, ‘lifted’, ‘his’, ‘head’, ‘a’, ‘little’, ‘he’, ‘could’, ‘see’, ‘his’, ‘brown’, ‘belly’, ‘slightly’, ‘domed’, ‘and’, ‘divided’, ‘by’, ‘arches’, ‘into’, ‘stiff’, ‘sections’, ‘The’, ‘bedding’, ‘was’, ‘hardly’, ‘able’, ‘to’, ‘cover’, ‘it’, ‘and’, ‘seemed’, ‘ready’, ‘to’, ‘slide’, ‘off’, ‘any’, ‘moment’, ‘His’, ‘many’, ‘legs’, ‘pitifully’, ‘thin’, ‘compared’, ‘with’, ‘the’, ‘size’, ‘of’, ‘the’, ‘rest’, ‘of’, ‘him’, ‘waved’, ‘about’, ‘helplessly’, ‘as’, ‘he’, ‘looked’, ‘What’, ‘s’, ‘happened’, ‘to’, ‘me’, ‘he’, ‘thought’, ‘It’, ‘wasn’, ‘t’, ‘a’, ‘dream’, ‘His’, ‘room’]
3- 通过 i) 按空格分割 ii) 移除标点符号进行清理
[‘One’, ‘morning’, ‘when’, ‘Gregor’, ‘Samsa’, ‘woke’, ‘from’, ‘troubled’, ‘dreams’, ‘he’, ‘found’, ‘himself’, ‘transformed’, ‘in’, ‘his’, ‘bed’, ‘into’, ‘a’, ‘horrible’, ‘vermin’, ‘He’, ‘lay’, ‘on’, ‘his’, ‘armourlike’, ‘back’, ‘and’, ‘if’, ‘he’, ‘lifted’, ‘his’, ‘head’, ‘a’, ‘little’, ‘he’, ‘could’, ‘see’, ‘his’, ‘brown’, ‘belly’, ‘slightly’, ‘domed’, ‘and’, ‘divided’, ‘by’, ‘arches’, ‘into’, ‘stiff’, ‘sections’, ‘The’, ‘bedding’, ‘was’, ‘hardly’, ‘able’, ‘to’, ‘cover’, ‘it’, ‘and’, ‘seemed’, ‘ready’, ‘to’, ‘slide’, ‘off’, ‘any’, ‘moment’, ‘His’, ‘many’, ‘legs’, ‘pitifully’, ‘thin’, ‘compared’, ‘with’, ‘the’, ‘size’, ‘of’, ‘the’, ‘rest’, ‘of’, ‘him’, ‘waved’, ‘about’, ‘helplessly’, ‘as’, ‘he’, ‘looked’, ‘Whats’, ‘happened’, ‘to’, ‘me’, ‘he’, ‘thought’, ‘It’, ‘wasnt’, ‘a’, ‘dream’, ‘His’, ‘room’, ‘a’, ‘proper’, ‘human’]
4 – 按空格分割并转换为小写进行清理
[‘one’, ‘morning,’, ‘when’, ‘gregor’, ‘samsa’, ‘woke’, ‘from’, ‘troubled’, ‘dreams,’, ‘he’, ‘found’, ‘himself’, ‘transformed’, ‘in’, ‘his’, ‘bed’, ‘into’, ‘a’, ‘horrible’, ‘vermin.’, ‘he’, ‘lay’, ‘on’, ‘his’, ‘armour-like’, ‘back,’, ‘and’, ‘if’, ‘he’, ‘lifted’, ‘his’, ‘head’, ‘a’, ‘little’, ‘he’, ‘could’, ‘see’, ‘his’, ‘brown’, ‘belly,’, ‘slightly’, ‘domed’, ‘and’, ‘divided’, ‘by’, ‘arches’, ‘into’, ‘stiff’, ‘sections.’, ‘the’, ‘bedding’, ‘was’, ‘hardly’, ‘able’, ‘to’, ‘cover’, ‘it’, ‘and’, ‘seemed’, ‘ready’, ‘to’, ‘slide’, ‘off’, ‘any’, ‘moment.’, ‘his’, ‘many’, ‘legs,’, ‘pitifully’, ‘thin’, ‘compared’, ‘with’, ‘the’, ‘size’, ‘of’, ‘the’, ‘rest’, ‘of’, ‘him,’, ‘waved’, ‘about’, ‘helplessly’, ‘as’, ‘he’, ‘looked.’, ‘”what\’s’, ‘happened’, ‘to’, ‘me?”‘, ‘he’, ‘thought.’, ‘it’, “wasn’t”, ‘a’, ‘dream.’, ‘his’, ‘room,’, ‘a’, ‘proper’, ‘human’]
5- 句子级别的 nltk 分词
One morning, when Gregor Samsa woke from troubled dreams, he found
himself transformed in his bed into a horrible vermin.
He lay on
his armour-like back, and if he lifted his head a little he could
see his brown belly, slightly domed and divided by arches into stiff
sections.
The bedding was hardly able to cover it and seemed ready
to slide off any moment.
His many legs, pitifully thin compared
with the size of the rest of him, waved about helplessly as he
looked.
6 – 词级别的 nltk 分词
[‘One’, ‘morning’, ‘,’, ‘when’, ‘Gregor’, ‘Samsa’, ‘woke’, ‘from’, ‘troubled’, ‘dreams’, ‘,’, ‘he’, ‘found’, ‘himself’, ‘transformed’, ‘in’, ‘his’, ‘bed’, ‘into’, ‘a’, ‘horrible’, ‘vermin’, ‘.’, ‘He’, ‘lay’, ‘on’, ‘his’, ‘armour-like’, ‘back’, ‘,’, ‘and’, ‘if’, ‘he’, ‘lifted’, ‘his’, ‘head’, ‘a’, ‘little’, ‘he’, ‘could’, ‘see’, ‘his’, ‘brown’, ‘belly’, ‘,’, ‘slightly’, ‘domed’, ‘and’, ‘divided’, ‘by’, ‘arches’, ‘into’, ‘stiff’, ‘sections’, ‘.’, ‘The’, ‘bedding’, ‘was’, ‘hardly’, ‘able’, ‘to’, ‘cover’, ‘it’, ‘and’, ‘seemed’, ‘ready’, ‘to’, ‘slide’, ‘off’, ‘any’, ‘moment’, ‘.’, ‘His’, ‘many’, ‘legs’, ‘,’, ‘pitifully’, ‘thin’, ‘compared’, ‘with’, ‘the’, ‘size’, ‘of’, ‘the’, ‘rest’, ‘of’, ‘him’, ‘,’, ‘waved’, ‘about’, ‘helplessly’, ‘as’, ‘he’, ‘looked’, ‘.’, ‘
‘, ‘What’, “‘s”, ‘happened’, ‘to’]
7- nltk 分词并移除非字母标记
[‘One’, ‘morning’, ‘when’, ‘Gregor’, ‘Samsa’, ‘woke’, ‘from’, ‘troubled’, ‘dreams’, ‘he’, ‘found’, ‘himself’, ‘transformed’, ‘in’, ‘his’, ‘bed’, ‘into’, ‘a’, ‘horrible’, ‘vermin’, ‘He’, ‘lay’, ‘on’, ‘his’, ‘back’, ‘and’, ‘if’, ‘he’, ‘lifted’, ‘his’, ‘head’, ‘a’, ‘little’, ‘he’, ‘could’, ‘see’, ‘his’, ‘brown’, ‘belly’, ‘slightly’, ‘domed’, ‘and’, ‘divided’, ‘by’, ‘arches’, ‘into’, ‘stiff’, ‘sections’, ‘The’, ‘bedding’, ‘was’, ‘hardly’, ‘able’, ‘to’, ‘cover’, ‘it’, ‘and’, ‘seemed’, ‘ready’, ‘to’, ‘slide’, ‘off’, ‘any’, ‘moment’, ‘His’, ‘many’, ‘legs’, ‘pitifully’, ‘thin’, ‘compared’, ‘with’, ‘the’, ‘size’, ‘of’, ‘the’, ‘rest’, ‘of’, ‘him’, ‘waved’, ‘about’, ‘helplessly’, ‘as’, ‘he’, ‘looked’, ‘What’, ‘happened’, ‘to’, ‘me’, ‘he’, ‘thought’, ‘It’, ‘was’, ‘a’, ‘dream’, ‘His’, ‘room’, ‘a’, ‘proper’, ‘human’, ‘room’]
8 – 清洗管道:nltk 词分词/转小写/移除标点符号/移除剩余的非字母字符/过滤停用词
[‘one’, ‘morning’, ‘gregor’, ‘samsa’, ‘woke’, ‘troubled’, ‘dreams’, ‘found’, ‘transformed’, ‘bed’, ‘horrible’, ‘vermin’, ‘lay’, ‘armourlike’, ‘back’, ‘lifted’, ‘head’, ‘little’, ‘could’, ‘see’, ‘brown’, ‘belly’, ‘slightly’, ‘domed’, ‘divided’, ‘arches’, ‘stiff’, ‘sections’, ‘bedding’, ‘hardly’, ‘able’, ‘cover’, ‘seemed’, ‘ready’, ‘slide’, ‘moment’, ‘many’, ‘legs’, ‘pitifully’, ‘thin’, ‘compared’, ‘size’, ‘rest’, ‘waved’, ‘helplessly’, ‘looked’, ‘happened’, ‘thought’, ‘nt’, ‘dream’, ‘room’, ‘proper’, ‘human’, ‘room’, ‘although’, ‘little’, ‘small’, ‘lay’, ‘peacefully’, ‘four’, ‘familiar’, ‘walls’, ‘collection’, ‘textile’, ‘samples’, ‘lay’, ‘spread’, ‘table’, ‘samsa’, ‘travelling’, ‘salesman’, ‘hung’, ‘picture’, ‘recently’, ‘cut’, ‘illustrated’, ‘magazine’, ‘housed’, ‘nice’, ‘gilded’, ‘frame’, ‘showed’, ‘lady’, ‘fitted’, ‘fur’, ‘hat’, ‘fur’, ‘boa’, ‘sat’, ‘upright’, ‘raising’, ‘heavy’, ‘fur’, ‘muff’, ‘covered’, ‘whole’, ‘lower’, ‘arm’, ‘towards’, ‘viewer’]
感谢分享。
你好!!
Day3 任务的文档是
‘在实验室里,他们研究一个重要的化学公式’,
‘数学家经常使用这个公式’,
‘一场重要的F1方程式比赛将于明天举行’,
‘我过去常常早起’,
‘很多时候事情并不像它们看起来那样’
(想法是验证 IDF 如何降低像“formula”这样的单词的贡献,尽管它们不是非常常见,但它们在 3 个不同的文档中出现(有 3 种不同的含义)。在这种情况下,该单词没有为前 3 个文档提供区分特征)
1-词袋模型表示 – Scikit-learn TF-IDF
{‘wake’: 27, ‘as’: 3, ‘mathematician’: 10, ‘are’: 2, ‘will’: 28, ‘race’: 16, ‘the’: 19, ‘laboratory’: 9, ‘take’: 18, ‘to’: 23, ‘use’: 26, ‘early’: 5, ‘formula’: 6, ‘this’: 22, ‘up’: 25, ‘chemical’: 4, ‘morning’: 11, ‘study’: 17, ‘tomorrow’: 24, ‘in’: 8, ‘appear’: 1, ‘not’: 12, ‘important’: 7, ‘things’: 21, ‘of’: 13, ‘place’: 15, ‘they’: 20, ‘often’: 14, ‘an’: 0}
[1.69314718 2.09861229 2.09861229 2.09861229 2.09861229 2.09861229
1.40546511 1.69314718 1.69314718 2.09861229 2.09861229 2.09861229
2.09861229 2.09861229 1.69314718 2.09861229 2.09861229 2.09861229
2.09861229 1.69314718 1.69314718 2.09861229 2.09861229 2.09861229
2.09861229 2.09861229 1.69314718 2.09861229 2.09861229]
文档 1
(1, 29)
[[0.31161965 0. 0. 0. 0.38624453 0.
0.25867246 0.31161965 0.31161965 0.38624453 0. 0.
0. 0. 0. 0. 0. 0.38624453
0. 0.31161965 0.31161965 0. 0. 0.
0. 0. 0. 0. 0. ]]
文档 2
(1, 29)
[[0. 0. 0. 0. 0. 0.
0.34582166 0. 0. 0. 0.51637397 0.
0. 0. 0.41660727 0. 0. 0.
0. 0. 0. 0. 0.51637397 0.
0. 0. 0.41660727 0. 0. ]]
文档 3
(1, 29)
[[0.28980239 0. 0. 0. 0. 0.
0.24056216 0.28980239 0. 0. 0. 0.
0. 0.35920259 0. 0.35920259 0.35920259 0.
0.35920259 0. 0. 0. 0. 0.
0.35920259 0. 0. 0. 0.35920259]]
文档 4
(1, 29)
[[0. 0. 0. 0. 0. 0.37924665
0. 0. 0.30597381 0. 0. 0.37924665
0. 0. 0. 0. 0. 0.
0. 0.30597381 0. 0. 0. 0.37924665
0. 0.37924665 0.30597381 0.37924665 0. ]]
文档 5
(1, 29)
[[0. 0.39835162 0.39835162 0.39835162 0. 0.
0. 0. 0. 0. 0. 0.
0.39835162 0. 0.32138758 0. 0. 0.
0. 0. 0.32138758 0.39835162 0. 0.
0. 0. 0. 0. 0. ]]
2-词袋模型表示 – Keras-COUNT
OrderedDict([(‘in’, 2), (‘the’, 2), (‘laboratory’, 1), (‘they’, 2), (‘study’, 1), (‘an’, 2), (‘important’, 2), (‘chemical’, 1), (‘formula’, 3), (‘mathematician’, 1), (‘often’, 2), (‘use’, 2), (‘this’, 1), (‘race’, 1), (‘of’, 1), (‘1’, 1), (‘will’, 1), (‘take’, 1), (‘place’, 1), (‘tomorrow’, 1), (‘i’, 1), (‘to’, 1), (‘wake’, 1), (‘up’, 1), (‘early’, 1), (‘morning’, 1), (‘things’, 1), (‘are’, 1), (‘not’, 1), (‘as’, 1), (‘appear’, 1)])
5
{‘wake’: 23, ‘i’: 21, ‘things’: 27, ‘are’: 28, ‘as’: 30, ‘will’: 17, ‘race’: 14, ‘the’: 3, ‘this’: 13, ‘take’: 18, ‘use’: 8, ‘1’: 16, ‘formula’: 1, ‘early’: 25, ‘laboratory’: 9, ‘to’: 22, ‘up’: 24, ‘chemical’: 11, ‘morning’: 26, ‘study’: 10, ‘tomorrow’: 20, ‘in’: 2, ‘appear’: 31, ‘not’: 29, ‘important’: 6, ‘mathematician’: 12, ‘of’: 15, ‘place’: 19, ‘they’: 4, ‘often’: 7, ‘an’: 5}
defaultdict(, {‘wake’: 1, ‘i’: 1, ‘things’: 1, ‘are’: 1, ‘as’: 1, ‘will’: 1, ‘of’: 1, ‘the’: 2, ‘this’: 1, ‘take’: 1, ‘use’: 2, ‘1’: 1, ‘formula’: 3, ‘early’: 1, ‘laboratory’: 1, ‘to’: 1, ‘up’: 1, ‘chemical’: 1, ‘morning’: 1, ‘study’: 1, ‘tomorrow’: 1, ‘in’: 2, ‘appear’: 1, ‘not’: 1, ‘important’: 2, ‘mathematician’: 1, ‘race’: 1, ‘place’: 1, ‘they’: 2, ‘often’: 2, ‘an’: 2})
[[0. 1. 1. 1. 1. 1. 1. 0. 0. 1. 1. 1. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
[0. 1. 0. 0. 0. 0. 0. 1. 1. 0. 0. 0. 1. 1. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
[0. 1. 0. 0. 0. 1. 1. 0. 0. 0. 0. 0. 0. 0. 1. 1. 1. 1. 1. 1. 1. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
[0. 0. 1. 1. 0. 0. 0. 0. 1. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 1. 1. 1. 1. 1. 1. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 1. 0. 0. 1. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 1. 1. 1. 1. 1.]]
3- -词袋模型表示 Keras TF-IDF
OrderedDict([(‘in’, 2), (‘the’, 2), (‘laboratory’, 1), (‘they’, 2), (‘study’, 1), (‘an’, 2), (‘important’, 2), (‘chemical’, 1), (‘formula’, 3), (‘mathematician’, 1), (‘often’, 2), (‘use’, 2), (‘this’, 1), (‘race’, 1), (‘of’, 1), (‘1’, 1), (‘will’, 1), (‘take’, 1), (‘place’, 1), (‘tomorrow’, 1), (‘i’, 1), (‘to’, 1), (‘wake’, 1), (‘up’, 1), (‘early’, 1), (‘morning’, 1), (‘things’, 1), (‘are’, 1), (‘not’, 1), (‘as’, 1), (‘appear’, 1)])
5
{‘wake’: 23, ‘i’: 21, ‘things’: 27, ‘are’: 28, ‘as’: 30, ‘will’: 17, ‘race’: 14, ‘the’: 3, ‘this’: 13, ‘take’: 18, ‘use’: 8, ‘1’: 16, ‘formula’: 1, ‘early’: 25, ‘laboratory’: 9, ‘to’: 22, ‘up’: 24, ‘chemical’: 11, ‘morning’: 26, ‘study’: 10, ‘tomorrow’: 20, ‘in’: 2, ‘appear’: 31, ‘not’: 29, ‘important’: 6, ‘mathematician’: 12, ‘of’: 15, ‘place’: 19, ‘they’: 4, ‘often’: 7, ‘an’: 5}
defaultdict(, {‘wake’: 1, ‘i’: 1, ‘things’: 1, ‘are’: 1, ‘as’: 1, ‘will’: 1, ‘of’: 1, ‘the’: 2, ‘this’: 1, ‘take’: 1, ‘use’: 2, ‘1’: 1, ‘formula’: 3, ‘early’: 1, ‘laboratory’: 1, ‘to’: 1, ‘up’: 1, ‘chemical’: 1, ‘morning’: 1, ‘study’: 1, ‘tomorrow’: 1, ‘in’: 2, ‘appear’: 1, ‘not’: 1, ‘important’: 2, ‘mathematician’: 1, ‘race’: 1, ‘place’: 1, ‘they’: 2, ‘often’: 2, ‘an’: 2})
[[0. 0.81093022 0.98082925 0.98082925 0.98082925 0.98082925
0.98082925 0. 0. 1.25276297 1.25276297 1.25276297
0. 0. 0. 0. 0. 0.
0. 0. 0. 0. 0. 0.
0. 0. 0. 0. 0. 0.
0. 0. ]
[0. 0.81093022 0. 0. 0. 0.
0. 0.98082925 0.98082925 0. 0. 0.
1.25276297 1.25276297 0. 0. 0. 0.
0. 0. 0. 0. 0. 0.
0. 0. 0. 0. 0. 0.
0. 0. ]
[0. 0.81093022 0. 0. 0. 0.98082925
0.98082925 0. 0. 0. 0. 0.
0. 0. 1.25276297 1.25276297 1.25276297 1.25276297
1.25276297 1.25276297 1.25276297 0. 0. 0.
0. 0. 0. 0. 0. 0.
0. 0. ]
[0. 0. 0.98082925 0.98082925 0. 0.
0. 0. 0.98082925 0. 0. 0.
0. 0. 0. 0. 0. 0.
0. 0. 0. 1.25276297 1.25276297 1.25276297
1.25276297 1.25276297 1.25276297 0. 0. 0.
0. 0. ]
[0. 0. 0. 0. 0.98082925 0.
0. 0.98082925 0. 0. 0. 0.
0. 0. 0. 0. 0. 0.
0. 0. 0. 0. 0. 0.
0. 0. 0. 1.25276297 1.25276297 1.25276297
1.25276297 1.25276297]]
再见
干得好!
大家好
对于 day4 任务,我又使用了卡夫卡的《变形记》。
至于 Python 代码,我组合了教程代码来
– 对整个数据集(这本书)进行句子级别分词
– 对每个句子进行分词和清理(如 day2 所述)
然后我继续训练 word2vec 模型。
这是 Python 代码
from gensim.models import Word2Vec
from sklearn.decomposition import PCA
from matplotlib import pyplot
from nltk.tokenize import word_tokenize
import string
from nltk.corpus import stopwords
# 定义训练数据
# nltk 清理
filename = ‘metamorphosis_clean.txt’
file = open(filename, ‘rt’)
text = file.read()
file.close()
# 分成句子
from nltk import sent_tokenize
sentences = sent_tokenize(text)
sent_list = [];
for sent in sentences
sent_tokens = word_tokenize(sent)
sent_tokens = [w.lower() for w in sent_tokens]
table = str.maketrans(”, ”, string.punctuation)
stripped = [w.translate(table) for w in sent_tokens]
# 移除剩余的非字母标记
words = [word for word in stripped if word.isalpha()]
# 过滤停用词
stop_words = set(stopwords.words(‘english’))
words = [w for w in words if not w in stop_words]
sent_list.append(words)
print(sent_list)
# 训练模型
model = Word2Vec(sent_list, min_count=1)
# 拟合一个2D PCA模型到向量
X = model[model.wv.vocab]
pca = PCA(n_components=2)
result = pca.fit_transform(X)
# 创建投影的散点图
pyplot.scatter(result[:, 0], result[:, 1])
words = list(model.wv.vocab)
for i, word in enumerate(words)
pyplot.annotate(word, xy=(result[i, 0], result[i, 1]))
pyplot.show()
model.wv.save_word2vec_format(‘model.txt’, binary=False);
我保存了两种情况下的词嵌入模型的ASCII模型:一种是在清理阶段使用了停用词移除,另一种没有使用(结果不同)。
我想问一下,您是否能帮我放大 PCA 分析的散点图,因为当使用相对较大的词汇量时,默认的视觉效果是无法理解的。
谢谢,并致以诚挚的问候
Paolo Bussotti
感谢分享!
更多关于改变图形大小的信息,请在
https://matplotlib.net.cn/3.1.0/api/_as_gen/matplotlib.pyplot.figure.html
Day5 测试
—————– 测试 1 ———————————————-
设置:vocab_length = 100(以最小化与 one_hot hashing 的冲突)
嵌入大小 = 8
正面句子(标签 = 1)
‘在这美丽的日落前,我感觉非常好’,
‘你的蛋糕味道很好’,
‘我看到的画被美丽地绘制了’,
‘你的成功是当之无愧的’,
‘我享受了一个非常愉快的下午’,
负面句子(标签 = 0)
‘多么可怕的话!’,
‘你的工作做得不好’,
‘绝望使他追求了错误的目标’,
‘失败让你陷入绝望’,
‘愤怒往往源于绝望’
[[10 3 13 12 18 24 19 4]
[ 1 36 40 43 10 30 7 0]
[49 42 10 12 35 21 34 0]
[ 1 2 35 12 5 0 0 0]
[10 23 43 10 30 43 0 0]
[ 4 43 36 44 8 1 0 0]
[ 1 19 43 6 33 0 0 0]
[20 46 4 9 25 29 0 0]
[26 40 34 22 8 25 0 0]
[21 20 26 32 25 0 0 0]]
_________________________________________________________________
层(类型) 输出形状 参数 #
=================================================================
embedding_49 (Embedding) (None, 8, 8) 400
_________________________________________________________________
flatten_49 (Flatten) (None, 64) 0
_________________________________________________________________
dense_49 (Dense) (None, 1) 65
=================================================================
总参数:465
可训练参数:465
不可训练参数: 0
_________________________________________________________________
无
准确率:100.000000
准确率是使用代码获得的
model.fit(padded_docs, labels, epochs=50, verbose=0)
loss, accuracy = model.evaluate(padded_docs, labels, verbose=0)
print(‘Accuracy: %f’ % (accuracy*100))
——————- 测试 2 ——————————————–
使用“metamorphosis”的预训练嵌入权重,
并评估上面相同的 10 个句子(嵌入大小 = 100),我得到了
正面句子(标签 = 1)
‘在这美丽的日落前,我感觉非常好’,
‘你的蛋糕味道很好’,
‘我看到的画被美丽地绘制了’,
‘你的成功是当之无愧的’,
‘我享受了一个非常愉快的下午’,
负面句子(标签 = 0)
‘多么可怕的话!’,
‘你的工作做得不好’,
‘绝望使他追求了错误的目标’,
‘失败让你陷入绝望’,
‘愤怒往往源于绝望’
vocab_size = 49
[[1, 11, 12, 4, 13, 14, 15, 16], [2, 17, 5, 3, 6, 7, 18], [19, 20, 1, 21, 8, 22, 23],
[2, 24, 8, 4, 25], [1, 26, 3, 6, 7, 27], [28, 3, 29, 30, 9, 31],
[2, 32, 33, 34, 35], [36, 37, 38, 39, 40, 41], [42, 5, 43, 44, 9, 10], [45, 46, 47, 48, 10]]
[[ 1 11 12 4 13 14 15 16]
[ 2 17 5 3 6 7 18 0]
[19 20 1 21 8 22 23 0]
[ 2 24 8 4 25 0 0 0]
[ 1 26 3 6 7 27 0 0]
[28 3 29 30 9 31 0 0]
[ 2 32 33 34 35 0 0 0]
[36 37 38 39 40 41 0 0]
[42 5 43 44 9 10 0 0]
[45 46 47 48 10 0 0 0]]
加载了 2581 个词向量。
Embedding_matrix (预训练权重)
[[ 0.00000000e+00 0.00000000e+00 0.00000000e+00 … 0.00000000e+00
0.00000000e+00 0.00000000e+00]
[-5.84273934e-01 4.10322547e-02 5.30006588e-01 … 2.99149841e-01
6.40603900e-02 4.65089172e-01]
[-1.88604981e-01 1.39787989e-02 1.74110234e-01 … 1.01836950e-01
2.37775557e-02 1.49607286e-01]
…
[-2.63660215e-03 1.93641812e-03 -1.67637059e-04 … 4.98276250e-03
4.28335834e-03 -9.61201615e-04]
[ 0.00000000e+00 0.00000000e+00 0.00000000e+00 … 0.00000000e+00
0.00000000e+00 0.00000000e+00]
[-6.03308380e-01 4.21844646e-02 5.49575210e-01 … 3.08609307e-01
6.65459782e-02 4.79907840e-01]]
_________________________________________________________________
层(类型) 输出形状 参数 #
=================================================================
embedding_61 (Embedding) (None, 8, 100) 4900
_________________________________________________________________
flatten_60 (Flatten) (None, 800) 0
_________________________________________________________________
dense_60 (Dense) (None, 1) 801
=================================================================
总参数:5,701
可训练参数:801
不可训练参数:4,900
_________________________________________________________________
无
准确率:89.999998
干得好!
我使用了 https://machinelearning.org.cn/use-word-embedding-layers-deep-learning-keras/ 中的代码,并替换了十个句子和预训练文件(即 Gensim 获得的 metamorphosis 词嵌入)。
此致
Paolo Bussotti
我有一个问题,当我们使用深度学习中的 sigmoid 激活函数时,我们的输出是以概率的形式出现的。但我需要输出是 0 和 1 的形式,为此我必须设置一个阈值,那么最好的阈值是多少??
你可以通过四舍五入来得到一个清晰的类别标签。
亲爱的 Jason,
我被卡在了一个最基本的问题上——分词。当我尝试运行你课程 2 中的 NLTK 示例代码时,我遇到了以下问题(见下文)。你有什么想法可能是什么问题吗?
谢谢你的帮助!
Špela
(base) spelavintar@Spelas-Air ~/Documents/python $ python tokenize.py
回溯(最近一次调用)
File “tokenize.py”, line 7, in
from nltk.tokenize import word_tokenize
File “/opt/anaconda3/lib/python3.7/site-packages/nltk/__init__.py”, line 98, in
from nltk.internals import config_java
文件“/opt/anaconda3/lib/python3.7/site-packages/nltk/internals.py”,第10行,在
导入 subprocess
文件“/opt/anaconda3/lib/python3.7/subprocess.py”,第155行,在
导入 threading
文件“/opt/anaconda3/lib/python3.7/threading.py”,第8行,在
从 traceback import format_exc as _format_exc
文件“/opt/anaconda3/lib/python3.7/traceback.py”,第5行,在
导入 linecache
文件“/opt/anaconda3/lib/python3.7/linecache.py”,第11行,在
导入 tokenize
文件“/Users/spelavintar/Documents/python/tokenize.py”,第7行,在
from nltk.tokenize import word_tokenize
文件“/opt/anaconda3/lib/python3.7/site-packages/nltk/tokenize/__init__.py”,第65行,在
从 nltk.data import load
文件“/opt/anaconda3/lib/python3.7/site-packages/nltk/data.py”,第58行,在
从 nltk.internals import deprecated
ImportError: cannot import name ‘deprecated’ from ‘nltk.internals’ (/opt/anaconda3/lib/python3.7/site-packages/nltk/internals.py)
听到这个消息我很遗憾,这可能是版本问题。
这个可能会有帮助
https://machinelearning.org.cn/faq/single-faq/why-does-the-code-in-the-tutorial-not-work-for-me
还有这个。
https://machinelearning.org.cn/setup-python-environment-machine-learning-deep-learning-anaconda/
非常有帮助,我很喜欢这篇文章,做得很好,感谢分享这样的信息。
谢谢!
我把第四项任务的一个 notebook 放在这里:https://www.kaggle.com/drandrewshannon/word-tokens-from-genesis,我在那里对《创世纪》做了这件事。我必须清理掉所有的数字,而且它仍然有点奇怪——大多数词都聚集在原点附近,而使用次数非常多的词则分散到高 X,并且 abs(Y) <~ X。
“And”和“and”的距离不近,这让我困扰了一段时间,但没关系,它们的使用方式不完全相同,所以这是有道理的。不过也许我应该全部转为小写。
干得好。
谢谢。我还把第五项任务的 notebook 放在这里 https://www.kaggle.com/drandrewshannon/learned-embedding——我花了令人尴尬的时间才理解分词器是如何与嵌入层交互的——我认为代码可能有效了至少二十分钟,直到我才明白为什么。
话虽如此,尽管它可以在训练数据上达到 100%,但我非常确定这只是对小样本的过拟合;当我将其应用于其他句子时,它基本上是随机的,模型值都在 0.45-0.55 的范围内,我将其解释为模型对其答案的置信度很低。
干得好!
我最近开始了这门课程,这是我对第一项任务的答案。
1. 调整词/短语空间
2. 清理录音,以便更容易地进行语音识别
3. 将输入文本分割成短语并理解其结构
4. 根据给定的想法构建句子
5. 查找给定文本中的风格或语法错误
6. 评估文本质量和逻辑
7. 从文本中提取数据以供将来使用
8. 文本翻译
9. 从录音中识别人的口音或方言
10. 识别和改变文本风格
干得好!
嗨,Jason,
我的答案
1. 句子检测(正面或负面)
2. 人物情感分析
3. 聊天机器人应用程序(Alexa,Siri。https://medium.com/@ODSC/top-10-ai-chatbot-research-papers-from-axxiv-org-in-2019-1982dddabdb4)
4. 电子邮件垃圾邮件检测
5. 输入辅助
6. 谷歌搜索引擎语音助手
7. 单词到图像转换
8. 阅读路边文字并转换为自动驾驶汽车的行动
9. 多语言音频转换
10. 语言翻译(谷歌翻译)
干得好!
嗨 Jason,我尝试使用像您邮件中那样只读取此文件,在第1347行针对故事书“Midshipman Marrill”出现错误。
然后我在网上查了一下,找到了解决方案。
旧的
file = open(filename, ‘rt’)
新的
file = open(filename, encoding=”utf8″)
这样就能工作了。
感谢分享。
如何共享经过处理的单词的新文件?
project gutenberg ebook midshipman merril henri harrison lewi ebook use anyon anywher unit state part world cost almost restrict whatsoev may copi give away reus term project gutenberg licens includ ebook onlin wwwgutenbergorg locat unit state check law countri locat use ebook titl midshipman merril author henri harrison lewi releas date novemb ebook languag english charact set encod produc demian katz craig kirkwood onlin distribut proofread team http wwwpgdpnet imag courtesi digit librari villanova univers http digitallibraryvillanovaedu
词袋模型:这对于短句和段落来说效果很好。对于大文档,它会创建一个稀疏矩阵,并且对序列没有意义。
稀疏矩阵问题将通过以下方式解决:
TF-IDF:分别处理文档中的词频和常用词频率(这会给常用词分配较低的频率)。
独热编码:创建具有不同词权重的矩阵,并且是一个密集矩阵,重视序列。这将有助于搜索引擎、语言翻译等。
感谢分享。
第一天的任务
1. 文本分类和分类
2. 拼写检查
3. 字符识别
4. 词性标注
5. 命名实体识别 (NER)
6. 语义解析和问答
7. 语言生成和多文档摘要
8. 机器翻译
9. 语音识别
10. 释义检测
干得好!
请在下方找到我对深度学习在自然语言处理领域应用的列表。
1. NMT(例如,AE 应用于 NIPS 2017 会议论文“Attention Is All You Need”https://papers.nips.cc/paper/2017/file/3f5ee243547dee91fbd053c1c4a845aa-Paper.pdf)
2. 语音识别领域的一系列研究
3. —//— 文本纠错领域
4. —//— 文本生成系统(包括对话生成)
5. 基于语音的健康诊断预测(例如,ACL 2020 会议论文“Predicting Depression in Screening Interviews from Latent Categorization of Interview Prompts”https://www.aclweb.org/anthology/2020.acl-main.2.pdf)
6. 定义文本情感(例如,ACL 2020 会议论文“Learning and Evaluating Emotion Lexicons for 91 Languages”https://www.aclweb.org/anthology/2020.acl-main.112.pdf)
7. 文本摘要(例如,标题/关键词生成)
8. 文本分类领域的一系列研究
9. NL 到机器语言翻译(包括 NL2(p)SQL:“NL2pSQL: Generating Pseudo SQL Queries from Under-Specified Natural Language Questions”,EMNLP 2019 会议论文https://www.aclweb.org/anthology/D19-1262.pdf)
10. 检测假新闻、垃圾邮件等领域的一系列研究
干得好!
在学习第二课时,我发现 NLTK 库不支持韩语。
可以使用 konply 库代替,但大多数教程都是韩语的。
谢谢你的提醒!
第一天
1- 邮件分类
https://medium.com/@morga046/multi-class-text-classification-with-doc2vec-and-t-sne-a-full-tutorial-55eb24fc40d3
2- 情感分析(coursera)
3- 生成字幕 https://medium.com/@samim/generating-captions-c31f00e8396e
4- 序列到序列预测 https://www.analyticsvidhya.com/blog/2019/01/neural-machine-translation-keras/
5- 聊天机器人 https://www.ultimate.ai/blog/ai-automation/how-nlp-text-based-chatbots-work
6- 语言翻译(谷歌翻译/deepl翻译)
7- 文本摘要
8- ML 驱动的自动完成功能(gmail 自动完成和智能回复)
9- 预测文本生成器
10- 假新闻检测
干得好!
1. 分词和文本分类(https://arxiv.org/abs/1408.5882)
2. 为图像生成字幕(https://www.cv-foundation.org/openaccess/content_cvpr_2015/papers/Vinyals_Show_and_Tell_2015_CVPR_paper.pdf)
3. 语音识别(https://ieeexplore.ieee.org/document/8632885/footnotes#footnotes)
4. 机器翻译(https://arxiv.org/abs/1409.0473)
5. 问答(https://www.sciencedirect.com/science/article/pii/S1877050918308226)
6. 文档摘要(http://www.abigailsee.com/2017/04/16/taming-rnns-for-better-summarization.html)
7. 语言建模(http://www.jmlr.org/papers/v3/bengio03a.html)
8. 聊天机器人(https://www.researchgate.net/publication/328582617_Intelligent_Chatbot_using_Deep_Learning)
9. NER(https://arxiv.org/abs/1812.09449)
10. 抄袭检测(https://www.researchgate.net/publication/339761241_A_deep_learning_based_technique_for_plagiarism_detection_a_comparative_study)
干得好!
第一天任务
深度学习在自然语言处理中的应用
1. 使用文本预测完成句子,例如,Gmail 的自动完成功能。
https://www.researchgate.net/publication/335743393_Sentence_Completion_using_NLP_Techniques
2. 语法和拼写检查器,例如,Grammarly 网页应用。
https://arxiv.org/pdf/1804.00540.pdf
3. 构建自动聊天机器人
https://arxiv.org/pdf/1909.03653.pdf
4. 语音识别
https://www.ijrte.org/wp-content/uploads/papers/v7i6C/F90340476C19.pdf
5. 文本分类器 [将亚马逊产品评论、电影评论或书籍评论分类为正面、负面或中性]
https://arxiv.org/pdf/2004.03705.pdf
6. 语言翻译器
https://www.researchgate.net/publication/326722662_Development_of_an_automated_English-to-local-language_translator_using_Natural_Language_Processing
7. 定向广告 [在在线购物平台上]
https://www.researchgate.net/publication/263895651_Web_based_Targeted_Advertising_A_Study_based_on_Patent_Information
8. 字幕生成器
https://arxiv.org/abs/1502.03044
9. 文档摘要
https://arxiv.org/abs/1509.00685
10. 紧急情况检测
https://www.researchgate.net/publication/342785664_On_detecting_urgency_in_short_crisis_messages_using_minimal_supervision_and_transfer_learning
干得好!
嗨 Jason
感谢您的教程!
我回到这个 NLP DL 速成课程,寻找文本分类教程的端到端应用参考。
您是否有除了经典的“情感分析”之外的任何其他 NLP 文本分类教程?我特别对文本多标签分类感兴趣。
谢谢
我记不太清楚了,暂时没有。
任何二元文本分类的例子都可以直接适应多分类,例如在这里的“文本分类”下。
https://machinelearning.org.cn/start-here/#nlp
好的,谢谢 Jason。
我还发现了这个多标签分类教程。
https://machinelearning.org.cn/multi-label-classification-with-deep-learning/
在那里,它很好地解释了如何调整模型输出层(到类数并保留“sigmoid”作为激活参数)以及模型编译方法(保留例如 =binary_crossentropy”作为损失函数)。
困惑之处在于使用多个输出神经元进行多分类,但使用“sigmoid”作为激活函数和 binary_crossentropy 作为损失函数,当我们使用多标签时,即使这些是“二元分类实现”的正确参数。
啊,我明白了。我不确定我以前是否处理过这种情况。
谢谢。
不客气!
第二天任务
书名:《多利安·格雷的画像》 作者:奥斯卡·王尔德
总字数:79151
manual_tokenize.py
filename = ‘ebook.txt’
file = open(filename, ‘rt’, encoding=’utf-8′)
text = file.read()
file.close()
# 按空格分割成单词
words = text.split()
# 转换为小写
words = [word.lower() for word in words]
# 删除每个单词的标点符号
import string
table = str.maketrans(”, ”, string.punctuation)
words = [w.translate(table) for w in words]
# 删除非字母单词
words = [word for word in words if word.isalpha()]
cleaned = words
print(len(cleaned))
print(cleaned[:50])
输出
77941
[‘preface’, ‘the’, ‘artist’, ‘is’, ‘the’, ‘creator’, ‘of’, ‘beautiful’, ‘things’, ‘to’, ‘reveal’, ‘art’, ‘and’, ‘conceal’, ‘the’, ‘artist’, ‘is’, ‘aim’, ‘the’, ‘critic’, ‘is’, ‘he’, ‘who’, ‘can’, ‘translate’, ‘into’, ‘another’, ‘manner’, ‘or’, ‘a’, ‘new’, ‘material’, ‘his’, ‘impression’, ‘of’, ‘beautiful’, ‘things’, ‘the’, ‘highest’, ‘as’, ‘the’, ‘lowest’, ‘form’, ‘of’, ‘criticism’, ‘is’, ‘a’, ‘mode’, ‘of’, ‘autobiography’]
nltk_tokenize.py
# 加载数据
filename = ‘ebook.txt’
file = open(filename, ‘rt’, encoding=’utf-8′)
text = file.read()
file.close()
# 分割成单词
from nltk.tokenize import word_tokenize
tokens = word_tokenize(text)
# 转换为小写
tokens = [w.lower() for w in tokens]
# 删除每个单词的标点符号
import string
table = str.maketrans(”, ”, string.punctuation)
tokens = [w.translate(table) for w in tokens]
#删除非字母符号
tokens = [word for word in tokens if word.isalpha()]
# 过滤掉停用词
from nltk.corpus import stopwords
stop_words = set(stopwords.words(‘english’))
tokens = [w for w in tokens if not w in stop_words]
# 词干提取
from nltk.stem.porter import PorterStemmer
porter = PorterStemmer()
tokens = [porter.stem(word) for word in tokens]
cleaned = tokens
print(len(cleaned))
print(cleaned[:50])
输出
36464
[‘prefac’, ‘artist’, ‘creator’, ‘beauti’, ‘thing’, ‘reveal’, ‘art’, ‘conceal’, ‘artist’, ‘art’, ‘aim’, ‘critic’, ‘translat’, ‘anoth’, ‘manner’, ‘new’, ‘materi’, ‘impress’, ‘beauti’, ‘thing’, ‘highest’, ‘lowest’, ‘form’, ‘critic’, ‘mode’, ‘autobiographi’, ‘find’, ‘ugli’, ‘mean’, ‘beauti’, ‘thing’, ‘corrupt’, ‘without’, ‘charm’, ‘fault’, ‘find’, ‘beauti’, ‘mean’, ‘beauti’, ‘thing’, ‘cultiv’, ‘hope’, ‘elect’, ‘beauti’, ‘thing’, ‘mean’, ‘beauti’, ‘thing’, ‘moral’, ‘immor’]
谢谢。
做得很好!感谢分享您的工作。
第三天任务
BOW(词袋模型)使用 Keras
# 文本文档列表
docs = [“This book is so funny”,
“This book is so boring!”,
“This book is very long..”,
“This is a bestseller novel”,
“This is a romantic novel”]
import nltk
from nltk.stem import PorterStemmer
from nltk.corpus import stopwords
import string
table = str.maketrans(”, ”, string.punctuation)
stemmer = PorterStemmer() #初始化词干提取器
stop_words = set(stopwords.words(‘english’))
for i in range(len(docs)): #循环遍历每个文档
words = nltk.word_tokenize(docs[i]) #从文档中提取每个单词
words = [w.lower() for w in words] #转换为小写
words = [w.translate(table) for w in words] # 从每个单词中删除标点符号
words = [word for word in words if word.isalpha()]#删除非字母符号
words = [stemmer.stem(word) for word in words if word not in stop_words] #查找每个单词的词干
docs[i] = ‘ ‘.join(words)
from keras.preprocessing.text import Tokenizer
print(‘Text After cleaning:’,docs)
# 创建分词器
t = Tokenizer()
# 在文档上拟合分词器
t.fit_on_texts(docs)
# 总结所学内容
#print(t.word_counts)
#print(t.document_count)
#print(t.word_index)
vocabList = [‘0′]
for key in t.word_index
vocabList.append(key)
#print(t.word_docs)
# 整数编码文档
#encoded_docs = t.texts_to_matrix(docs, mode=’count’)
encoded_docs = t.texts_to_matrix(docs, mode=’tfidf’)
#encoded_docs = t.texts_to_matrix(docs, mode=’binary’)
#print(encoded_docs)
import pandas as pd
df = pd.DataFrame(encoded_docs,columns=vocabList)
print(df)
输出
Text After cleaning: [‘book funni’, ‘book bore’, ‘book long’, ‘bestsel novel’, ‘romant novel’]
0 book novel funni bore long bestsel romant
0 0.0 0.81093 0.000000 1.252763 0.000000 0.000000 0.000000 0.000000
1 0.0 0.81093 0.000000 0.000000 1.252763 0.000000 0.000000 0.000000
2 0.0 0.81093 0.000000 0.000000 0.000000 1.252763 0.000000 0.000000
3 0.0 0.00000 0.980829 0.000000 0.000000 0.000000 1.252763 0.000000
4 0.0 0.00000 0.980829 0.000000 0.000000 0.000000 0.000000 1.252763
BOW 使用 SciKit
# 文本文档列表
text = [“This book is so funny”,
“This book is so boring!”,
“This book is very long..”,
“This is a bestseller novel”,
“This is a romantic novel”]
import nltk
from nltk.stem import PorterStemmer
from nltk.corpus import stopwords
import string
table = str.maketrans(”, ”, string.punctuation)
stemmer = PorterStemmer() #初始化词干提取器
stop_words = set(stopwords.words(‘english’))
for i in range(len(text)): #循环遍历每个文档
words = nltk.word_tokenize(text[i]) #从文档中提取每个单词
words = [w.lower() for w in words] #转换为小写
words = [w.translate(table) for w in words] # 从每个单词中删除标点符号
words = [word for word in words if word.isalpha()]#删除非字母符号
words = [stemmer.stem(word) for word in words if word not in stop_words] #查找每个单词的词干
text[i] = ‘ ‘.join(words)
print(‘Text After cleaning:’,text)
from sklearn.feature_extraction.text import TfidfVectorizer
#from sklearn.feature_extraction.text import CountVectorizer
#from sklearn.feature_extraction.text import HashingVectorizer
# 创建转换器
vectorizer = TfidfVectorizer()
#vectorizer = CountVectorizer()
#vectorizer = HashingVectorizer() # 适用于大型语料库
# 分词并构建词汇表
vectorizer.fit(text)
# 总结
#print(vectorizer.vocabulary_)
#print(vectorizer.get_feature_names())
#print(vectorizer.idf_)
# 对所有文档进行编码
vector = vectorizer.transform(text)
# 总结编码向量
#print(vector.shape)
#print(vector.toarray())
import pandas as pd
df = pd.DataFrame(vector.toarray(),columns=vectorizer.get_feature_names())
print(df)
输出
Text After cleaning: [‘book funni’, ‘book bore’, ‘book long’, ‘bestsel novel’, ‘romant novel’]
bestsel book bore funni long novel romant
0 0.000000 0.556451 0.000000 0.830881 0.000000 0.000000 0.000000
1 0.000000 0.556451 0.830881 0.000000 0.000000 0.000000 0.000000
2 0.000000 0.556451 0.000000 0.000000 0.830881 0.000000 0.000000
3 0.778283 0.000000 0.000000 0.000000 0.000000 0.627914 0.000000
4 0.000000 0.000000 0.000000 0.000000 0.000000 0.627914 0.778283
谢谢。
做得好!
第四天任务
from gensim.models import Word2Vec
import numpy as np
from sklearn.decomposition import PCA
from matplotlib import pyplot
# 加载数据
filename = ‘ebook2.txt’ # Ebook – General Science by Bertha May Clark
file = open(filename, ‘rt’, encoding=’utf-8′)
text = file.read()
file.close()
import nltk
from nltk.stem import PorterStemmer
from nltk.corpus import stopwords
import string
table = str.maketrans(”, ”, string.punctuation)
stemmer = PorterStemmer() #初始化词干提取器
stop_words = set(stopwords.words(‘english’))
sentences = []
# 遍历文件中的每个句子
for i in nltk.sent_tokenize(text)
words = nltk.word_tokenize(i) #从文档中提取每个单词
words = [w.lower() for w in words] #转换为小写
words = [w.translate(table) for w in words] # 从每个单词中删除标点符号
words = [word for word in words if word.isalpha()]#删除非字母符号
words = [stemmer.stem(word) for word in words if word not in stop_words] #查找每个单词的词干
if words
sentences.append(words)
# 训练模型(sg = 1 表示 skip-gram),词频 >= 20 的最常见词
model = Word2Vec(sentences, vector_size=1000, min_count=20, sg=1)
# 打印与“steam”最相似的10个词
sims = model.wv.most_similar(‘steam’, topn=10)
print(sims)
# 拟合一个2D PCA模型到向量
X = np.asarray(model.wv.vectors) #model[model.wv.vocab]
pca = PCA(n_components=2)
result = pca.fit_transform(X)
# 创建投影的散点图
pyplot.scatter(result[:, 0], result[:, 1])
words = list(model.wv.index_to_key)
for i, word in enumerate(words)
pyplot.annotate(word, xy=(result[i, 0], result[i, 1]))
pyplot.show()
使用 skip-gram 方法的命令行输出:与“steam”相似的词
[(‘cool’, 0.9987481832504272), (‘melt’, 0.9984795451164246), (‘gram’, 0.9984639286994934), (‘boil’, 0.9984306693077087), (‘ice’, 0.9984259009361267), (‘condens’, 0.9983178973197937), (‘vapor’, 0.9982845783233643), (‘freez’, 0.9981751441955566), (‘cold’, 0.9980911016464233), (‘hot’, 0.9979501962661743)]
使用 CBOW 方法的输出:与“steam”相似的词
[(‘day’, 0.9999123215675354), (‘order’, 0.9999120831489563), (‘henc’, 0.999911904335022), (‘necessari’, 0.9999117851257324), (‘cold’, 0.9999102354049683), (‘make’, 0.9999101758003235), (‘well’, 0.9999092221260071), (‘made’, 0.9999086856842041), (‘requir’, 0.9999080300331116), (‘suppli’, 0.9999080300331116)]
观察
使用 skip-gram 方法的输出似乎好得多。
谢谢,
Vasana。
这里做得很好!继续。
第五天任务
import nltk
from nltk.stem import PorterStemmer
from nltk.corpus import stopwords
import string
from numpy import array
from keras.preprocessing.text import Tokenizer
from keras.preprocessing.sequence import pad_sequences
from gensim.models import KeyedVectors
from numpy import zeros
来自 keras.models import Sequential
from keras.layers import Dense
from keras.layers import Flatten
from keras.layers import Embedding
def clean_docs(sentences)
table = str.maketrans(”, ”, string.punctuation)
stemmer = PorterStemmer() #初始化词干提取器
stop_words = set(stopwords.words(‘english’))
clnDocs = []
for i in sentences
words = nltk.word_tokenize(i) #从文档中提取每个单词
words = [w.lower() for w in words] #转换为小写
words = [w.translate(table) for w in words] # 从每个单词中删除标点符号
words = [word for word in words if word.isalpha()]#删除非字母符号
words = [stemmer.stem(word) for word in words if word not in stop_words] #查找每个单词的词干
#words = ” “.join(words) ## 不需要连接
if words
clnDocs.append(words)
return clnDocs
# 定义文档
docs = [‘Water can convert from liquid to solid’,
‘Oxygen and Hydrogen are gases’,
‘Copper is a type of metalic element’,
‘Mercury is a known chemical element’,
‘Alcohol is an intoxicating chemical compound’,
‘This is a beautiful cloth’,
‘Drink a glass of water’,
‘The food is great!’,
‘it is very hot in this room’,
‘Alcohol is bad for health’]
# 定义类别标签(如果文档看起来是科学评论 = 1,否则 = 0)
labels = array([1,1,1,1,1,0,0,0,0,0])
# 清理文档
cleaned = clean_docs(docs)
# 使用分词器进行整数编码
t = Tokenizer()
t.fit_on_texts(cleaned)
vocab_size = len(t.word_index) + 1
# 整数编码文档
encoded_docs = t.texts_to_sequences(cleaned)
print(encoded_docs)
# 将文档填充到最大长度为 4 个单词
max_length = 4
padded_docs = pad_sequences(encoded_docs, maxlen=max_length, padding=’post’)
print(padded_docs)
# 使用 Gensim 加载预训练词向量(Ebook – ‘General Science’ by Bertha May Clark)
wv = KeyedVectors.load(“word2vec.wordvectors”, mmap=’r’)
preTrained = list(wv.index_to_key)
print(‘Loaded %s pre-trained word vectors.’ % len(preTrained))
# 为训练文档中的词创建一个权重矩阵
embedding_matrix = zeros((vocab_size, 300))
for word, i in t.word_index.items()
embedding_vector = wv[word]
if embedding_vector is not None
embedding_matrix[i] = embedding_vector
# 定义模型
model = Sequential()
e = Embedding(vocab_size, 300, weights=[embedding_matrix], input_length=4, trainable=False)
model.add(e)
model.add(Flatten())
model.add(Dense(1, activation=’sigmoid’))
# 编译模型
model.compile(optimizer=’adam’, loss=’binary_crossentropy’, metrics=[‘accuracy’])
# 总结模型
打印(model.summary())
# 训练模型
model.fit(padded_docs, labels, epochs=50, verbose=0)
# 评估模型
loss, accuracy = model.evaluate(padded_docs, labels, verbose=0)
print(‘Accuracy: %f’ % (accuracy*100))
输出
Model: “sequential”
_________________________________________________________________
层(类型) 输出形状 参数 #
=================================================================
embedding (Embedding) (None, 4, 300) 8400
_________________________________________________________________
flatten (Flatten) (None, 1200) 0
_________________________________________________________________
dense (Dense) (None, 1) 1201
=================================================================
Total params: 9,601
Trainable params: 1,201
Non-trainable params: 8,400
_________________________________________________________________
无
准确率:89.999998
观察结果
准确率达到 90% 左右。是否有可能达到 100%?如果以上代码有任何错误,请告知。
谢谢,
Vasana。
通常 100% 是过于乐观的。鉴于模型并不复杂,您可能对此感到满意。感谢分享。
谢谢
嗨 Json,我看到 gensim 已更新到 v5,其中包含一些破坏性更改。我尝试遵循说明来转换本教程中的代码,但我遇到了键错误,因此认为本教程中的示例需要其他更细微的更改才能使其正常工作。您是否有计划为 gensim 5 进行更新?
谢谢你
嗨 Stewart……我们目前不关注该主题。以下内容可能对您有所帮助:
https://www.machinelearningplus.com/nlp/gensim-tutorial/