Keras 3 API 文件 / KerasNLP / 預先訓練的模型 / Albert / AlbertTextClassifierPreprocessor 圖層

AlbertTextClassifierPreprocessor 圖層

[來源]

AlbertTextClassifierPreprocessor 類別

keras_nlp.models.AlbertTextClassifierPreprocessor(
    tokenizer, sequence_length=512, truncate="round_robin", **kwargs
)

用於對輸入進行標記化和打包的 ALBERT 預處理圖層。

此預處理圖層將執行三項操作

  • 使用 tokenizer 對任意數量的輸入區段進行標記化。
  • 使用 keras_nlp.layers.MultiSegmentPacker,並使用適當的 "[CLS]""[SEP]""<pad>" 標記,將輸入打包在一起。
  • 建構一個字典,其中包含 "token_ids""segment_ids""padding_mask" 鍵,可以直接傳遞至 keras_nlp.models.AlbertBackbone

此圖層可以直接與 tf.data.Dataset.map 搭配使用,以預處理 keras.Model.fit 使用的 (x, y, sample_weight) 格式的字串資料。

此圖層的呼叫方法接受三個參數:xysample_weightx 可以是表示單個區段的 Python 字串或張量、表示一批單個區段的 Python 字串清單,或是表示要打包在一起的多個區段的張量清單。ysample_weight 都是可選的,可以有任何格式,並且將按原樣傳遞。

在使用 tf.data 對未標記的字串區段元組進行映射時,應特別注意。 tf.data.Dataset.map 會將此元組直接解壓縮到此圖層的呼叫參數中,而不是將所有參數都轉發到 x。為了處理這種情況,建議明確呼叫圖層,例如 ds.map(lambda seg1, seg2: preprocessor(x=(seg1, seg2)))

參數

  • tokenizerkeras_nlp.models.AlbertTokenizer 的執行個體。
  • sequence_length:已打包輸入的長度。
  • truncate:字串。將一組批次區隔截斷以符合 sequence_length 的演算法。此值可以是 round_robinwaterfall
    • "round_robin":可用空間會以循環方式一次分配一個權杖給仍然需要的輸入,直到達到限制為止。
    • "waterfall":預算的分配是使用「瀑布」演算法完成的,該演算法以從左到右的方式分配配額,並填滿儲存貯體,直到預算用盡為止。它支援任意數量的區隔。

範例

直接呼叫資料上的層。

preprocessor = keras_nlp.models.TextClassifierPreprocessor.from_preset(
    "albert_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.
bytes_io = io.BytesIO()
ds = tf.data.Dataset.from_tensor_slices(["The quick brown fox jumped."])
sentencepiece.SentencePieceTrainer.train(
    sentence_iterator=ds.as_numpy_iterator(),
    model_writer=bytes_io,
    vocab_size=10,
    model_type="WORD",
    pad_id=0,
    unk_id=1,
    bos_id=2,
    eos_id=3,
    pad_piece="<pad>",
    unk_piece="<unk>",
    bos_piece="[CLS]",
    eos_piece="[SEP]",
    user_defined_symbols="[MASK]",
)
tokenizer = keras_nlp.models.AlbertTokenizer(
    proto=bytes_io.getvalue(),
)
preprocessor = keras_nlp.models.AlbertTextClassifierPreprocessor(tokenizer)
preprocessor("The quick brown fox jumped.")

使用 tf.data.Dataset 進行映射。

preprocessor = keras_nlp.models.TextClassifierPreprocessor.from_preset(
    "albert_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 方法

AlbertTextClassifierPreprocessor.from_preset(preset, **kwargs)

從模型預設集實例化 keras_nlp.models.Preprocessor

預設集是設定檔、權重和其他檔案資產的目錄,用於儲存和載入預先訓練的模型。preset 可以作為以下其中一項傳遞

  1. 內建的預設集識別碼,例如 'bert_base_en'
  2. Kaggle 模型控點,例如 'kaggle://user/bert/keras/bert_base_en'
  3. Hugging Face 控點,例如 'hf://user/bert_base_en'
  4. 本機預設集目錄的路徑,例如 './bert_base_en'

對於任何 Preprocessor 子類別,您可以執行 cls.presets.keys() 來列出類別上可用的所有內建預設集。

由於給定模型通常有多個預處理類別,因此應該在特定子類別上呼叫此方法,例如 keras_nlp.models.BertTextClassifierPreprocessor.from_preset()

參數

  • preset:字串。內建的預設集識別碼、Kaggle 模型控點、Hugging Face 控點或本機目錄的路徑。

範例

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

# Load a preprocessor for Bert classification.
preprocessor = keras_nlp.models.BertTextClassifierPreprocessor.from_preset(
    "bert_base_en",
)
預設集名稱 參數 說明
albert_base_en_uncased 11.68M 所有輸入都小寫的 12 層 ALBERT 模型。根據英文維基百科 + BooksCorpus 進行訓練。
albert_large_en_uncased 17.68M 所有輸入都小寫的 24 層 ALBERT 模型。根據英文維基百科 + BooksCorpus 進行訓練。
albert_extra_large_en_uncased 58.72M 所有輸入都小寫的 24 層 ALBERT 模型。根據英文維基百科 + BooksCorpus 進行訓練。
albert_extra_extra_large_en_uncased 222.60M 所有輸入都小寫的 12 層 ALBERT 模型。根據英文維基百科 + BooksCorpus 進行訓練。

tokenizer 屬性

keras_nlp.models.AlbertTextClassifierPreprocessor.tokenizer

用於將字串標記化的標記器。