-
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.
- Loading branch information
Showing
3 changed files
with
109 additions
and
5 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,107 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "markdown", | ||
"source": [ | ||
"Turn your Ivy code into an efficient fully-functional graph, removing wrappers and unused parts of the code." | ||
], | ||
"metadata": { | ||
"collapsed": false | ||
} | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"source": [ | ||
"⚠️ 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 ⬇️\n", | ||
"\n", | ||
"If you want to run the notebook locally but don't have Ivy installed just yet, you can check out the [Get Started section of the docs.](https://unify.ai/docs/ivy/overview/get_started.html)" | ||
], | ||
"metadata": { | ||
"collapsed": false | ||
} | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"outputs": [], | ||
"source": [ | ||
"!pip install ivy" | ||
], | ||
"metadata": { | ||
"collapsed": false | ||
} | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"source": [ | ||
"Firstly, let's pick up where we left off in the [last notebook](), with our unified `normalize` function:" | ||
], | ||
"metadata": { | ||
"collapsed": false | ||
} | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 2, | ||
"outputs": [], | ||
"source": [ | ||
"import torch\n", | ||
"import ivy\n", | ||
"\n", | ||
"def normalize(x):\n", | ||
" mean = torch.mean(x)\n", | ||
" std = torch.std(x)\n", | ||
" return torch.div(torch.sub(x, mean), std)\n", | ||
"\n", | ||
"normalize = ivy.unify(normalize, source=\"torch\")\n", | ||
"\n", | ||
"# set ivy's backend to jax\n", | ||
"ivy.set_backend(\"jax\")\n", | ||
"\n", | ||
"# Import jax\n", | ||
"import jax\n", | ||
"\n", | ||
"# create random jax arrays for testing\n", | ||
"key = jax.random.PRNGKey(42)\n", | ||
"x = jax.random.uniform(key, shape=(10,))\n", | ||
"\n", | ||
"normalize(x)\n", | ||
"\n", | ||
"ivy.set_backend(\"jax\")\n", | ||
"comp = ivy.compile(normalize) # compiles to jax, due to ivy.set_backend\n", | ||
"\n", | ||
"comp(x)\n", | ||
"\n", | ||
"%%timeit\n", | ||
"normalize(x)\n", | ||
"\n", | ||
"%%timeit\n", | ||
"comp(x)" | ||
], | ||
"metadata": { | ||
"collapsed": false | ||
} | ||
} | ||
], | ||
"metadata": { | ||
"kernelspec": { | ||
"display_name": "Python 3", | ||
"language": "python", | ||
"name": "python3" | ||
}, | ||
"language_info": { | ||
"codemirror_mode": { | ||
"name": "ipython", | ||
"version": 2 | ||
}, | ||
"file_extension": ".py", | ||
"mimetype": "text/x-python", | ||
"name": "python", | ||
"nbconvert_exporter": "python", | ||
"pygments_lexer": "ipython2", | ||
"version": "2.7.6" | ||
} | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 0 | ||
} |
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