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

UnicodeCodepointTokenizer

[source]

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,其中输出的最后一个维度是 Ragged。如果设置了 sequence_length,该层将输出一个密集的 tf.Tensor,其中所有输入都已填充或截断为 sequence_length

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

输出 dtype 可以通过 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)

Ragged 输出。

>>> 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)

分词,然后批量处理以获得 Ragged 输出。

>>> 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]]>

批量处理,然后分词以获得 Ragged 输出。

>>> 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'

[source]

tokenize 方法

UnicodeCodepointTokenizer.tokenize(inputs)

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

参数

  • inputs:输入张量,或输入张量的 dict/list/tuple。
  • *args:其他位置参数。
  • **kwargs:其他关键字参数。

[source]

detokenize 方法

UnicodeCodepointTokenizer.detokenize(inputs)

将标记转换回字符串。

参数

  • inputs:输入张量,或输入张量的 dict/list/tuple。
  • *args:其他位置参数。
  • **kwargs:其他关键字参数。

[source]

get_vocabulary 方法

UnicodeCodepointTokenizer.get_vocabulary()

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


[source]

vocabulary_size 方法

UnicodeCodepointTokenizer.vocabulary_size()

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

None 表示未提供词汇表大小


[source]

token_to_id 方法

UnicodeCodepointTokenizer.token_to_id(token)

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


[source]

id_to_token 方法

UnicodeCodepointTokenizer.id_to_token(id)

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