原文链接:http:///?p=8448文本生成是NLP的最新应用之一。深度学习技术已用于各种文本生成任务,例如写作诗歌,生成电影脚本甚至创作音乐。但是,在本文中,我们将看到一个非常简单的文本生成示例,其中给定输入的单词字符串,我们将预测下一个单词。我们将使用莎士比亚著名小说《麦克白》的原始文本,并根据给定的一系列输入单词来预测下一个单词。 完成本文之后,您将能够使用所选的数据集执行文本生成。 导入库和数据集第一步是导入执行本文中的脚本所需的库以及数据集。以下代码导入所需的库: import numpy as np 下一步是下载数据集。我们将使用Python的NLTK库下载数据集。 download('gutenberg') 您应该看到以下输出: \['austen-emma.txt', 'austen-persuasion.txt', 'austen-sense.txt', 'bible-kjv.txt', 'blake-poems.txt', 'bryant-stories.txt', 'burgess-busterbrown.txt', 'carroll-alice.txt', 'chesterton-ball.txt', 'chesterton-brown.txt', 'chesterton-thursday.txt', 'edgeworth-parents.txt', 'melville-moby_dick.txt', 'milton-paradise.txt', 'shakespeare-caesar.txt', 'shakespeare-hamlet.txt', 'shakespeare-macbeth.txt', 'whitman-leaves.txt'\] 该文件包含小说“ Macbeth”的原始文本。要从此文件读取文本,可以使用类中的 macbeth_text = corpus.gutenberg.raw('shakespeare-macbeth.txt') 让我们从数据集中输出前500个字符: print(macbeth_text\[:500\]) 这是输出: Actus Primus. Scoena Prima. 您会看到文本包含许多特殊字符和数字。下一步是清理数据集。 数据预处理要删除标点符号和特殊字符,我们将定义一个名为的函数 def preprocess_text(sen):
现在让我们清理文本,然后再次输出前500个字符: macbeth\_text = preprocess\_text(macbeth_text) 这是输出: the tragedie of macbeth by william shakespeare actus primus scoena prima thunder and lightning enter three witches when shall we three meet againe in thunder lightning or in raine when the hurley burley done when the battaile lost and wonne that will be ere the set of sunne where the place vpon the heath there to meet with macbeth come gray malkin all padock calls anon faire is foule and foule is faire houer through the fogge and filthie ayre exeunt scena secunda alarum within enter king malcom 将单词转换为数字深度学习模型基于统计算法。因此,为了使用深度学习模型,我们需要将单词转换为数字。 在本文中,我们将使用一种非常简单的方法,将单词转换为单个整数。在将单词转换为整数之前,我们需要将文本标记为单个单词。 以下脚本标记我们数据集中的文本,然后输出数据集中的单词总数以及数据集中的唯一单词总数: from nltk.tokenize import word_tokenize 输出这样: Total Words: 17250 我们的文字总共有17250个单词,其中3436个单词是唯一的。要将标记化的单词转换为数字,可以使用模块中的 看下面的脚本: from keras.preprocessing.text import Tokenizer 要访问包含单词及其相应索引的字典, vocab\_size = len(tokenizer.word\_index) + 1 如果您检查字典的长度,它将包含3436个单词,这是我们数据集中唯一单词的总数。 现在让我们从字典中输出第500个唯一单词及其整数值。 print(macbeth\_text\_words\[500\]) 这是输出: comparisons 修改数据形状LSTM接受3维格式的数据(样本数,时间步数,每个时间步的特征)。由于输出将是单个单词,因此输出的形状将是二维的(样本数,语料库中唯一词的数量)。 以下脚本修改了输入序列和相应输出的形状。 input_sequence = \[\] 在上面的脚本中,我们声明两个空列表 现在让我们输出 print(input_sequence\[0\]) 输出: \[1, 869, 4, 40, 60, 1358, 1359, 408, 1360, 1361, 409, 265, 2, 870, 31, 190, 291, 76, 36, 30, 190, 327, 128, 8, 265, 870, 83, 8, 1362, 76, 1, 1363, 1364, 86, 76, 1, 1365, 354, 2, 871, 5, 34, 14, 168, 1, 292, 4, 649, 77, 1, 220, 41, 1, 872, 53, 3, 327, 12, 40, 52, 1366, 1367, 25, 1368, 873, 328, 355, 9, 410, 2, 410, 9, 355, 1369, 356, 1, 1370, 2, 874, 169, 103, 127, 411, 357, 149, 31, 51, 1371, 329, 107, 12, 358, 412, 875, 1372, 51, 20, 170, 92, 9\] 让我们通过将序列中的整数除以最大整数值来归一化输入序列。以下脚本还将输出转换为二维格式。 以下脚本输出输入和相应输出的形状。 print("X shape:", X.shape) 输出: X shape: (17150, 100, 1) 训练模型下一步是训练我们的模型。关于应使用多少层和神经元来训练模型,没有硬性规定。 我们将创建三个LSTM层,每个层具有800个神经元。最终将添加具有1个神经元的密集层,来预测下一个单词的索引,如下所示: ... 由于输出单词可以是3436个唯一单词之一,因此我们的问题是多类分类问题,因此使用 Model: "sequential_1" 要训练模型,我们可以简单地使用该 model.fit(X, y, batch_size=64, epochs=10, verbose=1) 预测为了进行预测,我们将从 以下脚本随机选择一个整数序列,然后输出相应的单词序列: ... 对于本文中的脚本,以下顺序是随机选择的: amen when they did say god blesse vs lady consider it not so deepely mac but wherefore could not pronounce amen had most need of blessing and amen stuck in my throat lady these deeds must not be thought after these wayes so it will make vs mad macb me thought heard voyce cry sleep no more macbeth does murther sleepe the innocent sleepe sleepe that knits vp the rauel sleeue of care the death of each dayes life sore labors bath balme of hurt mindes great natures second course chiefe nourisher in life feast lady what doe you meane 接下来,我们将按照上述单词顺序输出接下来的100个单词: for i in range(100):
final_output = "" 这是最终输出: amen when they did say god blesse vs lady consider it not so deepely mac but wherefore could not pronounce amen had most need of blessing and amen stuck in my throat lady these deeds must not be thought after these wayes so it will make vs mad macb me thought heard voyce cry sleep no more macbeth does murther sleepe the innocent sleepe sleepe that knits vp the rauel sleeue of care the death of each dayes life sore labors bath balme of hurt mindes great natures second course chiefe nourisher in life feast lady what doe you meane and and and and and and and and and and and and and and and and and and and and and and and and and and and and and and and and and and and and and and and and and and and and and and and and and and and and and and and and and and and and and and and and and and and and and and and and and and and and and and and and and and and and and and and and and and and and and and and and and and and and 结论在本文中,我们看到了如何通过Python的Keras库使用深度学习来创建文本生成模型。 |
|