<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                # 如何使用 Keras 為深度學習準備文本數據 > 原文: [https://machinelearningmastery.com/prepare-text-data-deep-learning-keras/](https://machinelearningmastery.com/prepare-text-data-deep-learning-keras/) 您無法直接將原始文本提供給深度學習模型。 文本數據必須編碼為數字,以用作機器學習和深度學習模型的輸入或輸出。 Keras 深度學習庫提供了一些基本工具來幫助您準備文本數據。 在本教程中,您將了解如何使用 Keras 準備文本數據。 完成本教程后,您將了解: * 關于可用于快速準備文本數據的便捷方法。 * Tokenizer API,可以適用于訓練數據,用于編碼訓練,驗證和測試文檔。 * Tokenizer API 提供的 4 種不同文檔編碼方案的范圍。 讓我們開始吧。 ![How to Prepare Text Data for Deep Learning with Keras](img/173941fdd7b41eb463da5a301be0b140.jpg) 如何使用 Keras 為深度學習準備文本數據照片來自 [ActiveSteve](https://www.flickr.com/photos/activesteve/36054715916/) ,保留一些權利。 ## 教程概述 本教程分為 4 個部分;他們是: 1. 用 text_to_word_sequence 分隔單詞。 2. 用 one_hot 編碼。 3. 使用 hashing_trick 進行哈希編碼。 4. Tokenizer API ## 使用 text_to_word_sequence 分割單詞 使用文本時,第一步是將其拆分為單詞。 單詞稱為標記,將文本拆分為標記的過程稱為分詞。 Keras 提供 [text_to_word_sequence()函數](https://keras.io/preprocessing/text/#text_to_word_sequence),您可以使用它將文本拆分為單詞列表。 默認情況下,此功能自動執行以下三項操作: * 按空格拆分單詞(split =“”)。 * 過濾掉標點符號(filters ='!“#$%&amp;()* +, - 。/:;&lt; =&gt;?@ [\\] ^ _` {|}?\ t \ n')。 * 將文本轉換為小寫(lower = True)。 您可以通過將參數傳遞給函數來更改任何這些默認值。 下面是使用 text_to_word_sequence()函數將文檔(在本例中為簡單字符串)拆分為單詞列表的示例。 ```py from keras.preprocessing.text import text_to_word_sequence # define the document text = 'The quick brown fox jumped over the lazy dog.' # tokenize the document result = text_to_word_sequence(text) print(result) ``` 運行該示例將創建一個包含文檔中所有單詞的數組。打印單詞列表以供審閱。 ```py ['the', 'quick', 'brown', 'fox', 'jumped', 'over', 'the', 'lazy', 'dog'] ``` 這是一個很好的第一步,但在使用文本之前需要進一步的預處理。 ## 用 one_hot 編碼 將文檔表示為整數值序列是很流行的,其中文檔中的每個單詞都表示為唯一的整數。 Keras 提供 [one_hot()函數](https://keras.io/preprocessing/text/#one_hot),您可以使用它來一步對文本文檔進行分詞和整數編碼。該名稱表明它將創建文檔的單熱編碼,但事實并非如此。 相反,該函數是下一節中描述的 hashing_trick()函數的包裝器。該函數返回文檔的整數編碼版本。散列函數的使用意味著可能存在沖突,并且不是所有單詞都將被分配唯一的整數值。 與上一節中的 text_to_word_sequence()函數一樣,one_hot()函數將使文本小寫,過濾掉標點符號,并根據空格分割單詞。 除文本外,還必須指定詞匯量(總詞數)。如果您打算編碼包含其他單詞的其他文檔,則可以是文檔中的單詞總數或更多。詞匯表的大小定義了散列單詞的散列空間。理想情況下,這應該比詞匯量大一些百分比(可能是 25%),以最大限度地減少碰撞次數。默認情況下,使用'hash'函數,雖然我們將在下一節中看到,但是在直接調用 hashing_trick()函數時可以指定備用散列函數。 我們可以使用上一節中的 text_to_word_sequence()函數將文檔拆分為單詞,然后使用集合僅表示文檔中的唯一單詞。該集的大小可用于估計一個文檔的詞匯表大小。 例如: ```py from keras.preprocessing.text import text_to_word_sequence # define the document text = 'The quick brown fox jumped over the lazy dog.' # estimate the size of the vocabulary words = set(text_to_word_sequence(text)) vocab_size = len(words) print(vocab_size) ``` 我們可以將它與 one_hot()函數放在一起,并對文檔中的單詞進行熱編碼。下面列出了完整的示例。 詞匯大小增加三分之一,以最大限度地減少散列詞時的沖突。 ```py from keras.preprocessing.text import one_hot from keras.preprocessing.text import text_to_word_sequence # define the document text = 'The quick brown fox jumped over the lazy dog.' # estimate the size of the vocabulary words = set(text_to_word_sequence(text)) vocab_size = len(words) print(vocab_size) # integer encode the document result = one_hot(text, round(vocab_size*1.3)) print(result) ``` 首先運行該示例將詞匯表的大小打印為 8.然后將編碼的文檔打印為整數編碼的單詞數組。 ```py 8 [5, 9, 8, 7, 9, 1, 5, 3, 8] ``` ## 使用 hashing_trick 進行哈希編碼 整數和計數基本編碼的限制是它們必須保持單詞的詞匯表及其到整數的映射。 此方法的替代方法是使用單向散列函數將單詞轉換為整數。這避免了跟蹤詞匯表的需要,詞匯表更快并且需要更少的內存。 Keras 提供了 [hashing_trick()函數](https://keras.io/preprocessing/text/#hashing_trick),它分詞然后對文檔進行整數編碼,就像 one_hot()函數一樣。它提供了更大的靈活性,允許您將散列函數指定為“散列”(默認)或其他散列函數,例如內置的 md5 函數或您自己的函數。 下面是使用 md5 哈希函數對文檔進行整數編碼的示例。 ```py from keras.preprocessing.text import hashing_trick from keras.preprocessing.text import text_to_word_sequence # define the document text = 'The quick brown fox jumped over the lazy dog.' # estimate the size of the vocabulary words = set(text_to_word_sequence(text)) vocab_size = len(words) print(vocab_size) # integer encode the document result = hashing_trick(text, round(vocab_size*1.3), hash_function='md5') print(result) ``` 運行該示例將打印詞匯表的大小和整數編碼的文檔。 我們可以看到,使用不同的散列函數會導致單詞的一致但不同的整數作為上一節中的 one_hot()函數。 ```py 8 [6, 4, 1, 2, 7, 5, 6, 2, 6] ``` ## Tokenizer API 到目前為止,我們已經研究了使用 Keras 準備文本的一次性便捷方法。 Keras 提供了更復雜的 API,用于準備可以適合和重用以準備多個文本文檔的文本。這可能是大型項目的首選方法。 Keras 提供 [Tokenizer 類](https://keras.io/preprocessing/text/#tokenizer),用于為深度學習準備文本文檔。必須構造 Tokenizer,然后將其放在原始文本文檔或整數編碼的文本文檔上。 例如: ```py from keras.preprocessing.text import Tokenizer # define 5 documents docs = ['Well done!', 'Good work', 'Great effort', 'nice work', 'Excellent!'] # create the tokenizer t = Tokenizer() # fit the tokenizer on the documents t.fit_on_texts(docs) ``` 適用后,Tokenizer 提供了 4 個屬性,您可以使用這些屬性查詢有關文檔的內容: * **word_counts** :單詞及其計數字典。 * **word_docs** :一個單詞詞典和每個出現的文檔數量。 * **word_index** :單詞字典及其唯一分配的整數。 * **document_count** :用于適合 Tokenizer 的文檔總數的整數計數。 例如: ```py # summarize what was learned print(t.word_counts) print(t.document_count) print(t.word_index) print(t.word_docs) ``` 一旦 Tokenizer 適合訓練數據,它就可用于編碼訓練或測試數據集中的文檔。 Tokenizer 上的 texts_to_matrix()函數可用于為每個輸入提供每個文檔創建一個向量。向量的長度是詞匯表的總大小。 此函數提供了一套標準的詞袋模型文本編碼方案,可以通過函數的模式參數提供。 可用的模式包括: * ' _binary_ ':文檔中是否存在每個單詞。這是默認值。 * ' _count_ ':文檔中每個單詞的計數。 * ' _tfidf_ ':文本頻率 - 反向文檔頻率(TF-IDF)對文檔中每個單詞的評分。 * ' _freq_ ':每個單詞的頻率,作為每個文檔中單詞的比例。 我們可以將所有這些與一個有效的例子放在一起。 ```py from keras.preprocessing.text import Tokenizer # define 5 documents docs = ['Well done!', 'Good work', 'Great effort', 'nice work', 'Excellent!'] # create the tokenizer t = Tokenizer() # fit the tokenizer on the documents t.fit_on_texts(docs) # summarize what was learned print(t.word_counts) print(t.document_count) print(t.word_index) print(t.word_docs) # integer encode documents encoded_docs = t.texts_to_matrix(docs, mode='count') print(encoded_docs) ``` 運行該示例使 Tokenizer 與 5 個小文檔相匹配。打印適合標記符的詳細信息。然后使用字數對 5 個文檔進行編碼。 每個文檔被編碼為 9 元素向量,每個字具有一個位置,并且每個字位置具有所選擇的編碼方案值。在這種情況下,使用簡單的字數計數模式。 ```py OrderedDict([('well', 1), ('done', 1), ('good', 1), ('work', 2), ('great', 1), ('effort', 1), ('nice', 1), ('excellent', 1)]) 5 {'work': 1, 'effort': 6, 'done': 3, 'great': 5, 'good': 4, 'excellent': 8, 'well': 2, 'nice': 7} {'work': 2, 'effort': 1, 'done': 1, 'well': 1, 'good': 1, 'great': 1, 'excellent': 1, 'nice': 1} [[ 0\. 0\. 1\. 1\. 0\. 0\. 0\. 0\. 0.] [ 0\. 1\. 0\. 0\. 1\. 0\. 0\. 0\. 0.] [ 0\. 0\. 0\. 0\. 0\. 1\. 1\. 0\. 0.] [ 0\. 1\. 0\. 0\. 0\. 0\. 0\. 1\. 0.] [ 0\. 0\. 0\. 0\. 0\. 0\. 0\. 0\. 1.]] ``` ## 進一步閱讀 如果您要深入了解,本節將提供有關該主題的更多資源。 * [文本預處理 Keras API](https://keras.io/preprocessing/text/) * [text_to_word_sequence Keras API](https://keras.io/preprocessing/text/#text_to_word_sequence) * [one_hot Keras API](https://keras.io/preprocessing/text/#one_hot) * [hashing_trick Keras API](https://keras.io/preprocessing/text/#hashing_trick) * [Tokenizer Keras API](https://keras.io/preprocessing/text/#tokenizer) ## 摘要 在本教程中,您了解了如何使用 Keras API 為深度學習準備文本數據。 具體來說,你學到了: * 關于可用于快速準備文本數據的便捷方法。 * Tokenizer API,可以適用于訓練數據,用于編碼訓練,驗證和測試文檔。 * Tokenizer API 提供的 4 種不同文檔編碼方案的范圍。 你有任何問題嗎? 在下面的評論中提出您的問題,我會盡力回答。
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看