KerasHub:預訓練模型 / API 文件 / 模型架構 / GPT2 / GPT2CausalLMPreprocessor 層

GPT2CausalLMPreprocessor 層

[來源]

GPT2CausalLMPreprocessor 類別

keras_hub.models.GPT2CausalLMPreprocessor(
    tokenizer, sequence_length=1024, add_start_token=True, add_end_token=True, **kwargs
)

GPT2 因果語言模型預處理器。

此預處理層旨在與 keras_hub.models.GPT2CausalLM 一起使用。預設情況下,它將接收字串批次,並以 (x, y, sample_weight) 格式傳回輸出,其中 y 標籤是 x 序列中的下一個 token ID。

為了用於生成,此層也公開了兩種方法 generate_preprocess()generate_postprocess()。當此預處理器附加到 keras_hub.models.GPT2CausalLM 實例時,這些方法將在 generate() 中隱含地調用。它們也可以獨立調用(例如,在單獨的進程中預先計算生成預處理輸入)。

引數

  • tokenizerkeras_hub.models.GPT2Tokenizer 實例。
  • sequence_length:封裝輸入的長度。
  • add_start_token:如果為 True,預處理器將在每個輸入序列前面加上 tokenizer 的開始 token。
  • add_end_token:如果為 True,預處理器將在每個輸入序列後面加上 tokenizer 的結束 token。

呼叫引數

  • x:字串、tf.Tensor 或 Python 字串列表。
  • y:標籤資料。應始終為 None,因為此層會生成標籤。
  • sample_weight:標籤權重。應始終為 None,因為此層會生成標籤權重。
  • sequence_length:傳遞以覆寫層的已配置 sequence_length

範例

# Load the preprocessor from a preset.
preprocessor = keras_hub.models.GPT2CausalLMPreprocessor.from_preset(
    "gpt2_base_en"
)

# Tokenize and pack a single sentence.
sentence = tf.constant("League of legends")
preprocessor(sentence)
# Same output.
preprocessor("League of legends")

# Tokenize a batch of sentences.
sentences = tf.constant(["Taco tuesday", "Fish taco please!"])
preprocessor(sentences)
# Same output.
preprocessor(["Taco tuesday", "Fish taco please!"])

# Map a dataset to preprocess a single sentence.
features = tf.constant(
    [
        "Avatar 2 is amazing!",
        "Well, I am not sure.",
    ]
)
labels = tf.constant([1, 0])
ds = tf.data.Dataset.from_tensor_slices((features, labels))
ds = ds.map(preprocessor, num_parallel_calls=tf.data.AUTOTUNE)

# Map a dataset to preprocess unlabled sentences.
ds = tf.data.Dataset.from_tensor_slices(features)
ds = ds.map(preprocessor, num_parallel_calls=tf.data.AUTOTUNE)

[來源]

from_preset 方法

GPT2CausalLMPreprocessor.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:字串。內建預設識別符、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",
)
預設 參數 描述
gpt2_base_en 124.44M 12 層 GPT-2 模型,其中保留大小寫。在 WebText 上訓練。
gpt2_base_en_cnn_dailymail 124.44M 12 層 GPT-2 模型,其中保留大小寫。在 CNN/DailyMail 摘要資料集上微調。
gpt2_medium_en 354.82M 24 層 GPT-2 模型,其中保留大小寫。在 WebText 上訓練。
gpt2_large_en 774.03M 36 層 GPT-2 模型,其中保留大小寫。在 WebText 上訓練。
gpt2_extra_large_en 1.56B 48 層 GPT-2 模型,其中保留大小寫。在 WebText 上訓練。

[來源]

generate_preprocess 方法

GPT2CausalLMPreprocessor.generate_preprocess(x, sequence_length=None)

將字串轉換為用於生成的整數 token 輸入。

與為訓練呼叫此層類似,此方法接收字串或 tensor 字串,將輸入 token 化和封裝,並計算一個 padding 遮罩,遮罩所有未以 padding 值填入的輸入。

與為訓練呼叫此層不同,此方法不計算標籤,並且永遠不會將 tokenizer.end_token_id 附加到序列的末尾(因為預期生成會在輸入提示的末尾繼續)。


[來源]

generate_postprocess 方法

GPT2CausalLMPreprocessor.generate_postprocess(x)

將用於生成的整數 token 輸出轉換為字串。

此方法反轉 generate_preprocess(),方法是先移除所有 padding 和開始/結束 token,然後將整數序列轉換回字串。


tokenizer 屬性

keras_hub.models.GPT2CausalLMPreprocessor.tokenizer

用於 token 化字串的 tokenizer。