Transpile any model

Transpile a Keras model into a PyTorch module.

⚠️ If you are running this notebook in Colab, you will have to install Ivy and some dependencies manually. You can do so by running the cell below ⬇️

If you want to run the notebook locally but don’t have Ivy installed just yet, you can check out the Setting Up section of the docs.

!git clone
!cd ivy && git checkout d6bc18c64a47a135fe18404d9f83f98d9f3b63cf && python3 -m pip install --user -e .

For the installed packages to be available you will have to restart your kernel. In Colab, you can do this by clicking on “Runtime > Restart Runtime”. Once the runtime has been restarted you should skip the previous cell 😄

To use the compiler and the transpiler now you will need an API Key. If you already have one, you should replace the string in the next cell.

!mkdir -p .ivy
!echo -n $API_KEY > .ivy/key.pem

As we’ve already seen, ivy.transpile can convert functions and whole libraries from one framework to another. However, in machine learning and deep learning, much of the focus is on trainable modules. Fortunately, Ivy can manage the parameters of these modules and ensure that the transpiled module is fully compatible with the target framework. This allows you to take full advantage of the training utilities provided by any framework and to build complex models on top of the transpiled ones. Let’s see how this works!

Let’s start by importing the neccessary libraries:

import ivy
import torch
import numpy as np
import tensorflow as tf

There are examples which use more involved models in the Guides section, but to keep things simple, let’s define a basic convolutional network using Keras’ Sequential API to use it as the starting point!

model = tf.keras.Sequential([
    tf.keras.layers.Conv2D(32, kernel_size=(3, 3), activation='relu', input_shape=(28, 28, 3)),
    tf.keras.layers.Dense(10, activation='softmax')

Now, we can use ivy.transpile to convert this Keras model to PyTorch. Since we are passing a framework-specific object to the transpile function, there is no need to specify the source keyword argument this time.

input_array = tf.random.normal((1, 28, 28, 3))
torch_model = ivy.transpile(model, to="torch", args=(input_array,))

Thanks to (eager) transpilation, we now have a fully-fledged torch.nn.module 🚀

isinstance(torch_model, torch.nn.Module)

This means that we can pass PyTorch inputs (keeping the channels-last format of Keras, as the new computational graph is identical to the original one!) and get PyTorch tensors as the output:

input_array = torch.rand((1, 28, 28, 3))
output_array = torch_model(input_array)
tensor([[0.0768, 0.0727, 0.0942, 0.1300, 0.1350, 0.0839, 0.1511, 0.1061, 0.0606,
         0.0896]], grad_fn=<SoftmaxBackward0>)

Furthermore, having a torch.nn.Module also enables you to train the model using PyTorch training code, and also to use the transpiled model to build more complex torch models, as shown in the Transpiling a haiku model to build on top guide!

Round up

This is the last tutorial related to the compiler/transpiler in Learn the Basics. In the next tutorial, we’ll go over an introduction to building models directly using Ivy 👨‍💻. If you are interested in continuing to learn about transpilation, you can check out the more complex tutorials in the Guides section!