使用 Optimum Habana 预训练 Transformers
从 Transformers 预训练模型,如 BERT,与微调一样简单。模型应该从配置中使用 .from_config
实例化,而不是从预训练的检查点中使用 .from_pretrained
实例化。以下是如何使用 GPT2 的示例
from transformers import AutoConfig, AutoModelForXXX
config = AutoConfig.from_pretrained("gpt2")
model = AutoModelForXXX.from_config(config)
其中 XXX 是要执行的任务,例如 ImageClassification
。
以下是一个 BERT 用于掩码语言建模的预训练工作示例
from datasets import load_dataset
from optimum.habana import GaudiTrainer, GaudiTrainingArguments
from transformers import AutoConfig, AutoModelForMaskedLM, AutoTokenizer, DataCollatorForLanguageModeling
# Load the training set (this one has already been preprocessed)
training_set = load_dataset("philschmid/processed_bert_dataset", split="train")
# Load the tokenizer
tokenizer = AutoTokenizer.from_pretrained("philschmid/bert-base-uncased-2022-habana")
# Instantiate an untrained model
config = AutoConfig.from_pretrained("bert-base-uncased")
model = AutoModelForMaskedLM.from_config(config)
model.resize_token_embeddings(len(tokenizer))
# The data collator will take care of randomly masking the tokens
data_collator = DataCollatorForLanguageModeling(tokenizer=tokenizer)
training_args = GaudiTrainingArguments(
output_dir="/tmp/bert-base-uncased-mlm",
num_train_epochs=1,
per_device_train_batch_size=8,
use_habana=True,
use_lazy_mode=True,
gaudi_config_name="Habana/bert-base-uncased",
)
# Initialize our Trainer
trainer = GaudiTrainer(
model=model,
args=training_args,
train_dataset=training_set,
tokenizer=tokenizer,
data_collator=data_collator,
)
trainer.train()
您可以在 这篇博文 中看到另一个预训练示例。
< > 在 GitHub 上更新