KerasHub:預訓練模型 / API 文件 / 模型架構 / OPT / OPTCausalLMPreprocessor 層

OPTCausalLMPreprocessor 層

[原始碼]

OPTCausalLMPreprocessor 類別

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

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

此預處理層主要用於 keras_hub.models.OPTCausalLM。預設情況下,它會接收一批字串,並以 (x, y, sample_weight) 格式傳回輸出,其中 y 標籤是 x 序列中的下一個詞符 ID。若用於產生,請傳入 return_labels=False,在此情況下,輸出將僅是編碼的字串特徵。

引數

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

呼叫引數

  • x:字串、tf.Tensor 或 Python 字串清單。
  • y:標籤資料。應始終為 None,因為該層會產生標籤。
  • sample_weight:標籤權重。應始終為 None,因為該層會產生標籤權重。
  • sequence_length:傳入以覆寫該層設定的 sequence_length
  • add_start_token:傳入以覆寫該層設定的 add_start_token 值。
  • add_end_token:傳入以覆寫該層設定的 add_end_token 值。
  • return_labels:如果為 True,輸出 "token_ids" 將會偏移一並以標籤傳回。如果為 False,則只會傳回特徵。

範例

# Load the preprocessor from a preset.
preprocessor = keras_hub.models.OPTCausalLMPreprocessor.from_preset(
    "opt_125m_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 方法

OPTCausalLMPreprocessor.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",
)
預設值 參數 說明
opt_125m_en 125.24M 12 層 OPT 模型,其中大小寫會保持不變。在 BookCorpus、CommonCrawl、Pile 和 PushShift.io 語料庫上訓練。
opt_1.3b_en 1.32B 24 層 OPT 模型,其中大小寫會保持不變。在 BookCorpus、CommonCrawl、Pile 和 PushShift.io 語料庫上訓練。
opt_2.7b_en 2.70B 32 層 OPT 模型,其中大小寫會保持不變。在 BookCorpus、CommonCrawl、Pile 和 PushShift.io 語料庫上訓練。
opt_6.7b_en 6.70B 32 層 OPT 模型,其中大小寫會保持不變。在 BookCorpus、CommonCrawl、Pile 和 PushShift.io 語料庫上訓練。

tokenizer 屬性

keras_hub.models.OPTCausalLMPreprocessor.tokenizer

用於詞符化字串的斷詞器。