Keras 3 API 文件 / KerasNLP / 預訓練模型 / XLMRoberta / XLMRobertaTextClassifierPreprocessor 層

XLMRobertaTextClassifierPreprocessor 層

[來源]

XLMRobertaTextClassifierPreprocessor 類別

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

用於對輸入進行標記化和打包的 XLM-RoBERTa 預處理層。

此預處理層將執行三個操作

  1. 使用 tokenizer 對任意數量的輸入區段進行標記化。
  2. 使用 keras_nlp.layers.MultiSegmentPacker 將輸入打包在一起,並使用適當的 "<s>""</s>""<pad>" 標記,即在整個序列的開頭添加一個 "<s>",在每個區段的結尾添加 "</s></s>"(最後一個區段除外),並在整個序列的結尾添加 "</s>"
  3. 建構一個包含鍵 "token_ids""padding_mask" 的字典,可以直接傳遞給 XLM-RoBERTa 模型。

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

參數

  • tokenizer:一個 keras_nlp.tokenizers.XLMRobertaTokenizer 實例。
  • sequence_length:打包輸入的長度。
  • truncate:用於截斷批次區段列表以適應 sequence_length 的演算法。值可以是 round_robinwaterfall: - "round_robin":可用空間以循環方式一次分配一個標記給仍然需要的輸入,直到達到限制。 - "waterfall":使用「瀑布」演算法分配預算,該演算法以從左到右的方式分配配額,並填滿區塊,直到預算用完為止。它支援任意數量的區段。

呼叫參數

  • x:單個字串序列的張量,或要打包在一起的多個張量序列的元組。輸入可以是批次或非批次的。對於單個序列,原始 Python 輸入將轉換為張量。對於多個序列,請直接傳遞張量。
  • y:任何標籤數據。將照原樣傳遞。
  • sample_weight:任何標籤權重數據。將照原樣傳遞。

範例

直接在數據上呼叫圖層。

preprocessor = keras_nlp.models.TextClassifierPreprocessor.from_preset(
    "xlm_roberta_base_multi"
)

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

# Tokenize a batch of single sentences.
preprocessor(["The quick brown fox jumped.", "اسمي اسماعيل"])

# Preprocess a batch of sentence pairs.
# When handling multiple sequences, always convert to tensors first!
first = tf.constant(["The quick brown fox jumped.", "اسمي اسماعيل"])
second = tf.constant(["The fox tripped.", "الأسد ملك الغابة"])
preprocessor((first, second))

# Custom vocabulary.
def train_sentencepiece(ds, vocab_size):
    bytes_io = io.BytesIO()
    sentencepiece.SentencePieceTrainer.train(
        sentence_iterator=ds.as_numpy_iterator(),
        model_writer=bytes_io,
        vocab_size=vocab_size,
        model_type="WORD",
        unk_id=0,
        bos_id=1,
        eos_id=2,
    )
    return bytes_io.getvalue()
ds = tf.data.Dataset.from_tensor_slices(
    ["the quick brown fox", "the earth is round"]
)
proto = train_sentencepiece(ds, vocab_size=10)
tokenizer = keras_nlp.models.XLMRobertaTokenizer(proto=proto)
preprocessor = keras_nlp.models.XLMRobertaTextClassifierPreprocessor(
    tokenizer
)
preprocessor("The quick brown fox jumped.")

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

preprocessor = keras_nlp.models.TextClassifierPreprocessor.from_preset(
    "xlm_roberta_base_multi"
)

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 方法

XLMRobertaTextClassifierPreprocessor.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",
)
預設集名稱 參數 說明
xlm_roberta_base_multi 277.45M 12 層 XLM-RoBERTa 模型,保留大小寫。在 CommonCrawl 上以 100 種語言進行訓練。
xlm_roberta_large_multi 558.84M 24 層 XLM-RoBERTa 模型,保留大小寫。在 CommonCrawl 上以 100 種語言進行訓練。

tokenizer 屬性

keras_nlp.models.XLMRobertaTextClassifierPreprocessor.tokenizer

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