# Loading PyTorch model from TF checkpoint

**URL:** <https://discuss.huggingface.co/t/loading-pytorch-model-from-tf-checkpoint/6338>\
**Category:** Beginners\
**Created:** [May 24, 2021, 12:11pm UTC](https://discuss.huggingface.co/t/loading-pytorch-model-from-tf-checkpoint/6338 "2021-05-24T12:11:16Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![SpaceMeerkat](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/spacemeerkat/32/1679_2.png) [@SpaceMeerkat](https://discuss.huggingface.co/u/SpaceMeerkat)\
**Post date:** [May 24, 2021, 12:11pm UTC](https://discuss.huggingface.co/t/loading-pytorch-model-from-tf-checkpoint/6338/1 "2021-05-24T12:11:16Z")

</div>

I am trying to load a pretrained model from the model repository ([this model](https://huggingface.co/cross-encoder/ms-marco-TinyBERT-L-6)), but when I attempt to instantiate the model I get an error referring to loading a PyTorch model from a TensorFlow checkpoint.

There are similar posts (e.g. [[1]](https://github.com/huggingface/transformers/issues/4336), [[2]](https://discuss.huggingface.co/t/how-to-load-weights-which-was-trained-older-version-of-pytorch/1267)) from others experiencing this problem in the past but I can’t for the life of me see how they are resolved. Some attempt to match the `sentence-transformers` and `torch` package versions to when the models were trained but I have no idea how to find the versions to down/upgrade my packages. Others simply upgrade their `torch` package to 1.8.0 which I’m using anyway.

### Edit:

Please note that I am explicitly trying to load the model from it’s repository files rather than use the recommended way of loading these models which is to simply pass the model name e.g. `model = CrossEncoder('ms-marco-TinyBERT-L-6')`, which triggers an automatic http download of the model I believe.

* * *

Here is my workflow:

### Clone the model repository and get the required packages:

```auto
$git clone https://huggingface.co/cross-encoder/ms-marco-TinyBERT-L-6
$conda install -c conda-forge sentence-transformers

```

### Load the model in a python script:

```auto
### Import packages
from sentence_transformers.cross_encoder import CrossEncoder

### Setup paths
model_path = 'ms-marco-TinyBERT-L-6'

### Instantiate model
model = CrossEncoder(model_path)

```

### The resulting error:

> Traceback (most recent call last):  
> File “/home/userx/anaconda3/envs/NLP/lib/python3.6/site-packages/transformers/modeling\_utils.py”, line 1205, in from\_pretrained  
> state\_dict = torch.load(resolved\_archive\_file, map\_location=“cpu”)  
> File “/home/userx/anaconda3/envs/NLP/lib/python3.6/site-packages/torch/serialization.py”, line 593, in load  
> return \_legacy\_load(opened\_file, map\_location, pickle\_module, \*\*pickle\_load\_args)  
> File “/home/userx/anaconda3/envs/NLP/lib/python3.6/site-packages/torch/serialization.py”, line 762, in \_legacy\_load  
> magic\_number = pickle\_module.load(f, \*\*pickle\_load\_args)  
> \_pickle.UnpicklingError: invalid load key, ‘v’.
> 
> During handling of the above exception, another exception occurred:
> 
> Traceback (most recent call last):  
> File “test.py”, line 8, in   
> model = CrossEncoder(model\_path)  
> File “/home/userx/anaconda3/envs/NLP/lib/python3.6/site-packages/sentence\_transformers/cross\_encoder/CrossEncoder.py”, line 47, in **init**  
> self.model = AutoModelForSequenceClassification.from\_pretrained(model\_name, config=self.config)  
> File “/home/userx/anaconda3/envs/NLP/lib/python3.6/site-packages/transformers/models/auto/auto\_factory.py”, line 381, in from\_pretrained  
> return model\_class.from\_pretrained(pretrained\_model\_name\_or\_path, \*model\_args, config=config, \*\*kwargs)  
> File “/home/userx/anaconda3/envs/NLP/lib/python3.6/site-packages/transformers/modeling\_utils.py”, line 1208, in from\_pretrained  
> f"Unable to load weights from pytorch checkpoint file for ‘{pretrained\_model\_name\_or\_path}’ "  
> OSError: Unable to load weights from pytorch checkpoint file for ‘ms-marco-TinyBERT-L-6’ at 'ms-marco-TinyBERT-L-6/pytorch\_model.bin’If you tried to load a PyTorch model from a TF 2.0 checkpoint, please set from\_tf=True.

Any help on this would be greatly appreciated, thank you!

### Extra info that may be useful

sentence-transformers=0.4.1  
pytorch=1.8.0

---

<div class="post-metadata">

**Author:** ![CK42](https://avatars.discourse-cdn.com/v4/letter/c/b3f665/32.png) [@CK42](https://discuss.huggingface.co/u/CK42)\
**Post date:** [August 13, 2022, 2:59pm UTC](https://discuss.huggingface.co/t/loading-pytorch-model-from-tf-checkpoint/6338/2 "2022-08-13T14:59:51Z")

</div>

I had a similar problem and this helped [Getting an error “UnpicklingError: invalid load key, 'v'.” in Pytorch model deploying in Streamlit - #3 by Anubhav1107 - PyTorch Forums](https://discuss.pytorch.org/t/getting-an-error-unpicklingerror-invalid-load-key-v-in-pytorch-model-deploying-in-streamlit/107768/3)
