tf.keras.preprocessing.text.Tokenizer() 和 tfds.features.text.Tokenizer() 的比较
Comparison of tf.keras.preprocessing.text.Tokenizer() and tfds.features.text.Tokenizer()
作为背景,我最近越来越关注 NLP 和文本处理。我对计算机视觉更加熟悉。我完全理解令牌化的想法。
我的困惑源于 Tokenizer
class 的各种实现,这些实现可以在 Tensorflow
中找到 生态系统。
Tokenizer
class Tensorflow Datasets
(tfds
) 以及在 Tensorflow
中找到的一个:tfds.features.text.Tokenizer()
& tf.keras.preprocessing.text.Tokenizer()
分别.
我查看了源代码(链接如下)但无法收集到任何有用的见解
tfds
implementation
tf
implementation... line 18
links to the next link
- text data summarization function
这里的 tl;dr 问题是:您使用哪个库来做什么?一个图书馆相对于另一个图书馆有什么好处?
注意
我跟着Tensorflow In Practice Specialization as well as this tutorial。 TF in Practice Specialization使用tf.Keras.preprocessing.text.Tokenizer()
实现,文本加载教程使用tfds.features.text.Tokenizer()
有许多包已经开始提供自己的 API 来进行文本预处理,但是,每个包都有自己的细微差别。
tf.keras.preprocessing.text.Tokenizer()
由 Keras 实现,并作为高级 API 得到 Tensorflow 的支持。
tfds.features.text.Tokenizer()
由tensorflow自己开发维护。
两者都有自己的令牌编码方式。你可以用下面的例子来说明。
import tensorflow as tf
from tensorflow.keras.preprocessing.text import Tokenizer
import tensorflow_datasets as tfds
让我们获取一些示例数据并查看 API 的编码输出:
text_data = ["4. Kurt Betschart - Bruno Risi ( Switzerland ) 22",
"Israel approves Arafat 's flight to West Bank .",
"Moreau takes bronze medal as faster losing semifinalist .",
"W D L G / F G / A P",
"-- Helsinki newsroom +358 - 0 - 680 50 248",
"M'bishi Gas sets terms on 7-year straight ."]
首先,让我们看看 tf.keras.Tokenizer()
的结果:
tf_keras_tokenizer = Tokenizer()
tf_keras_tokenizer.fit_on_texts(text_data)
tf_keras_encoded = tf_keras_tokenizer.texts_to_sequences(text_data)
tf_keras_encoded = pad_sequences(tf_keras_encoded, padding="post")
对于我们输入数据中的第一句话,结果将是:
tf_keras_encoded[0]
array([2, 3, 4, 5, 6, 7, 8, 0], dtype=int32)
如果我们看一下单词到索引的映射。
tf_keras_tokenizer.index_word
{1: 'g',
2: '4',
3: 'kurt',
4: 'betschart',
5: 'bruno',
6: 'risi',
7: 'switzerland',
8: '22',
9: 'israel',
10: 'approves',
11: 'arafat',
12: "'s",
13: 'flight',
14: 'to',
15: 'west',
16: 'bank',
17: 'moreau',
18: 'takes',
19: 'bronze',
20: 'medal',
21: 'as',
22: 'faster',
23: 'losing',
24: 'semifinalist',
25: 'w',
26: 'd',
27: 'l',
28: 'f',
29: 'a',
30: 'p',
31: 'helsinki',
32: 'newsroom',
33: '358',
34: '0',
35: '680',
36: '50',
37: '248',
38: "m'bishi",
39: 'gas',
40: 'sets',
41: 'terms',
42: 'on',
43: '7',
44: 'year',
45: 'straight'}
现在让我们试试tfds.features.text.Tokenizer()
:
text_vocabulary_set = set()
for text in text_data:
text_tokens = tfds_tokenizer.tokenize(text)
text_vocabulary_set.update(text_tokens)
tfds_text_encoder = tfds.features.text.TokenTextEncoder(text_vocabulary_set, tokenizer=tfds_tokenizer)
对于我们输入数据中的第一句话,结果将是:
tfds_text_encoder.encode(text_data[0])
[35, 19, 44, 38, 32, 2, 14]
如果我们看一下单词到索引的映射(注意这里的索引是从0开始的)。
tfds_text_encoder._token_to_id
{'0': 0,
'22': 13,
'248': 17,
'358': 23,
'4': 34,
'50': 9,
'680': 6,
'7': 26,
'A': 19,
'Arafat': 39,
'Bank': 35,
'Betschart': 43,
'Bruno': 37,
'D': 15,
'F': 20,
'G': 28,
'Gas': 29,
'Helsinki': 38,
'Israel': 3,
'Kurt': 18,
'L': 44,
'M': 5,
'Moreau': 22,
'P': 10,
'Risi': 31,
'Switzerland': 1,
'W': 30,
'West': 33,
'approves': 4,
'as': 7,
'bishi': 2,
'bronze': 12,
'faster': 8,
'flight': 27,
'losing': 42,
'medal': 32,
'newsroom': 11,
'on': 25,
's': 24,
'semifinalist': 40,
'sets': 36,
'straight': 45,
'takes': 41,
'terms': 16,
'to': 14,
'year': 21}
您可以看到两个结果的编码差异以及 API 都提供了一些可以根据需要使用和更改的超参数。
作为背景,我最近越来越关注 NLP 和文本处理。我对计算机视觉更加熟悉。我完全理解令牌化的想法。
我的困惑源于 Tokenizer
class 的各种实现,这些实现可以在 Tensorflow
中找到 生态系统。
Tokenizer
class Tensorflow Datasets
(tfds
) 以及在 Tensorflow
中找到的一个:tfds.features.text.Tokenizer()
& tf.keras.preprocessing.text.Tokenizer()
分别.
我查看了源代码(链接如下)但无法收集到任何有用的见解
tfds
implementationtf
implementation... line18
links to the next link- text data summarization function
这里的 tl;dr 问题是:您使用哪个库来做什么?一个图书馆相对于另一个图书馆有什么好处?
注意
我跟着Tensorflow In Practice Specialization as well as this tutorial。 TF in Practice Specialization使用tf.Keras.preprocessing.text.Tokenizer()
实现,文本加载教程使用tfds.features.text.Tokenizer()
有许多包已经开始提供自己的 API 来进行文本预处理,但是,每个包都有自己的细微差别。
tf.keras.preprocessing.text.Tokenizer()
由 Keras 实现,并作为高级 API 得到 Tensorflow 的支持。
tfds.features.text.Tokenizer()
由tensorflow自己开发维护。
两者都有自己的令牌编码方式。你可以用下面的例子来说明。
import tensorflow as tf
from tensorflow.keras.preprocessing.text import Tokenizer
import tensorflow_datasets as tfds
让我们获取一些示例数据并查看 API 的编码输出:
text_data = ["4. Kurt Betschart - Bruno Risi ( Switzerland ) 22",
"Israel approves Arafat 's flight to West Bank .",
"Moreau takes bronze medal as faster losing semifinalist .",
"W D L G / F G / A P",
"-- Helsinki newsroom +358 - 0 - 680 50 248",
"M'bishi Gas sets terms on 7-year straight ."]
首先,让我们看看 tf.keras.Tokenizer()
的结果:
tf_keras_tokenizer = Tokenizer()
tf_keras_tokenizer.fit_on_texts(text_data)
tf_keras_encoded = tf_keras_tokenizer.texts_to_sequences(text_data)
tf_keras_encoded = pad_sequences(tf_keras_encoded, padding="post")
对于我们输入数据中的第一句话,结果将是:
tf_keras_encoded[0]
array([2, 3, 4, 5, 6, 7, 8, 0], dtype=int32)
如果我们看一下单词到索引的映射。
tf_keras_tokenizer.index_word
{1: 'g',
2: '4',
3: 'kurt',
4: 'betschart',
5: 'bruno',
6: 'risi',
7: 'switzerland',
8: '22',
9: 'israel',
10: 'approves',
11: 'arafat',
12: "'s",
13: 'flight',
14: 'to',
15: 'west',
16: 'bank',
17: 'moreau',
18: 'takes',
19: 'bronze',
20: 'medal',
21: 'as',
22: 'faster',
23: 'losing',
24: 'semifinalist',
25: 'w',
26: 'd',
27: 'l',
28: 'f',
29: 'a',
30: 'p',
31: 'helsinki',
32: 'newsroom',
33: '358',
34: '0',
35: '680',
36: '50',
37: '248',
38: "m'bishi",
39: 'gas',
40: 'sets',
41: 'terms',
42: 'on',
43: '7',
44: 'year',
45: 'straight'}
现在让我们试试tfds.features.text.Tokenizer()
:
text_vocabulary_set = set()
for text in text_data:
text_tokens = tfds_tokenizer.tokenize(text)
text_vocabulary_set.update(text_tokens)
tfds_text_encoder = tfds.features.text.TokenTextEncoder(text_vocabulary_set, tokenizer=tfds_tokenizer)
对于我们输入数据中的第一句话,结果将是:
tfds_text_encoder.encode(text_data[0])
[35, 19, 44, 38, 32, 2, 14]
如果我们看一下单词到索引的映射(注意这里的索引是从0开始的)。
tfds_text_encoder._token_to_id
{'0': 0,
'22': 13,
'248': 17,
'358': 23,
'4': 34,
'50': 9,
'680': 6,
'7': 26,
'A': 19,
'Arafat': 39,
'Bank': 35,
'Betschart': 43,
'Bruno': 37,
'D': 15,
'F': 20,
'G': 28,
'Gas': 29,
'Helsinki': 38,
'Israel': 3,
'Kurt': 18,
'L': 44,
'M': 5,
'Moreau': 22,
'P': 10,
'Risi': 31,
'Switzerland': 1,
'W': 30,
'West': 33,
'approves': 4,
'as': 7,
'bishi': 2,
'bronze': 12,
'faster': 8,
'flight': 27,
'losing': 42,
'medal': 32,
'newsroom': 11,
'on': 25,
's': 24,
'semifinalist': 40,
'sets': 36,
'straight': 45,
'takes': 41,
'terms': 16,
'to': 14,
'year': 21}
您可以看到两个结果的编码差异以及 API 都提供了一些可以根据需要使用和更改的超参数。