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 序列中的下一個詞符 ID。

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

引數

  • tokenizerkeras_hub.models.GPT2Tokenizer 實例。
  • sequence_length:封裝輸入的長度。
  • add_start_token:如果為 True,預處理器會將詞符器的開始詞符附加到每個輸入序列的開頭。
  • add_end_token:如果為 True,預處理器會將詞符器的結束詞符附加到每個輸入序列的結尾。

呼叫引數

  • 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 模型控制代碼,例如 '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 模型控制代碼、Hugging Face 控制代碼或本機目錄的路徑。

範例

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

# Load a preprocessor for Bert classification.
preprocessor = keras_hub.models.BertTextClassifierPreprocessor.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)

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

與呼叫該層進行訓練類似,此方法接收字串或張量字串,將輸入標記並封裝,並計算遮罩所有未以填補值填入的輸入的填補遮罩。

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


[原始碼]

generate_postprocess 方法

GPT2CausalLMPreprocessor.generate_postprocess(x)

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

此方法會反轉 generate_preprocess(),首先移除所有填補和開始/結束詞符,然後將整數序列轉換回字串。


tokenizer 屬性

keras_hub.models.GPT2CausalLMPreprocessor.tokenizer

用於標記字串的詞符器。