# Using PyTorch model in TensorFlow

**URL:** </static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fusing-pytorch-model-in-tensorflow%2F4202%3E%5C
**Category:** 🤗Transformers\
**Created:** [March 6, 2021, 9:52am UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fusing-pytorch-model-in-tensorflow%2F4202 "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](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2Fgruffgoran)%5C
**Post date:** [March 6, 2021, 9:52am UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fusing-pytorch-model-in-tensorflow%2F4202%2F1 "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](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2Flewtun)%5C
**Post date:** [March 6, 2021, 2:32pm UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fusing-pytorch-model-in-tensorflow%2F4202%2F2 "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](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2FAayushShah)%5C
**Post date:** [June 7, 2023, 10:15am UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fusing-pytorch-model-in-tensorflow%2F4202%2F3 "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.
