Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@fehiepsi
Created May 30, 2019 23:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fehiepsi/c72a90a493b68d87a63606db589d38c2 to your computer and use it in GitHub Desktop.
Save fehiepsi/c72a90a493b68d87a63606db589d38c2 to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import jax.numpy as np\n",
"from jax import jit, grad, lax\n",
"from jax.config import config; config.update(\"jax_platform_name\", \"gpu\")\n",
"\n",
"import numpyro.distributions as dist\n",
"from numpyro.examples.datasets import SP500, load_dataset\n",
"from numpyro.handlers import sample\n",
"from numpyro.hmc_util import initialize_model\n",
"from numpyro.mcmc import hmc"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"_, fetch = load_dataset(SP500, shuffle=False)\n",
"dates, returns = fetch()"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"def potential_fn(x):\n",
" return ((x - returns) ** 2).mean()\n",
"\n",
"@jit\n",
"def loop(x):\n",
" def body_fn(i, x):\n",
" return x + 0.01 * grad(potential_fn)(x)\n",
"\n",
" return lax.fori_loop(0, 1000, body_fn, x)"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"36.1 ms ± 2.84 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)\n"
]
}
],
"source": [
"%%timeit\n",
"loop(np.array(0.))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Here is the result in CPU."
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"161 µs ± 1.81 µs per loop (mean ± std. dev. of 7 runs, 10000 loops each)\n"
]
}
],
"source": [
"%%timeit\n",
"loop(np.array(0.))"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python (pydata)",
"language": "python",
"name": "pydata"
},
"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.6.8"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment