# Using PyTorch model in TensorFlow

**URL:** <https://discuss.huggingface.co/t/using-pytorch-model-in-tensorflow/4202>\
**Category:** 🤗Transformers\
**Created:** [March 6, 2021, 9:52am UTC](https://discuss.huggingface.co/t/using-pytorch-model-in-tensorflow/4202 "2021-03-06T09:52:59Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![gruffgoran](https://avatars.discourse-cdn.com/v4/letter/g/7ab992/32.png) [@gruffgoran](https://discuss.huggingface.co/u/gruffgoran)\
**Post date:** [March 6, 2021, 9:52am UTC](https://discuss.huggingface.co/t/using-pytorch-model-in-tensorflow/4202/1 "2021-03-06T09:52:59Z")

</div>

Hi,

I would like to use a model built using PyTorch (namely [this one](https://huggingface.co/KB/bert-base-swedish-cased) ) in a Tensorflow environment.

More specifically I would like to start by just extract some of the embeddings in the later layers, and then potentially run some fine-tuning.

So I have two questions:

1. Is there a way to load and run inference from a PyTorch model in TensorFlow?
2. Is there a way to load and fine-tune a PyTorch model in Tensorflow?

Thanks in advance!

---

<div class="post-metadata">

**Author:** ![lewtun](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/lewtun/32/30995_2.png) [@lewtun](https://discuss.huggingface.co/u/lewtun)\
**Post date:** [March 6, 2021, 2:32pm UTC](https://discuss.huggingface.co/t/using-pytorch-model-in-tensorflow/4202/2 "2021-03-06T14:32:52Z")

</div>

Hi @gruffgoran, your use cases sound like a perfect match for the [ONNX](https://onnx.ai/index.html) format 🙂

Having said that, you might be able to get a quick win by trying something like the following (see [docs](https://huggingface.co/transformers/model_sharing.html#make-your-model-work-on-all-frameworks)):

```auto
tf_model = TFBertForSequenceClassification.from_pretrained("KB/bert-base-swedish-cased", from_pt=True)

```

From here you can then run inference / fine-tune etc using TensorFlow.

If you want to go the ONNX route, the idea would be to convert PyTorch → ONNX and then load the ONNX model in TensorFlow. Details on doing the conversion can be found here: [Exporting transformers models — transformers 4.3.0 documentation](https://huggingface.co/transformers/serialization.html#onnx-onnxruntime)

---

<div class="post-metadata">

**Author:** ![AayushShah](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/aayushshah/32/22671_2.png) [@AayushShah](https://discuss.huggingface.co/u/AayushShah)\
**Post date:** [June 7, 2023, 10:15am UTC](https://discuss.huggingface.co/t/using-pytorch-model-in-tensorflow/4202/3 "2023-06-07T10:15:58Z")

</div>

Hello, thanks but the link seems to be broken: [https://huggingface.co/transformers/serialization.html#onnx-onnxruntime](https://huggingface.co/transformers/serialization.html#onnx-onnxruntime)

Please direct to the correct one.
