alex_bn_lee

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

统计

【504】NLP实战系列(一)—— one-hot encoding

参考:Text Preprocessing —— Tokenizer

参考:Preprocessing » 文本预处理

  对于 Embedding 层使用的输入,就是整数矩阵,并不是真正的 one-hot 向量,需要利用 Tokenizer 来实现。

1. Tokenizer 

1.1 语法

1
2
3
4
5
6
7
keras.preprocessing.text.Tokenizer(num_words=None,
                                   filters='!"#$%&()*+,-./:;<=>?@[\]^_`{|}~ ',
                                   lower=True,
                                   split=' ',
                                   char_level=False,
                                   oov_token=None,
                                   document_count=0)

  文本标记实用类。该类允许使用两种方法向量化一个文本语料库: 将每个文本转化为一个整数序列(每个整数都是词典中标记的索引); 或者将其转化为一个向量,其中每个标记的系数可以是二进制值、词频、TF-IDF权重等。

1.2 参数说明

  • num_words: 需要保留的最大词数,基于词频。只有最常出现的 num_words 词会被保留。
  • filters: 一个字符串,其中每个元素是一个将从文本中过滤掉的字符。默认值是所有标点符号,加上制表符和换行符,减去 ' 字符。
  • lower: 布尔值。是否将文本转换为小写。
  • split: 字符串。按该字符串切割文本。
  • char_level: 如果为 True,则每个字符都将被视为标记。
  • oov_token: 如果给出,它将被添加到 word_index 中,并用于在 text_to_sequence 调用期间替换词汇表外的单词。

  默认情况下,删除所有标点符号,将文本转换为空格分隔的单词序列(单词可能包含 ' 字符)。 这些序列然后被分割成标记列表。然后它们将被索引或向量化。

  0 是不会被分配给任何单词的保留索引。

1.3 类方法

  • fit_on_texts(texts)

    • texts:要用以训练的文本列表
  • texts_to_sequences(texts)

    • texts:待转为序列的文本列表

    • 返回值:序列的列表,列表中每个序列对应于一段输入文本

    • [[1, 2, 3, 4, 1, 5], [1, 6, 7, 8, 9]]
  • texts_to_sequences_generator(texts)

    • 本函数是texts_to_sequences的生成器函数版

    • texts:待转为序列的文本列表

    • 返回值:每次调用返回对应于一段输入文本的序列

  • texts_to_matrix(texts, mode):

    • texts:待向量化的文本列表

    • mode:‘binary’,‘count’,‘tfidf’,‘freq’之一,默认为‘binary’

    • 返回值:形如(len(texts), nb_words)的numpy array

    • [[0. 1. 1. ... 0. 0. 0.]
       [0. 1. 0. ... 0. 0. 0.]]
  • fit_on_sequences(sequences):

    • sequences:要用以训练的序列列表
  • sequences_to_matrix(sequences):

    • sequences:待向量化的序列列表

    • mode:‘binary’,‘count’,‘tfidf’,‘freq’之一,默认为‘binary’

    • 返回值:形如(len(sequences), nb_words)的numpy array

1.4 属性

  • word_counts:字典,将单词(字符串)映射为它们在训练期间出现的次数。仅在调用fit_on_texts之后设置。
    • OrderedDict([('the', 3), ('cat', 1), ('sat', 1), ('on', 1), ('mat', 1), ('dog', 1), ('ate', 1), ('my', 1), ('homework', 1)])
    • len(word_counts) 来计算单词的个数
  • word_docs: 字典,将单词(字符串)映射为它们在训练期间所出现的文档或文本的数量。仅在调用fit_on_texts之后设置。
  • word_index: 字典,将单词(字符串)映射为它们的排名或者索引。仅在调用fit_on_texts之后设置。
  • document_count: 整数。分词器被训练的文档(文本或者序列)数量。仅在调用fit_on_texts或fit_on_sequences之后设置。

1.5 举例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
from keras.preprocessing.text import Tokenizer
 
samples = ['The cat sat on the mat.', 'The dog ate my homework.']
 
# We create a tokenizer, configured to only take
# into account the top-1000 most common words
tokenizer = Tokenizer(num_words=1000)
# This builds the word index
tokenizer.fit_on_texts(samples)
 
print("word_counts: \n", tokenizer.word_counts)
print("\ntotal words: \n", len(tokenizer.word_counts))
 
# This turns strings into lists of integer indices.
sequences = tokenizer.texts_to_sequences(samples)
 
print("\nsequences:\n", sequences)
 
# You could also directly get the one-hot binary representations.
# Note that other vectorization modes than one-hot encoding are supported!
one_hot_results = tokenizer.texts_to_matrix(samples, mode='binary')
 
print("\none_hot_results:\n", one_hot_results)
 
# This is how you can recover the word index that was computed
word_index = tokenizer.word_index
 
print("\nword_index:\n", word_index)
print('\nFound %s unique tokens.' % len(word_index))

  outputs:

word_counts: 
 OrderedDict([('the', 3), ('cat', 1), ('sat', 1), ('on', 1), ('mat', 1), ('dog', 1), ('ate', 1), ('my', 1), ('homework', 1)])

total words: 
 9

sequences:
 [[1, 2, 3, 4, 1, 5], [1, 6, 7, 8, 9]]

one_hot_results:
 [[0. 1. 1. ... 0. 0. 0.]
 [0. 1. 0. ... 0. 0. 0.]]

word_index:
 {'the': 1, 'cat': 2, 'sat': 3, 'on': 4, 'mat': 5, 'dog': 6, 'ate': 7, 'my': 8, 'homework': 9}

Found 9 unique tokens.

posted on   McDelfino  阅读(184)  评论(0编辑  收藏  举报

编辑推荐:
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· .NET10 - 预览版1新功能体验(一)
历史上的今天:
2015-12-24 【194】Windows 上使用 wget
点击右上角即可分享
微信分享提示