huggingface / transformers

🤗 Transformers: State-of-the-art Machine Learning for Pytorch, TensorFlow, and JAX.
https://huggingface.co/transformers
Apache License 2.0
134.25k stars 26.84k forks source link

Error started happening today: ImportError: cannot import name 'get_linear_schedule_with_warmup' #1837

Closed Santosh-Gupta closed 4 years ago

Santosh-Gupta commented 4 years ago

🐛 Bug

Model I am using (Bert, XLNet....): Bert

Language I am using the model on (English, Chinese....): English

The problem arise when using:

The squad script: https://github.com/huggingface/transformers/blob/master/examples/run_squad.py

The tasks I am working on is:

Squad 2.0 from https://rajpurkar.github.io/SQuAD-explorer/

To Reproduce


!pip install transformers
import urllib.request

url = 'https://rajpurkar.github.io/SQuAD-explorer/dataset/train-v2.0.json'
urllib.request.urlretrieve(url, 'train-v2.0.json')

url = 'https://rajpurkar.github.io/SQuAD-explorer/dataset/dev-v2.0.json'
urllib.request.urlretrieve(url, 'dev-v2.0.json')
!wget 'https://raw.githubusercontent.com/huggingface/transformers/master/examples/run_squad.py'
!wget 'https://raw.githubusercontent.com/huggingface/pytorch-transformers/master/examples/utils_squad.py'
!wget 'https://raw.githubusercontent.com/huggingface/pytorch-transformers/master/examples/utils_squad_evaluate.py'

# $SQUAD_DIR/train-v1.1.json

SQUAD_Train = '/content/train-v2.0.json'
SQUAD_Dev = '/content/dev-v2.0.json'

!python run_squad.py \
  --model_type bert \
  --model_name_or_path bert-base-cased \
  --do_train \
  --do_eval \
  --do_lower_case \
  --train_file '$SQUAD_Train' \
  --predict_file '$SQUAD_Dev' \
  --per_gpu_train_batch_size 12 \
  --learning_rate 3e-5 \
  --num_train_epochs 2.0 \
  --max_seq_length 384 \
  --doc_stride 128 \
  --version_2_with_negative \
  --output_dir /tmp/debug_squad/

Results in

ImportError: cannot import name 'get_linear_schedule_with_warmup'

I ran it yesterday and it worked fine, but today it's not working.

For convenience, here's a colab notebook with the code

https://colab.research.google.com/drive/1tNisXX5siuNnkuEQ-X_XdEdDTtdJ0qeL

YuxiangLu commented 4 years ago

try edit: from transformers import AdamW from transformers import WarmupLinearSchedule as get_linear_schedule_with_warmup

yeliu918 commented 4 years ago

I tried the WarmupLinearSchedule, but I have a problem no key num_warmup_steps and num_training_steps. scheduler = WarmupLinearSchedule(optimizer, num_warmup_steps=args.warmup_steps, num_training_steps=t_total) I think get_linear_schedule_with_warmup and WarmupLinearSchedule are two different scheduler

julien-c commented 4 years ago

The version of the lib you use is not in sync with the scripts you run (cc @rlouf, @LysandreJik)

If you run the scripts from master, then you also need to install the lib from master:

pip install git+https://github.com/huggingface/transformers

This is a frequent issue so maybe we should do something about it, @thomwolf

rlouf commented 4 years ago

I tried the WarmupLinearSchedule, but I have a problem no key num_warmup_steps and num_training_steps. scheduler = WarmupLinearSchedule(optimizer, num_warmup_steps=args.warmup_steps, num_training_steps=t_total) I think get_linear_schedule_with_warmup and WarmupLinearSchedule are two different scheduler

They are the same schedulers but we introduced breaking changes, and indeed renamed warmup_steps -> num_warmup_steps and t_total -> ˋnum_training_steps`.

And yes, to work on the same version of the lib as the examples, go in the root directory and:

makevirtualenv my-project && workon my-project # or anything else you use to create a virtual environnement 
pip install .  # or Julien-c’s command

@julien-c I asked for advice on this one.

iedmrc commented 4 years ago

The version of the lib you use is not in sync with the scripts you run (cc @rlouf, @LysandreJik)

If you run the scripts from master, then you also need to install the lib from master:

pip install git+https://github.com/huggingface/transformers

This is a frequent issue so maybe we should do something about it, @thomwolf

Maybe we can indicate clearly on https://github.com/huggingface/transformers/blob/master/README.md#from-source or documentation.

ajay-sreeram commented 4 years ago

same problem for me too :(

YuxiangLu commented 4 years ago

same problem for me too :( reinstall the package from local: pip install .

rlouf commented 4 years ago

We are documenting this in #1889. It is because you are trying to run bleeding-edge examples with a pip-installed version of the library, which corresponds to the last release. Do as @YuxiangLu says in a new virtual environment.

iedmrc commented 4 years ago

can be closed because solved in #1889 ?

Am1n3e commented 4 years ago

when breaking changes are introduced the major number should increase to make the users aware.

LysandreJik commented 4 years ago

Indeed @aminHatim ! This is why this was released in version 2.2.0 an hour ago. Breaking changes are bound to happen when installing from source or running bleeding edges examples (which are based on the source).

cjagadis commented 7 months ago

ImportError: cannot import name 'get_linear_schedule_with_warmup' from 'transformers'

The version is 4.14.1.

ArthurZucker commented 7 months ago

I think you should upgrade to a more recent version