XLMRobertaTextClassifier
類別keras_nlp.models.XLMRobertaTextClassifier(
backbone,
num_classes,
preprocessor=None,
activation=None,
hidden_dim=None,
dropout=0.0,
**kwargs
)
用於分類任務的端到端 XLM-RoBERTa 模型。
此模型將分類頭附加到 keras_nlp.model.XLMRobertaBackbone
實例,將骨幹輸出映射到適合分類任務的 logits。如需使用此模型搭配預先訓練的權重,請參閱 from_preset()
建構函式。
此模型可以選擇性地使用 preprocessor
層進行配置,在這種情況下,它將在 fit()
、predict()
和 evaluate()
期間自動將預處理應用於原始輸入。使用 from_preset()
建立模型時,預設會執行此操作。
免責聲明:預先訓練的模型「按原樣」提供,不提供任何形式的保證或條件。基礎模型由第三方提供,並受獨立授權條款約束,此處提供。
參數
keras_nlp.models.XLMRobertaBackbone
實例。keras_nlp.models.XLMRobertaTextClassifierPreprocessor
或 None
。如果為 None
,則此模型將不會應用預處理,並且應在呼叫模型之前對輸入進行預處理。str
或可呼叫函式。用於模型輸出的激活函式。設定 activation="softmax"
以返回輸出概率。預設值為 None
。範例
原始字串資料。
features = ["The quick brown fox jumped.", "نسيت الواجب"]
labels = [0, 3]
# Pretrained classifier.
classifier = keras_nlp.models.XLMRobertaTextClassifier.from_preset(
"xlm_roberta_base_multi",
num_classes=4,
)
classifier.fit(x=features, y=labels, batch_size=2)
classifier.predict(x=features, batch_size=2)
# Re-compile (e.g., with a new learning rate).
classifier.compile(
loss=keras.losses.SparseCategoricalCrossentropy(from_logits=True),
optimizer=keras.optimizers.Adam(5e-5),
jit_compile=True,
)
# Access backbone programmatically (e.g., to change `trainable`).
classifier.backbone.trainable = False
# Fit again.
classifier.fit(x=features, y=labels, batch_size=2)
預處理的整數資料。
features = {
"token_ids": np.ones(shape=(2, 12), dtype="int32"),
"padding_mask": np.array([[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0]] * 2),
}
labels = [0, 3]
# Pretrained classifier without preprocessing.
classifier = keras_nlp.models.XLMRobertaTextClassifier.from_preset(
"xlm_roberta_base_multi",
num_classes=4,
preprocessor=None,
)
classifier.fit(x=features, y=labels, batch_size=2)
自訂骨幹和詞彙表。
features = ["The quick brown fox jumped.", "نسيت الواجب"]
labels = [0, 3]
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,
sequence_length=128,
)
backbone = keras_nlp.models.XLMRobertaBackbone(
vocabulary_size=250002,
num_layers=4,
num_heads=4,
hidden_dim=256,
intermediate_dim=512,
max_sequence_length=128,
)
classifier = keras_nlp.models.XLMRobertaTextClassifier(
backbone=backbone,
preprocessor=preprocessor,
num_classes=4,
)
classifier.fit(x=features, y=labels, batch_size=2)
from_preset
方法XLMRobertaTextClassifier.from_preset(preset, load_weights=True, **kwargs)
從模型預設值實例化 keras_nlp.models.Task
。
預設集 (Preset) 是一個包含配置、權重和其他檔案資源的目錄,用於儲存和載入預先訓練的模型。preset
可以傳遞為以下其中一種:
'bert_base_en'
'kaggle://user/bert/keras/bert_base_en'
'hf://user/bert_base_en'
'./bert_base_en'
對於任何 Task
子類別,您可以執行 cls.presets.keys()
來列出類別上可用的所有內建預設集。
這個建構函數可以用兩種方式呼叫。可以從特定任務的基底類別呼叫,例如 keras_nlp.models.CausalLM.from_preset()
,也可以從模型類別呼叫,例如 keras_nlp.models.BertTextClassifier.from_preset()
。如果從基底類別呼叫,則返回物件的子類別將從預設集目錄中的配置推斷。
參數
True
,則儲存的權重將會載入模型架構中。如果為 False
,則所有權重都將會隨機初始化。範例
# Load a Gemma generative task.
causal_lm = keras_nlp.models.CausalLM.from_preset(
"gemma_2b_en",
)
# Load a Bert classification task.
model = keras_nlp.models.TextClassifier.from_preset(
"bert_base_en",
num_classes=2,
)
預設集名稱 | 參數 | 說明 |
---|---|---|
xlm_roberta_base_multi | 277.45M | 12 層 XLM-RoBERTa 模型,保留大小寫。以 100 種語言的 CommonCrawl 進行訓練。 |
xlm_roberta_large_multi | 558.84M | 24 層 XLM-RoBERTa 模型,保留大小寫。以 100 種語言的 CommonCrawl 進行訓練。 |
backbone
屬性keras_nlp.models.XLMRobertaTextClassifier.backbone
一個具有核心架構的 keras_nlp.models.Backbone
模型。
preprocessor
屬性keras_nlp.models.XLMRobertaTextClassifier.preprocessor
一個用於預處理輸入的 keras_nlp.models.Preprocessor
層。