transformers: AttributeError: 'BertForPreTraining' object has no attribute 'shape'
Is there any suggestion for fixing the following? I was trying “convert_tf_checkpoint_to_pytorch.py” to convert a model trained from scratch but the conversion didn’t work out…
Skipping cls/seq_relationship/output_weights/adam_v
Traceback (most recent call last):
File "pytorch_pretrained_bert/convert_tf_checkpoint_to_pytorch.py", line 66, in <module>
args.pytorch_dump_path)
File "pytorch_pretrained_bert/convert_tf_checkpoint_to_pytorch.py", line 37, in convert_tf_checkpoint_to_pytorch
load_tf_weights_in_bert(model, tf_checkpoint_path)
File "/content/my_pytorch-pretrained-BERT/pytorch_pretrained_bert/modeling.py", line 117, in load_tf_weights_in_bert
assert pointer.shape == array.shape
File "/usr/local/lib/python3.6/dist-packages/torch/nn/modules/module.py", line 535, in __getattr__
type(self).__name__, name))
AttributeError: 'BertForPreTraining' object has no attribute 'shape'
About this issue
- Original URL
- State: closed
- Created 5 years ago
- Comments: 16 (2 by maintainers)
Commits related to this issue
- # ebudur: added some additional variables to skip according to the suggestion in the following issue # https://github.com/huggingface/transformers/issues/393#issuecomment-513446685 — committed to e-budur/transformers by e-budur 4 years ago
- # added the variable "batch_finite" as well # according to the suggestion in the following issue # https://github.com/huggingface/transformers/issues/393#issuecomment-513446685 — committed to e-budur/transformers by e-budur 4 years ago
- # added the variable "accum" as well # according to the suggestion in the following issue # https://github.com/huggingface/transformers/issues/393#issuecomment-513446685 — committed to e-budur/transformers by e-budur 4 years ago
- # added the variable "local_step" as well # according to the suggestion in the following issue # https://github.com/huggingface/transformers/issues/393#issuecomment-513446685 — committed to e-budur/transformers by e-budur 4 years ago
- Add support for Grouped Query Attention on Llama Model (#393) Resolves #388 — committed to birkskyum/transformers by felladrin 8 months ago
I’m getting a similar error when trying to convert the newer BERT models released at tensorflow/models/tree/master/official/nlp/.
These models are either BERT models trained with Keras or else checkpoints converted from the original google-research/bert repository. I also get the same error when I convert the TF1 to TF2 checkpoints myself using the tf2_encoder_checkpoint_converter.py script:
What I have tried:
First, I have downloaded a model:
After unpacking:
The command prints the configuration but throws the following error:
This is happening in a fresh environment with PyTorch 1.3 installed in Anaconda (Linux), as well as pip-installing
tf-nightly
andtransformers
(2.3.0).Has anyone else been able to successfully convert the TF 2.0 version models to PyTorch or know where I’m going wrong? Thanks!
@thomwolf If the above fix will be added to the master branch this will be great https://github.com/smartshark/transformers/pull/1
I managed to get it working by going through the pointers in debug mode and checking what variable name corresponded to what. This is the function I ended up using.
I revised
modeling_bert.by
following @lrizzello 's code and could save tf1 checkpoint I personally trained into pytorch. I first changed tf1 checkpoint to tf2, and then used the below code. Here is the code I revised inmodeling_bert.py
For convert_tf_to_pytorch function, I used below.
Hope this help!
Hi, this is an actual programming error in modeling_bert.py. If you look at line 145 it’s pretty obvious that the code should be continuing to the next iteration of the outer loop (over name, array) rather than the inner one (over the path components of name) - otherwise why would the error messages say “skipping {name}”:
https://github.com/huggingface/transformers/blob/master/src/transformers/models/bert/modeling_bert.py#L145
To fix this, simply extract the try/except block so that it wraps the entire loop (lines 127-148). I would supply a patch but I have to work with transformers 3.5.1 for the moment since I’m using sentence-transformers which hasn’t been updated to the latest version.
I solve this problem by bypass some variables in the model, such as “bad_steps”, “global_step", “good_steps”, “loss_scale”. They don’t have attribute 'shape‘ and I don’t need them when fineturning the model.
In modeling.py, line 121, replace it with
if any(n in [“adam_v”, “adam_m”, “global_step”, “bad_steps”, “global_step”, “good_steps”, “loss_scale”] for n in name): and delete line 151-156.