Import error in training arguments in Colaboratory

Question:

I am using Google Colaboratory for my NLP project. I installed transformers and other libraries, but I got an error.

from transformers import Trainer, TrainingArguments

batch_size = 64
logging_steps = len(stationary_dataset_encoded["train"]) // batch_size
model_name = f"{model_ckpt}-finetuned-stationary-update"
training_args = TrainingArguments(output_dir=model_name,
                                  num_train_epochs=10,
                                  learning_rate=2e-5,
                                  per_device_train_batch_size=batch_size,
                                  per_device_eval_batch_size=batch_size,
                                  weight_decay=0.01,
                                  evaluation_strategy="epoch",
                                  disable_tqdm=False,
                                  logging_steps=logging_steps,
                                  push_to_hub=False,
                                  log_level="error")

In the training_args line, I got an import error. It’s showing as:

ImportError                               Traceback (most recent call last)
<ipython-input-98-839907b16fa0> in <cell line: 6>()
      4 logging_steps = len(stationary_dataset_encoded["train"]) // batch_size
      5 model_name = f"{model_ckpt}-finetuned-stationary-update"
----> 6 training_args = TrainingArguments(output_dir=model_name,
      7                                   num_train_epochs=10,
      8                                   learning_rate=2e-5,

4 frames
/usr/local/lib/python3.10/dist-packages/transformers/training_args.py in _setup_devices(self)
   1785     def __str__(self):
   1786         self_as_dict = asdict(self)
-> 1787
   1788         # Remove deprecated arguments. That code should be removed once
   1789         # those deprecated arguments are removed from TrainingArguments. (TODO: v5)

ImportError: Using the `Trainer` with `PyTorch` requires `accelerate>=0.20.1`: Please run `pip install transformers[torch]` or `pip install accelerate -U

I tried to reinstall transformers, use pip install accelerate -U and also used PyTorch accelerate >= 0.20.1.

How can I resolve this issue?

Asked By: Kamruzzaman Sajib

||

Answers:

There might be an issue with your transformers library installation.

  1. Uninstall the current transformers library.
  2. Reinstall the transformers library.
  3. Install the accelerate library.
  4. Ensure that you have the correct version of PyTorch.
!pip uninstall -y transformers
!pip install transformers
!pip install accelerate -U

Check your PyTorch version with the following command:

import torch
print(torch.__version__)

The message suggests that you need to install the accelerate library with version 0.20.1 or higher.

You can install it using the following command:

!pip install accelerate -U

you might want to try installing the transformers library with the torch extra. This can be done with the following command:

!pip install transformers[torch]

Answered By: Jannik Sinz
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.