KerasHub: 预训练模型 / API 文档 / 分词器 / UnicodeCodepointTokenizer

UnicodeCodepointTokenizer

[源代码]

UnicodeCodepointTokenizer

keras_hub.tokenizers.UnicodeCodepointTokenizer(
    sequence_length=None,
    lowercase=True,
    normalization_form=None,
    errors="replace",
    replacement_char=65533,
    input_encoding="UTF-8",
    output_encoding="UTF-8",
    vocabulary_size=None,
    dtype="int32",
    **kwargs
)

一个 unicode 字符分词器层。

这个分词器是一个无词汇表的分词器,它将文本分词为 unicode 字符的码点。

分词器输出可以通过 sequence_length 参数进行填充和截断,也可以不截断。具体输出将取决于输入张量的秩。

如果输入是字符串的批次(秩 > 0):默认情况下,该层将输出一个 tf.RaggedTensor,其中输出的最后一个维度是不规则的。如果设置了 sequence_length,该层将输出一个稠密的 tf.Tensor,其中所有输入都已填充或截断到 sequence_length

如果输入是标量字符串(秩 == 0):默认情况下,该层将输出一个具有静态形状 [None] 的稠密 tf.Tensor。如果设置了 sequence_length,输出将是一个形状为 [sequence_length] 的稠密 tf.Tensor

输出数据类型可以通过 dtype 参数控制,该参数应该是一个整数类型("int16", "int32" 等)。

参数

  • lowercase:如果为 True,输入文本将在分词前首先转换为小写。
  • sequence_length:如果设置,输出将转换为一个稠密张量,并进行填充/截断,使所有输出的长度都为 sequence_length。
  • normalization_form:以下字符串值之一(None, 'NFC', 'NFKC', 'NFD', 'NFKD')。如果设置,将在分词之前将 unicode 规范化为给定的形式。
  • errors:'replace', 'remove', 'strict' 中的一个。指定 detokenize() 在遇到无效码点时的行为。值为 'strict' 会导致分词器在任何无效的输入格式上产生 InvalidArgument 错误。值为 'replace' 会导致分词器将输入中的任何无效格式替换为 replacement_char 码点。值为 'ignore' 会导致分词器跳过输入中的任何无效格式,并且不产生相应的输出字符。
  • replacement_char:用于替换无效码点的 unicode 码点。(U+FFFD) 是 65533。默认为 65533
  • input_encoding:("UTF-8", "UTF-16-BE", 或 "UTF-32-BE") 中的一个。输入文本的编码。默认为 "UTF-8"
  • output_encoding:("UTF-8", "UTF-16-BE", 或 "UTF-32-BE") 中的一个。输出文本的编码。默认为 "UTF-8"
  • vocabulary_size:设置词汇表 vocabulary_size,将所有码点钳制在 [0, vocabulary_size) 范围内。实际上,这将使 vocabulary_size - 1 的 ID 成为 OOV(未出现)值。

示例

基本用法。

>>> inputs = "Unicode Tokenizer"
>>> tokenizer = keras_hub.tokenizers.UnicodeCodepointTokenizer()
>>> outputs = tokenizer(inputs)
>>> np.array(outputs)
array([117, 110, 105,  99, 111, 100, 101,  32, 116, 111, 107, 101, 110,
    105, 122, 101, 114], dtype=int32)

不规则输出。

>>> inputs = ["पुस्तक", "کتاب"]
>>> tokenizer = keras_hub.tokenizers.UnicodeCodepointTokenizer()
>>> seq1, seq2 = tokenizer(inputs)
>>> np.array(seq1)
array([2346, 2369, 2360, 2381, 2340, 2325])
>>> np.array(seq2)
array([1705, 1578, 1575, 1576])

稠密输出。

>>> inputs = ["पुस्तक", "کتاب"]
>>> tokenizer = keras_hub.tokenizers.UnicodeCodepointTokenizer(
...     sequence_length=8)
>>> seq1, seq2 = tokenizer(inputs)
>>> np.array(seq1)
array([2346, 2369, 2360, 2381, 2340, 2325,    0,    0], dtype=int32)
>>> np.array(seq2)
array([1705, 1578, 1575, 1576,    0,    0,    0,    0], dtype=int32)

先分词,然后批处理以获得不规则输出。

>>> inputs = ["Book", "पुस्तक", "کتاب"]
>>> tokenizer = keras_hub.tokenizers.UnicodeCodepointTokenizer()
>>> ds = tf.data.Dataset.from_tensor_slices(inputs)
>>> ds = ds.map(tokenizer)
>>> ds = ds.apply(tf.data.experimental.dense_to_ragged_batch(3))
>>> ds.take(1).get_single_element()
<tf.RaggedTensor [[98, 111, 111, 107],
    [2346, 2369, 2360, 2381, 2340, 2325],
    [1705, 1578, 1575, 1576]]>

先批处理,然后分词以获得不规则输出。

>>> inputs = ["Book", "पुस्तक", "کتاب"]
>>> tokenizer = keras_hub.tokenizers.UnicodeCodepointTokenizer()
>>> ds = tf.data.Dataset.from_tensor_slices(inputs)
>>> ds = ds.batch(3).map(tokenizer)
>>> ds.take(1).get_single_element()
<tf.RaggedTensor [[98, 111, 111, 107],
    [2346, 2369, 2360, 2381, 2340, 2325],
    [1705, 1578, 1575, 1576]]>

先分词,然后批处理以获得稠密输出(提供了 sequence_length)。

>>> inputs = ["Book", "पुस्तक", "کتاب"]
>>> tokenizer = keras_hub.tokenizers.UnicodeCodepointTokenizer(
...     sequence_length=5)
>>> ds = tf.data.Dataset.from_tensor_slices(inputs)
>>> ds = ds.map(tokenizer)
>>> ds = ds.apply(tf.data.experimental.dense_to_ragged_batch(3))
>>> ds.take(1).get_single_element()
<tf.Tensor: shape=(3, 5), dtype=int32, numpy=
array([[  98,  111,  111,  107,    0],
    [2346, 2369, 2360, 2381, 2340],
    [1705, 1578, 1575, 1576,    0]], dtype=int32)>

先批处理,然后分词以获得稠密输出(提供了 sequence_length)。

>>> inputs = ["Book", "पुस्तक", "کتاب"]
>>> tokenizer = keras_hub.tokenizers.UnicodeCodepointTokenizer(
...     sequence_length=5)
>>> ds = tf.data.Dataset.from_tensor_slices(inputs)
>>> ds = ds.batch(3).map(tokenizer)
>>> ds.take(1).get_single_element()
<tf.Tensor: shape=(3, 5), dtype=int32, numpy=
array([[  98,  111,  111,  107,    0],
    [2346, 2369, 2360, 2381, 2340],
    [1705, 1578, 1575, 1576,    0]], dtype=int32)>

带截断的分词。

>>> inputs = ["I Like to Travel a Lot", "मैं किताबें पढ़ना पसंद करता हूं"]
>>> tokenizer = keras_hub.tokenizers.UnicodeCodepointTokenizer(
...     sequence_length=5)
>>> outputs = tokenizer(inputs)
>>> np.array(outputs)
array([[ 105,   32,  108,  105,  107],
       [2350, 2376, 2306,   32, 2325]], dtype=int32)

带 vocabulary_size 的分词。

>>> latin_ext_cutoff = 592
>>> tokenizer = keras_hub.tokenizers.UnicodeCodepointTokenizer(
...     vocabulary_size=latin_ext_cutoff)
>>> outputs = tokenizer("¿Cómo estás?")
>>> np.array(outputs)
array([191,  99, 243, 109, 111,  32, 101, 115, 116, 225, 115,  63],
      dtype=int32)
>>> outputs = tokenizer("आप कैसे हैं")
>>> np.array(outputs)
array([591, 591,  32, 591, 591, 591, 591,  32, 591, 591, 591],
      dtype=int32)

反分词。

>>> inputs = tf.constant([110, 105, 110, 106,  97], dtype="int32")
>>> tokenizer = keras_hub.tokenizers.UnicodeCodepointTokenizer()
>>> tokenizer.detokenize(inputs)
'ninja'

带填充的反分词。

>>> tokenizer = keras_hub.tokenizers.UnicodeCodepointTokenizer(
...     sequence_length=7)
>>> dataset = tf.data.Dataset.from_tensor_slices(["a b c", "b c", "a"])
>>> dataset = dataset.map(tokenizer)
>>> dataset.take(1).get_single_element()
<tf.Tensor: shape=(7,), dtype=int32,
    numpy=array([97, 32, 98, 32, 99,  0,  0], dtype=int32)>
>>> detokunbatched = dataset.map(tokenizer.detokenize)
>>> detokunbatched.take(1).get_single_element()
<tf.Tensor: shape=(), dtype=string, numpy=b'a b c'>

带无效字节的反分词。

>>> inputs = tf.constant([110, 105, 10000000, 110, 106,  97])
>>> tokenizer = keras_hub.tokenizers.UnicodeCodepointTokenizer(
...     errors="replace", replacement_char=88)
>>> tokenizer.detokenize(inputs)
'niXnja'

[源代码]

tokenize 方法

UnicodeCodepointTokenizer.tokenize(inputs)

将字符串输入张量转换为输出标记。

参数

  • inputs:输入张量,或输入张量的字典/列表/元组。
  • *args:额外的位置参数。
  • **kwargs:额外的关键字参数。

[源代码]

detokenize 方法

UnicodeCodepointTokenizer.detokenize(inputs)

将标记转换回字符串。

参数

  • inputs:输入张量,或输入张量的字典/列表/元组。
  • *args:额外的位置参数。
  • **kwargs:额外的关键字参数。

[源代码]

get_vocabulary 方法

UnicodeCodepointTokenizer.get_vocabulary()

获取分词器的词汇表,作为字符串术语列表。


[源代码]

vocabulary_size 方法

UnicodeCodepointTokenizer.vocabulary_size()

获取分词器词汇表的大小。

None 表示未提供词汇表大小。


[源代码]

token_to_id 方法

UnicodeCodepointTokenizer.token_to_id(token)

将字符串标记转换为整数 ID。


[源代码]

id_to_token 方法

UnicodeCodepointTokenizer.id_to_token(id)

将整数 ID 转换为字符串标记。