Is there a way to convert tensorflow model to pytorch?

I’m trying to understand how to rewrite a model architecture from tensorflow to pytorch and other way around
are there any automatic tools for that?
I only managed to convert tensorflow to onnx, but wasn’t able to convert it further from onnx to pytoch

I think you could ask this in the pytorch forum:

1 Like

yes i already did, but i thought as it’s a question that belong to both sides so I can get answers here and there

If you have already the tf->onnx it is on the pytorch side.


1 Like

i tried this one, but couldn’t fix errors while converting this way

it’s not necessarily should be with onnx , but any converter from tf to pytorch and from pytorch to tf

Anything reliable that I know on our side. What is you scope?

okay thank you for answers!
I was just curious if there is any established converter Im missing, but I guess there aren’t many of them

As far as I know, there is no tool that automatically converts it, you can convert it via NumPy arrays. For example, you can convert torch.nn.Linear layer to tf.keras.layers.Dense layer with the below code.

torch_linear = ...
tf_dense = ...

kernel = torch_linear.weight.T.numpy()
bias = torch_linear.bias.numpy()
tf_dense.set_weights([kernel, bias])

Also, you can convert more complex models like BERT by converting each layer. If all operations and values are the exactly same, like the epsilon value of layer normalization(PyTorch has 1e-5 as default, and TensorFlow has 1e-3 as default), the output value will be very very close. You can check it with np.testing.assert_allclose.

I wrote a post in Korean related to this topic almost a year ago.Maybe you can use the translation in Chrome or something else. You can check it if you want details.


thank you, I will mb try this way!
I’ve seen this post already :rofl::rofl::rofl:

1 Like