BertTextClassifierPreprocessor
類別keras_hub.models.BertTextClassifierPreprocessor(
tokenizer, sequence_length=512, truncate="round_robin", **kwargs
)
一個 BERT 預處理層,用於 Tokenize 和打包輸入。
此預處理層將執行三件事
tokenizer
對任意數量的輸入片段進行 Tokenize。keras_hub.layers.MultiSegmentPacker
將輸入打包在一起,並帶有適當的 "[CLS]"
、"[SEP]"
和 "[PAD]"
標記。"token_ids"
、"segment_ids"
、"padding_mask"
,可以直接傳遞到 BERT 模型。此層可以直接與 tf.data.Dataset.map
一起使用,以預處理 (x, y, sample_weight)
格式的字串資料,該格式由 keras.Model.fit
使用。
Arguments(參數)
keras_hub.models.BertTokenizer
實例。sequence_length
的演算法。值可以是 round_robin
或 waterfall
"round_robin"
:可用空間以循環方式一次分配一個 token 給仍然需要 token 的輸入,直到達到限制。"waterfall"
:預算的分配使用「瀑布式」演算法完成,該演算法以從左到右的方式分配配額,並填充儲存桶,直到預算用完為止。它支援任意數量的片段。Call arguments(呼叫參數)
範例
直接在資料上呼叫層。
preprocessor = keras_hub.models.TextClassifierPreprocessor.from_preset(
"bert_base_en_uncased"
)
# Tokenize and pack a single sentence.
preprocessor("The quick brown fox jumped.")
# Tokenize a batch of single sentences.
preprocessor(["The quick brown fox jumped.", "Call me Ishmael."])
# Preprocess a batch of sentence pairs.
# When handling multiple sequences, always convert to tensors first!
first = tf.constant(["The quick brown fox jumped.", "Call me Ishmael."])
second = tf.constant(["The fox tripped.", "Oh look, a whale."])
preprocessor((first, second))
# Custom vocabulary.
vocab = ["[UNK]", "[CLS]", "[SEP]", "[PAD]", "[MASK]"]
vocab += ["The", "quick", "brown", "fox", "jumped", "."]
tokenizer = keras_hub.models.BertTokenizer(vocabulary=vocab)
preprocessor = keras_hub.models.BertTextClassifierPreprocessor(tokenizer)
preprocessor("The quick brown fox jumped.")
使用 tf.data.Dataset
進行映射。
preprocessor = keras_hub.models.TextClassifierPreprocessor.from_preset(
"bert_base_en_uncased"
)
first = tf.constant(["The quick brown fox jumped.", "Call me Ishmael."])
second = tf.constant(["The fox tripped.", "Oh look, a whale."])
label = tf.constant([1, 1])
# Map labeled single sentences.
ds = tf.data.Dataset.from_tensor_slices((first, label))
ds = ds.map(preprocessor, num_parallel_calls=tf.data.AUTOTUNE)
# Map unlabeled single sentences.
ds = tf.data.Dataset.from_tensor_slices(first)
ds = ds.map(preprocessor, num_parallel_calls=tf.data.AUTOTUNE)
# Map labeled sentence pairs.
ds = tf.data.Dataset.from_tensor_slices(((first, second), label))
ds = ds.map(preprocessor, num_parallel_calls=tf.data.AUTOTUNE)
# Map unlabeled 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
方法BertTextClassifierPreprocessor.from_preset(
preset, config_file="preprocessor.json", **kwargs
)
從模型預設實例化 keras_hub.models.Preprocessor
。
預設是一個配置、權重和其他檔案資產的目錄,用於儲存和載入預訓練模型。 preset
可以作為以下其中之一傳遞
'bert_base_en'
'kaggle://user/bert/keras/bert_base_en'
'hf://user/bert_base_en'
'./bert_base_en'
對於任何 Preprocessor
子類別,您可以執行 cls.presets.keys()
以列出該類別上所有可用的內建預設。
由於給定模型通常有多個預處理類別,因此此方法應在特定子類別上呼叫,例如 keras_hub.models.BertTextClassifierPreprocessor.from_preset()
。
Arguments(參數)
範例
# 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",
)
預設 | 參數 | 描述 |
---|---|---|
bert_tiny_en_uncased | 4.39M | 2 層 BERT 模型,其中所有輸入均為小寫。在 English Wikipedia + BooksCorpus 上訓練。 |
bert_tiny_en_uncased_sst2 | 4.39M | 在 SST-2 情感分析資料集上微調的 bert_tiny_en_uncased 主幹模型。 |
bert_small_en_uncased | 28.76M | 4 層 BERT 模型,其中所有輸入均為小寫。在 English Wikipedia + BooksCorpus 上訓練。 |
bert_medium_en_uncased | 41.37M | 8 層 BERT 模型,其中所有輸入均為小寫。在 English Wikipedia + BooksCorpus 上訓練。 |
bert_base_zh | 102.27M | 12 層 BERT 模型。在 Chinese Wikipedia 上訓練。 |
bert_base_en | 108.31M | 12 層 BERT 模型,其中保留大小寫。在 English Wikipedia + BooksCorpus 上訓練。 |
bert_base_en_uncased | 109.48M | 12 層 BERT 模型,其中所有輸入均為小寫。在 English Wikipedia + BooksCorpus 上訓練。 |
bert_base_multi | 177.85M | 12 層 BERT 模型,其中保留大小寫。在 104 種語言的 Wikipedia 上訓練 |
bert_large_en | 333.58M | 24 層 BERT 模型,其中保留大小寫。在 English Wikipedia + BooksCorpus 上訓練。 |
bert_large_en_uncased | 335.14M | 24 層 BERT 模型,其中所有輸入均為小寫。在 English Wikipedia + BooksCorpus 上訓練。 |
tokenizer
屬性keras_hub.models.BertTextClassifierPreprocessor.tokenizer
用於 Tokenize 字串的 tokenizer。