-
Notifications
You must be signed in to change notification settings - Fork 61
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add torch -> tensorflow model transpilation demo
- Loading branch information
1 parent
90d2d7c
commit 8db7b5d
Showing
3 changed files
with
182 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import torch | ||
|
||
|
||
class SimpleModel(torch.nn.Module): | ||
def __init__(self): | ||
super(SimpleModel, self).__init__() | ||
self.conv1 = torch.nn.Conv2d(1, 3, kernel_size=3) | ||
self.relu = torch.nn.ReLU() | ||
self.fc = torch.nn.Linear(3 * 26 * 26, 10) | ||
|
||
def forward(self, x): | ||
x = self.conv1(x) | ||
x = self.relu(x) | ||
x = torch.flatten(x, 1) | ||
x = self.fc(x) | ||
return x |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,160 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"# Transpiling Models from PyTorch to TensorFlow" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"You can install the dependencies required for this notebook by running the cell below ⬇️, or check out the [Get Started](https://ivy.dev/docs/overview/get_started.html) section of the docs to find out more about installing ivy." | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"!pip install ivy\n", | ||
"!pip install torch\n", | ||
"!pip install tensorflow" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"Here we'll go through an example of how any model written in PyTorch can be converted, and used in, TensorFlow via `ivy.transpile`. First, lets import a simple torch model." | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"from example_models import SimpleModel\n", | ||
"\n", | ||
"\"\"\"\n", | ||
"This model is defined as follows:\n", | ||
"\n", | ||
"class SimpleModel(torch.nn.Module):\n", | ||
" def __init__(self):\n", | ||
" super(SimpleModel, self).__init__()\n", | ||
" self.conv1 = torch.nn.Conv2d(1, 3, kernel_size=3)\n", | ||
" self.relu = torch.nn.ReLU()\n", | ||
" self.fc = torch.nn.Linear(3 * 26 * 26, 10)\n", | ||
"\n", | ||
" def forward(self, x):\n", | ||
" x = self.conv1(x)\n", | ||
" x = self.relu(x)\n", | ||
" x = torch.flatten(x, 1)\n", | ||
" x = self.fc(x)\n", | ||
" return x\n", | ||
"\"\"\"" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"Next, we can convert the model to tensorflow" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"import ivy\n", | ||
"\n", | ||
"TFSimpleModel = ivy.transpile(SimpleModel, source=\"torch\", target=\"tensorflow\")" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"Now we can use the model with TensorFlow" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 9, | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"data": { | ||
"text/plain": [ | ||
"TensorShape([1, 10])" | ||
] | ||
}, | ||
"execution_count": 9, | ||
"metadata": {}, | ||
"output_type": "execute_result" | ||
} | ||
], | ||
"source": [ | ||
"import tensorflow as tf\n", | ||
"\n", | ||
"tf_model = TFSimpleModel()\n", | ||
"tf_model(tf.random.normal((1, 1, 28, 28))).shape" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"We can also take advantage of TensorFlow-specific features, such as `tf.function`:" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 10, | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"data": { | ||
"text/plain": [ | ||
"TensorShape([1, 10])" | ||
] | ||
}, | ||
"execution_count": 10, | ||
"metadata": {}, | ||
"output_type": "execute_result" | ||
} | ||
], | ||
"source": [ | ||
"compiled_model = tf.function(tf_model)\n", | ||
"compiled_model(tf.random.normal((1, 1, 28, 28))).shape" | ||
] | ||
} | ||
], | ||
"metadata": { | ||
"kernelspec": { | ||
"display_name": "Python 3", | ||
"language": "python", | ||
"name": "python3" | ||
}, | ||
"language_info": { | ||
"codemirror_mode": { | ||
"name": "ipython", | ||
"version": 3 | ||
}, | ||
"file_extension": ".py", | ||
"mimetype": "text/x-python", | ||
"name": "python", | ||
"nbconvert_exporter": "python", | ||
"pygments_lexer": "ipython3", | ||
"version": "3.10.14" | ||
} | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 2 | ||
} |