Transformers: Getting "AttributeError: 'Tensor' object has no attribute 'numpy'" while fine-tuning BERT for NER

Created on 21 Jul 2020  路  2Comments  路  Source: huggingface/transformers

As per https://github.com/huggingface/transformers/tree/master/examples/token-classification after doing the required setup and installing required libraries, when I run

python3 run_tf_ner.py --data_dir ./ \
--labels ./labels.txt \
--model_name_or_path $BERT_MODEL \
--output_dir $OUTPUT_DIR \
--max_seq_length  $MAX_LENGTH \
--num_train_epochs $NUM_EPOCHS \
--per_device_train_batch_size $BATCH_SIZE \
--save_steps $SAVE_STEPS \
--seed $SEED \
--do_train \
--do_eval \
--do_predict

it stops at one point with error

    /usr/local/lib/python3.6/dist-packages/transformers/trainer_tf.py:488 _accumulate_next  *
        return self._accumulate_gradients(per_replica_features, per_replica_labels)
    /usr/local/lib/python3.6/dist-packages/transformers/trainer_tf.py:498 _accumulate_gradients  *
        per_replica_loss = self.args.strategy.experimental_run_v2(
    /usr/local/lib/python3.6/dist-packages/transformers/trainer_tf.py:511 _forward  *
        per_example_loss, _ = self._run_model(features, labels, True)
    /usr/local/lib/python3.6/dist-packages/transformers/trainer_tf.py:534 _run_model  *
        outputs = self.model(features, labels=labels, training=training)[:2]
    /usr/local/lib/python3.6/dist-packages/transformers/modeling_tf_distilbert.py:879 call  *
        loss = self.compute_loss(labels, logits)
    /usr/local/lib/python3.6/dist-packages/transformers/modeling_tf_utils.py:142 compute_loss  *
        if tf.math.reduce_any(labels == -1).numpy() is True:

    AttributeError: 'Tensor' object has no attribute 'numpy'

Tensorflow version: 2.2.0
Numpy version: 1.19.0
CUDA version: 10.2

As per some possible solutions I have checked that tf.executing_eagerly() is True.

Tried on own computer and colab both and it ends up at the same point with same error.

Most helpful comment

I run into the same error, installing Transformers with pip fix this (not a preferred solution but it works)
!pip install --upgrade --no-deps --force-reinstall transformers

fine-tuning BERT for NER also fail using run_ner.py. The error is

Traceback (most recent call last):
  File "transformers/examples/token-classification/run_ner.py", line 304, in <module>
    main()
  File "transformers/examples/token-classification/run_ner.py", line 266, in main
    predictions, label_ids, metrics = trainer.predict(test_dataset)
  File "/usr/local/lib/python3.6/dist-packages/transformers/trainer.py", line 781, in predict
    test_dataloader = self.get_test_dataloader(test_dataset)
  File "/usr/local/lib/python3.6/dist-packages/transformers/trainer.py", line 297, in get_test_dataloader
    if isinstance(self.test_dataset, torch.utils.data.IterableDataset):
AttributeError: 'Trainer' object has no attribute 'test_dataset'

All 2 comments

I run into the same error, installing Transformers with pip fix this (not a preferred solution but it works)
!pip install --upgrade --no-deps --force-reinstall transformers

fine-tuning BERT for NER also fail using run_ner.py. The error is

Traceback (most recent call last):
  File "transformers/examples/token-classification/run_ner.py", line 304, in <module>
    main()
  File "transformers/examples/token-classification/run_ner.py", line 266, in main
    predictions, label_ids, metrics = trainer.predict(test_dataset)
  File "/usr/local/lib/python3.6/dist-packages/transformers/trainer.py", line 781, in predict
    test_dataloader = self.get_test_dataloader(test_dataset)
  File "/usr/local/lib/python3.6/dist-packages/transformers/trainer.py", line 297, in get_test_dataloader
    if isinstance(self.test_dataset, torch.utils.data.IterableDataset):
AttributeError: 'Trainer' object has no attribute 'test_dataset'

Thanks for the input @kevin-yauris

I run into the same error, installing Transformers with pip fix this (not a preferred solution but it works)
!pip install --upgrade --no-deps --force-reinstall transformers

This fixed the issue for me too.

Was this page helpful?
0 / 5 - 0 ratings