I am pretty sure this question has been answered before, but I could not find it.
Basically, I am learning how to train a Bert classifier from scratch to classify a set of e-mails as âspamâ or ânot spamâ on Google colab using T4 GPU
Most of the code is pretty basic and standard, i.e., loading âsms_spamâ data set, tokening loading the model (âdistilbert-base-uncasedâ), and Unfreezing all the model parameters.
And here is the instance of the trainer class:
import numpy as np
from transformers import DataCollatorWithPadding, Trainer, TrainingArguments
from accelerate import Accelerator
accelerator = Accelerator()
def compute_metrics(eval_pred):
predictions, labels = eval_pred
predictions = np.argmax(predictions, axis=1)
return {âaccuracyâ: (predictions == labels).mean()}
The HuggingFace Trainer class handles the training and eval loop for PyTorch for us.
Read more about it here Trainer
trainer = Trainer(
model=model,
args=TrainingArguments(
output_dir=â/content/drive/My Drive/DATA/spam_not_spamâ,
learning_rate=2e-3,
per_device_train_batch_size=4,
per_device_eval_batch_size=4,
evaluation_strategy=âepochâ,
save_strategy=âepochâ,
num_train_epochs=2,
weight_decay=0.01,
load_best_model_at_end=True,
),
train_dataset=tokenized_dataset["train"],
eval_dataset=tokenized_dataset["test"],
tokenizer=tokenizer,
data_collator=DataCollatorWithPadding(tokenizer=tokenizer),
compute_metrics=compute_metrics,
accelerator=accelerator,
)
trainer.train()
Note: First time, I did not have âaccelerateâ. Colab threw an error and suggested I install accelerator and create an instance which I did with â!pip install accelerate -Uâ
But even after that, I keep getting the following error:
<<
ImportError Traceback (most recent call last)
in <cell line: 18>()
18 trainer = Trainer(
19 model=model,
â> 20 args=TrainingArguments(
21 # output_dir=â./data/spam_not_spamâ,
22 output_dir=â/content/drive/My Drive/DATA/spam_not_spamâ,
4 frames
/usr/local/lib/python3.10/dist-packages/transformers/training_args.py in _setup_devices(self)
1785 if not is_sagemaker_mp_enabled():
1786 if not is_accelerate_available(min_version=â0.20.1â):
â 1787 raise ImportError(
1788 âUsing the Trainer
with PyTorch
requires accelerate>=0.20.1
: Please run pip install transformers[torch]
or pip install accelerate -U
â
1789 )
ImportError: Using the Trainer
with PyTorch
requires accelerate>=0.20.1
: Please run pip install transformers[torch]
or pip install accelerate -U
Can anyone offer any suggestions on what I am doing wrong and how to fix this? Cheers