Skip to main content

一个允许轻松创建 LSTM 文本预测数据集的 python 包

项目描述

预测性的

一个 python 包,可以轻松创建 LSTM 文本预测数据集。只需几行代码,您就可以开始在 PDF、.txt 文件和 UTF-8 托管的在线原始文本上训练您的网络!

🚩 目录

安装

安装此软件包的最简单方法是简单地使用 pip

pip install predictionary

你也可以很容易地克隆这个 repo。

进口

以下行从 Predictionary 包中导入所有类和方法。

from predictionary.source import load_raw_text
from predictionary.vata import process_text, word_to_int, int_to_word, make_data, make_sentences, encode_sentences
from predictionary.visualize import make_prevelence_dict, plot_top

让我们来看看这些功能,一次一种方法。

资源

  • 加载原始文本

    Source 类只有一种方法,“load_raw_text”。此方法用于从 Web、PDF 或本地文本文件中提取原始文本数据。它还可以合并数据,或返回分离数据的列表。

    论据

    • URL(s) - 要传递的第一个参数是您要从中提取的文件的 URL 或路径列表
    • 方法- 此变量对应于您的源的类型,例如“web”表示传递的第一个值将是 URL 列表,“text”将传递本地 .txt 文件路径,“pdf”将传递传递了本地 .pdf 文件的路径。
    • merge_data - 要设置的最后一个变量是 merge_data,如果设置为“True”或未实例化,则将合并传递的列表中的所有源并在最后返回单个字符串。如果设置为“False”,则返回长度为 len(URL(s)) 的列表,其中包含每个源的数据

    注意使用“load_raw_text”时,请务必在“method”变量中输入正确的标识符。

    • 网络

    Web 值允许我们从托管的 .txt 文件中提取数据,例如古腾堡项目的文件。

    text = load_raw_text(["https://www.gutenberg.org/files/76/76-0.txt", "https://www.gutenberg.org/files/6130/6130-0.txt"], method="web", merge_data=True)
    

    在上面的示例中,我们从托管在项目 gutenburg 的两本书中获取数据,并告诉包合并这两本书的数据。该命令将返回一个字符串,其中包含要处理的数据。如果我们改为使用“merge_data=False”,我们将返回一个包含 2 个此类字符串的列表,然后我们必须单独处理这些字符串。

    • 文本

    此源方法允许用户从其本地 .txt 文件中提取数据。

    text = load_raw_text([r'C:\Users\Chuggy\Documents\hello.txt', r'C:\Users\Chuggy\Documents\hello1.txt'], method="text", merge_data=False)
    

    在此示例中,我们从两个本地文件“hello.txt”和“hello1.txt”中提取。我们还通过指定“merge_data=False”告诉包返回 2 个文件数据的 2 个元素列表。

    • PDF格式

    最终的源方法允许用户直接从 PDF 中提取数据。

    text = load_raw_text([r'C:\Users\Chuggy\Downloads\flashboys.pdf'], method="pdf")
    

    这里我们使用默认值进行合并,所以如果我们指定了 2 个不同的文件,文件就会被合并,但是我们这里只有一个,所以我们很好。与往常一样,我们将输入法指定为“pdf”,在这种情况下,我们会返回一个包含来自我们源的原始文本的字符串。

数据

  • 进程文本

    此方法是包的主要文本“清洁器”。它将接收“Source”类返回的原始文本数据,并将其转换为已清除(读作“分离”)标点符号的字符或单词。

    论据

    • text - 要传递的第一个参数是从“Source”类返回的原始文本字符串。
    • split_by_words - 此参数将确定包是按单词还是按字符拆分文本。
    • keep_spaces - 此方法中的最后一个参数确定输入数据是否应包含空格,或者只是单词/字符。

  • split_by_words = 真

    示例用法:

    fully_processed_text = process_text(text, split_by_words=True)
    

    回报:

    ['the', ' ', 'project', ' ', 'gutenberg', ' ', 'ebook', ' ', 'of', ' ', 'adventures', ' ', 'of', ' ', 'huckleberry', ...]
    

    上面的示例是我们将原始文本从源中拆分为单词的示例。

  • split_by_words = 假

    示例用法:

    fully_processed_text = process_text(text, split_by_words=False)
    

    回报:

    ['t', 'h', 'e', ' ', 'p', 'r', 'o', 'j', 'e', 'c', 't', ' ', 'g', 'u', 't', 'e', 'n', ...]
    

    在此示例中,我们按字符拆分原始文本。

  • split_by_words = True,keep_spaces=False

    示例用法:

    fully_processed_text = process_text(text, split_by_words=False)
    

    回报:

    ['the', 'project', 'gutenberg', 'ebook', 'of', 'adventures', 'of', 'huckleberry', ...]
    

    在这个例子中,我们只想要没有空格的单词。

  • word_to_int

    此方法用于为我们的数据流入 LSTM 架构所需的 word_to_int 过程创建字典。

    论据

    • fully_processed_text - 接受由 process_text 方法返回的完全处理的文本。

    示例用法:

    word_to_int_dictionary = word_to_int(fully_processed_text)
    

    回报:

    {'': 0, '\r': 1, '\n': 2, ' ': 3, '!': 4, '#': 5, '$': 6, '%': 7, ...}
    
  • int_to_word

    此方法用于从 word_to_int 方法创建一个逆字典,用于将模型的 one-hot 数组输出的 argmax 转换回字符/单词。

    论据

    • word_to_int_dictionary - 从 word_to_int 方法中获取 word_to_int_dictionary 并将其反转。

    示例用法:

    int_to_word_dictionary = int_to_word(word_to_int_dictionary)
    

    回报:

    {0: '', 1: '\r', 2: '\n', 3: ' ', 4: '!', 5: '#', 6: '$', 7: '%', ...}
    
  • make_data

    这个方法是这个 Data 类的主要工作。它用于从处理文本方法中获取处理后的文本,并将其转化为完全可训练的 LSTM 文本预测数据集。

    论据

    • fully_processed_text - 从 process_text 方法传入完全处理的文本。
    • int_to_word_dictionary - 从 int_to_word 字典创建方法传入 int_to_word_dictionary。
    • sequence_length - 您希望每个训练示例的字符/单词的长度。
    • X_one_hot - 一个布尔值,用于确定您是否希望以整数或 one-hot 数组的形式将训练序列传递到网络。
    • Y_one_hot - 一个布尔值,用于确定您是否希望以整数或 one-hot 数组的形式将目标数据传递到网络。
    • split_train_test - 一个布尔值,如果设置为 True,该方法将传回拆分为 X_train、Y_train、X_test、Y_test 形式的数据,或者如果设置为 False,将传回 X、Y 形式的数据。
    • test_train_split_ratio - 一个浮点值,允许用户设置测试数据与训练数据的比率。
    • save_locally - 一个布尔值,如果设置为 True,将在本地以 .npy 格式保存创建的数组。

    示例用法

    X_train, X_test, y_train, y_test = make_data(fully_processed_text, int_to_word_dictionary, sequence_length=68, X_one_hot=True, split_train_test=True, test_train_split_ratio=0.33, save_locally=False)
    

    回报:

    What it Actually Returns:
    X_train, X_test, y_train, y_test
    
    **The Features of What it Returns***:
    
    Number of Train Examples:  385072
    Number of Test Examples:  189663
    
    X_train.shape:
    (385072, 69, 60)
    
    X_train[0]:
    [[0. 0. 0. ... 0. 0. 0.]
     [0. 0. 0. ... 0. 0. 0.]
     [0. 0. 0. ... 0. 0. 0.]
     ...
     [0. 0. 0. ... 0. 0. 0.]
     [0. 0. 0. ... 0. 0. 0.]
     [0. 0. 0. ... 0. 0. 0.]]
    
    y_train.shape:
    (385072, 60)
    
    y_train[0]:
    [0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 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. 0. 0. 0. 0.
     0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
    

    在上面的示例中,我们创建了每个序列包含 68 个字符/单词的数据,采用 one-hot 编码,有 33% 的测试来训练数据配给,并且不保存到我们的本地机器。

    示例用法

    X, y = make_data(fully_processed_text, int_to_word_dictionary, sequence_length=69, X_one_hot=False, split_train_test=False, save_locally=True))
    

    回报:

    What it Actually Returns:
    X, y
    
    **The Features of What it Returns***:
    
    Number of Train Examples:  574735
    Number of Test Examples: 0
    
    X.shape:
    (574735, 69, 1)
    
    X[0]:
    [[53]
     [41]
     [38]
     [ 3]
     [49]
     [51]
     [48]
     [43]
     [38]
      .
      .
      .
         ]
    
    y_train.shape:
    (574735, 1)
    
    y_train[0]:
    [128]
    
    

    在上面的例子中,我们创建了每个序列包含 69 个字符/单词的数据,不是 one-hot 编码的,对于 Y 不是 one-hot 编码的,不分成测试和训练数据集,并保存到我们的本地机器。

  • 造句

    这个方法在这个包中有点事后的想法,是应朋友的要求而包含的。它通过像 [. !?]

    论据

    • fully_processed_text - 接受由 process_text 方法返回的完全处理的文本。

    示例用法

    train sentences = make_sentences(fully_processed_text)
    

    回报:

    [' ', 'i', ' ', 'n', 'e', 'v', 'e', 'r', ' ', 's', 'e', 'e', ' ', 's', 'u', 'c', 'h', ' ', 'a', ' ', 's', 'o', 'n', '.']
    
  • 编码句子

    此方法只是 make_sentences 方法的编码器。使用 int_to_word_dictionary 将它们转换为每个单词的整数表示。

    论据

    • fully_processed_text - 接受由 process_text 方法返回的完全处理的文本。
    • int_to_word_dictionary - 接收由 int_to_word 方法返回的 int_to_word_dictionary。

    示例用法

    encoded = encode_sentences(sentences, int_to_word_dictionary)
    

    回报:

    [3, 42, 3, 47, 38, 55, 38, 51, 3, 52, 38, 38, 3, 52, 54, 36, 41, 3, 34, 3, 52, 48, 47, 15]
    

    请注意,Data 类中的最后两种方法留给用户来创建目标数据,因为有许多不同的方法可以使用 LSTM 对句子数据进行训练。

可视化

  • make_prevelence_dict

    这是一个快速的方法,它接受完全处理的文本中的所有字符/单词,并返回一个基于流行度降序排序的字典。这是一个很好的工具,可以可视化您的数据是什么样子并捕捉可能的过度拟合原因。

    论据

    • fully_processed_text - 接受由 process_text 方法返回的完全处理的文本。

    示例用法

    prevelence_dictionary = make_prevelence_dict(fully_processed_text)
    

    回报:

    [(' ', 104548), ('e', 49605), ('t', 42825), ('o', 37018), ('a', 36947), ('n', 33119), ('i', 28636), ('h', 26660), ('s', 25503), ('d', 23906), ('r', 20554), ('l', 17637), ('u', 14114), ('w', 13419), ('g', 10906), ...]
    
  • plot_top

    现在我们有了排序流行字典,我们可以制作一个简单的条形图来更好地可视化字典。

    论据

    • number - 为要显示的前“n”个字符/单词输入一个数字。
    • prevelence_dictionary - 从 make_prevelence_dict 方法中获取 prevelence_dictionary。

    示例用法

    plot_top(10, prevelence_dictionary)
    

    回报:

结果

对于以下示例,我使用 Predictionary 从 5 本书的古腾堡项目中提取数据。《白鲸记》、《爱丽丝梦游仙境》、《哈克贝利·芬》、《杰基尔博士和海德先生的奇案》以及《伊利亚特》。这些书总共有超过 3,000,000 个序列示例供网络学习。我使用了具有超过 200,000 个权重的 2 层 LSTM 和以下架构......

Model: "model_3"
__________________________________________________________________________________________________
Layer (type)                    Output Shape         Param #     Connected to                     
==================================================================================================
input_4 (InputLayer)            (None, 50, 1)        0                                            
__________________________________________________________________________________________________
permute_4 (Permute)             (None, 1, 50)        0           input_4[0][0]                    
__________________________________________________________________________________________________
dense_9 (Dense)                 (None, 1, 50)        2550        permute_4[0][0]                  
__________________________________________________________________________________________________
attention_prob (Permute)        (None, 50, 1)        0           dense_9[0][0]                    
__________________________________________________________________________________________________
multiply_4 (Multiply)           (None, 50, 1)        0           input_4[0][0]                    
                                                                 attention_prob[0][0]             
__________________________________________________________________________________________________
lstm_7 (LSTM)                   (None, 50, 128)      66560       multiply_4[0][0]                 
__________________________________________________________________________________________________
dropout_7 (Dropout)             (None, 50, 128)      0           lstm_7[0][0]                     
__________________________________________________________________________________________________
lstm_8 (LSTM)                   (None, 128)          131584      dropout_7[0][0]                  
__________________________________________________________________________________________________
dropout_8 (Dropout)             (None, 128)          0           lstm_8[0][0]                     
__________________________________________________________________________________________________
dense_10 (Dense)                (None, 100)          12900       dropout_8[0][0]                  
__________________________________________________________________________________________________
dense_11 (Dense)                (None, 70)           7070        dense_10[0][0]                   
==================================================================================================
Total params: 220,664
Trainable params: 220,664
Non-trainable params: 0

这个网络在我的机器上训练非常慢,所以我将 epoch 的数量限制在 35 个左右,结果是 40 小时左右。

这里有一些输入种子,以及计算机的预测......

输入:

"The old woman talked surgeon's astronomy in the back country, and by j..."

输出:

"...ove the soul the last the milking of the contrast in the coursers he dropped it out the new charms"

输入:

"The old woman talked surgeon's astronomy in the back country, and by j..."

输出:

"...ove without paused great achilles lies and i show one murmer all brightly enough"

正如您所看到的,无论想象如何,都不是很好或连贯,但对于一次只能看到 50 个字符并且具有 > 3M 训练示例的模型来说还不错。

📠– 许可证

麻省理工学院许可证

版权所有 (c) 2019 奥利弗·马蒂亚斯

特此免费授予任何人获得本软件和相关文档文件(“软件”)的副本,以不受限制地处理本软件,包括但不限于使用、复制、修改、合并的权利、发布、分发、再许可和/或出售本软件的副本,并允许向其提供本软件的人这样做,但须符合以下条件:

上述版权声明和本许可声明应包含在本软件的所有副本或大部分内容中。

本软件按“原样”提供,不提供任何形式的明示或暗示保证,包括但不限于适销性、特定用途适用性和非侵权保证。在任何情况下,作者或版权持有人均不对任何索赔、损害或其他责任承担任何责任,无论是在合同、侵权或其他方面,由本软件或本软件的使用或其他交易引起或与之相关。软件。

项目详情


下载文件

下载适用于您平台的文件。如果您不确定要选择哪个,请了解有关安装包的更多信息。

源分布

predictionary-0.1.5.tar.gz (17.3 kB 图哈希)

已上传 source