-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #33 from invrs-io/readme
Expand readme and add animation
- Loading branch information
Showing
4 changed files
with
171 additions
and
2 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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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,127 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"id": "e4f6b9c2-e829-46b1-bfd1-de01c690ef27", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"import jax\n", | ||
"import jax.numpy as jnp\n", | ||
"\n", | ||
"import matplotlib.pyplot as plt\n", | ||
"import numpy as onp\n", | ||
"from skimage import measure\n", | ||
"import gifcm\n", | ||
"\n", | ||
"import invrs_gym\n", | ||
"import invrs_opt" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"id": "340fab20-b508-4d47-8c7d-ae884282dce8", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"challenge = invrs_gym.challenges.ceviche_lightweight_waveguide_bend()\n", | ||
"\n", | ||
"# Define loss function, which also returns auxilliary quantities.\n", | ||
"def loss_fn(params):\n", | ||
" response, aux = challenge.component.response(params)\n", | ||
" loss = challenge.loss(response)\n", | ||
" distance = challenge.distance_to_target(response)\n", | ||
" metrics = challenge.metrics(response, params, aux)\n", | ||
" return loss, (response, distance, aux)\n", | ||
"\n", | ||
"value_and_grad_fn = jax.value_and_grad(loss_fn, has_aux=True)\n", | ||
"\n", | ||
"# Select an optimizer.\n", | ||
"opt = invrs_opt.density_lbfgsb(beta=4)\n", | ||
"\n", | ||
"# Generate initial parameters, and use these to initialize the optimizer state.\n", | ||
"params = challenge.component.init(jax.random.PRNGKey(0))\n", | ||
"state = opt.init(params)\n", | ||
"\n", | ||
"# Carry out the optimization.\n", | ||
"data = []\n", | ||
"for i in range(36):\n", | ||
" params = opt.params(state)\n", | ||
" (value, (response, distance, aux)), grad = value_and_grad_fn(params)\n", | ||
" state = opt.update(grad=grad, value=value, params=params, state=state)\n", | ||
" data.append((i, value, params, aux))" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"id": "6f6c6fc3-11b5-4f4e-91ab-191c123f5d06", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"# Create an animated gif showing the evolution of the waveguide bend design.\n", | ||
"anim = gifcm.AnimatedFigure(figure=plt.figure(figsize=(8, 4)))\n", | ||
"\n", | ||
"for i, _, params, aux in data:\n", | ||
" with anim.frame():\n", | ||
" # Plot fields, using some of the methods specific to the underlying ceviche model.\n", | ||
" density = challenge.component.ceviche_model.density(params.array)\n", | ||
"\n", | ||
" ax = plt.subplot(121)\n", | ||
" ax.imshow(density, cmap=\"gray\")\n", | ||
" plt.text(100, 90, f\"step {i:02}\", color=\"w\", fontsize=20)\n", | ||
" ax.axis(False)\n", | ||
" ax.set_xlim(ax.get_xlim()[::-1])\n", | ||
" ax.set_ylim(ax.get_ylim()[::-1])\n", | ||
"\n", | ||
" # Plot the field, which is a part of the `aux` returned with the challenge response.\n", | ||
" # The field will be overlaid with contours of the binarized design.\n", | ||
" field = onp.real(aux[\"fields\"])\n", | ||
" field = field[0, 0, :, :] # First wavelength, first excitation port.\n", | ||
" contours = measure.find_contours(density)\n", | ||
"\n", | ||
" ax = plt.subplot(122)\n", | ||
" im = ax.imshow(field, cmap=\"bwr\")\n", | ||
" im.set_clim([-onp.amax(field), onp.amax(field)])\n", | ||
" for c in contours:\n", | ||
" plt.plot(c[:, 1], c[:, 0], \"k\", lw=1)\n", | ||
" ax.axis(False)\n", | ||
" ax.set_xlim(ax.get_xlim()[::-1])\n", | ||
" ax.set_ylim(ax.get_ylim()[::-1])\n", | ||
"\n", | ||
"anim.save_gif(\"waveguide_bend.gif\", duration=200)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"id": "f75622f1-b207-4ba1-aeec-2c989985b1eb", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [] | ||
} | ||
], | ||
"metadata": { | ||
"kernelspec": { | ||
"display_name": "Python 3 (ipykernel)", | ||
"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.12" | ||
} | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 5 | ||
} |
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 |
---|---|---|
|
@@ -5,3 +5,5 @@ | |
|
||
__version__ = "v0.0.0" | ||
__author__ = "Martin F. Schubert <[email protected]>" | ||
|
||
from invrs_gym import challenges as challenges |