KerasHub: 预训练模型 / API 文档 / 模型架构 / FNet / FNetMaskedLMPreprocessor 层

FNetMaskedLMPreprocessor 层

[源代码]

FNetMaskedLMPreprocessor

keras_hub.models.FNetMaskedLMPreprocessor(
    tokenizer,
    sequence_length=512,
    truncate="round_robin",
    mask_selection_rate=0.15,
    mask_selection_length=96,
    mask_token_rate=0.8,
    random_token_rate=0.1,
    **kwargs
)

用于掩码语言建模任务的 FNet 预处理层。

此预处理层将为掩码语言建模任务准备输入。它主要用于配合 keras_hub.models.FNetMaskedLM 任务模型使用。预处理将分多个步骤进行。

  1. 使用 tokenizer 对任意数量的输入段进行分词。
  2. 将输入与适当的 "<s>", "</s>""<pad>" 标记打包在一起,即,在整个序列的开头添加一个 "<s>",在每个段之间添加 "</s></s>",并在整个序列的末尾添加一个 "</s>"
  3. 随机选择非特殊标记进行掩码,由 mask_selection_rate 控制。
  4. 构建一个适合用于与 keras_hub.models.FNetMaskedLM 任务模型一起训练的 (x, y, sample_weight) 元组。

参数

  • tokenizer: 一个 keras_hub.models.FNetTokenizer 实例。
  • sequence_length: 打包输入的长度。
  • mask_selection_rate: 输入标记将被动态掩码的概率。
  • mask_selection_length: 层支持的掩码标记的最大数量。
  • mask_token_rate: float。mask_token_rate 必须介于 0 和 1 之间,表示将 mask_token 替换为选定进行掩码的标记的频率。默认为 0.8
  • random_token_rate: float。random_token_rate 必须介于 0 和 1 之间,表示将随机标记替换为选定进行掩码的标记的频率。注意:mask_token_rate + random_token_rate <= 1,对于 (1 - mask_token_rate - random_token_rate) 的情况,标记将不会改变。默认为 0.1
  • truncate: string。用于截断一批段列表以使其符合 sequence_length 的算法。该值可以是 round_robinwaterfall
    • "round_robin": 可用空间以轮询方式一次分配一个标记给仍然需要空间的输入,直到达到限制。
    • "waterfall": 预算分配采用“瀑布流”算法完成,该算法从左到右分配配额,并填满桶直到预算耗尽。它支持任意数量的段。

示例

直接在数据上调用层。

# Load the preprocessor from a preset.
preprocessor = keras_hub.models.FNetMaskedLMPreprocessor.from_preset(
    "f_net_base_en"
)

# Tokenize and mask a single sentence.
preprocessor("The quick brown fox jumped.")

# Tokenize and mask a batch of single sentences.
preprocessor(["The quick brown fox jumped.", "Call me Ishmael."])

# Tokenize and mask sentence pairs.
# In this case, always convert input to tensors before calling the layer.
first = tf.constant(["The quick brown fox jumped.", "Call me Ishmael."])
second = tf.constant(["The fox tripped.", "Oh look, a whale."])
preprocessor((first, second))

tf.data.Dataset 映射。

preprocessor = keras_hub.models.FNetMaskedLMPreprocessor.from_preset(
    "f_net_base_en"
)

first = tf.constant(["The quick brown fox jumped.", "Call me Ishmael."])
second = tf.constant(["The fox tripped.", "Oh look, a whale."])

# Map single sentences.
ds = tf.data.Dataset.from_tensor_slices(first)
ds = ds.map(preprocessor, num_parallel_calls=tf.data.AUTOTUNE)

# Alternatively, you can create a preprocessor from your own vocabulary.
vocab_data = tf.data.Dataset.from_tensor_slices(
    ["the quick brown fox", "the earth is round"]
)

# Map sentence pairs.
ds = tf.data.Dataset.from_tensor_slices((first, second))
# Watch out for tf.data's default unpacking of tuples here!
# Best to invoke the `preprocessor` directly in this case.
ds = ds.map(
    lambda first, second: preprocessor(x=(first, second)),
    num_parallel_calls=tf.data.AUTOTUNE,
)

[源代码]

from_preset 方法

FNetMaskedLMPreprocessor.from_preset(
    preset, config_file="preprocessor.json", **kwargs
)

从模型预设实例化一个 keras_hub.models.Preprocessor

预设是一个包含配置、权重和其他文件资产的目录,用于保存和加载预训练模型。preset 可以作为以下之一传入:

  1. 一个内置预设标识符,例如 'bert_base_en'
  2. 一个 Kaggle Models 句柄,例如 'kaggle://user/bert/keras/bert_base_en'
  3. 一个 Hugging Face 句柄,例如 'hf://user/bert_base_en'
  4. 本地预设目录的路径,例如 './bert_base_en'

对于任何 Preprocessor 子类,您可以运行 cls.presets.keys() 来列出类上所有可用的内置预设。

由于一个给定模型通常有多个预处理类,应在特定子类上调用此方法,例如 keras_hub.models.BertTextClassifierPreprocessor.from_preset()

参数

  • preset: string。内置预设标识符、Kaggle Models 句柄、Hugging Face 句柄或本地目录的路径。

示例

# Load a preprocessor for Gemma generation.
preprocessor = keras_hub.models.CausalLMPreprocessor.from_preset(
    "gemma_2b_en",
)

# Load a preprocessor for Bert classification.
preprocessor = keras_hub.models.TextClassifierPreprocessor.from_preset(
    "bert_base_en",
)
预设 参数 描述
f_net_base_en 82.86M 12层 FNet 模型,保留大小写。在 C4 数据集上训练。
f_net_large_en 236.95M 24层 FNet 模型,保留大小写。在 C4 数据集上训练。

tokenizer 属性

keras_hub.models.FNetMaskedLMPreprocessor.tokenizer

用于对字符串进行分词的分词器。