Created
March 28, 2022 00:44
-
-
Save davidliyutong/59b303cb44f2b5104933d8477cc04ace to your computer and use it in GitHub Desktop.
Jax Usage Demo
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
{ | |
"cells": [ | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"import jax\n", | |
"import jax.numpy as jnp" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"class BaseLayer:\n", | |
" _parameters = {}\n", | |
"\n", | |
"\n", | |
"class Dense2Layer(BaseLayer):\n", | |
" def __init__(self, input_dims: int, output_dims: int):\n", | |
" super().__init__()\n", | |
" assert (isinstance(input_dims, int) and isinstance(output_dims, int))\n", | |
" self._parameters['u'] = jax.random.normal(jax.random.PRNGKey(0), (input_dims, output_dims))\n", | |
" self._parameters['v'] = jax.random.normal(jax.random.PRNGKey(0), (input_dims, output_dims))\n", | |
"\n", | |
" self._parameters['b'] = jax.random.normal(jax.random.PRNGKey(0), (output_dims, ))\n", | |
" self._parameters['x'] = None\n", | |
" self.vjp_fn = None\n", | |
"\n", | |
" @classmethod\n", | |
" def forward_fn(cls, x, u, v, b):\n", | |
" return x**2 @ u + x @ v + b\n", | |
"\n", | |
" def __call__(self, x: jax.numpy.DeviceArray):\n", | |
" self._parameters['x'] = x\n", | |
" out, self.vjp_fn = jax.vjp(self.forward_fn, layer._parameters['x'], layer._parameters['u'], layer._parameters['v'],\n", | |
" layer._parameters['b'])\n", | |
" return out\n", | |
"\n", | |
"\n", | |
"class MSELoss:\n", | |
" def __call__(self, x, y):\n", | |
" return jnp.mean((x - y)**2)\n" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"layer = Dense2Layer(20, 3)\n", | |
"loss_fn = MSELoss()" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"x = jax.random.normal(jax.random.PRNGKey(0), (2, 20))\n", | |
"y = jax.numpy.array([[1, 1, 1], [0, 0, 0]], dtype=jax.numpy.float32)\n", | |
"pred = layer(x)\n", | |
"loss = loss_fn(pred, y)\n", | |
"loss_grad = jax.grad(loss_fn, argnums=0)(pred, y)\n", | |
"print(loss)\n", | |
"print(loss_grad)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"print(layer.vjp_fn(loss_grad)[0].shape)\n", | |
"print(layer.vjp_fn(loss_grad)[1].shape)\n", | |
"print(layer.vjp_fn(loss_grad)[2].shape)\n", | |
"print(layer.vjp_fn(loss_grad)[3].shape)" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [] | |
} | |
], | |
"metadata": { | |
"interpreter": { | |
"hash": "aedd5e66c633617a2b6367c11c0534f47dbb53f58d311e2fdabbf5bcacebad6a" | |
}, | |
"kernelspec": { | |
"display_name": "Python 3.8.8 ('default')", | |
"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.8.8" | |
}, | |
"orig_nbformat": 4 | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 2 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment