Skip to content

Commit

Permalink
deploy: ae40885
Browse files Browse the repository at this point in the history
  • Loading branch information
TimRoith committed Jun 19, 2024
0 parents commit 0d7cfd1
Show file tree
Hide file tree
Showing 490 changed files with 66,356 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .buildinfo
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Sphinx build info version 1
# This file hashes the configuration used when building these files. When it is not found, a full rebuild will be done.
config: b97544b7428bf48020e81a5f5a9211ac
tags: 645f666f9bcd5a90fca523b33c5a78b7
Binary file added .doctrees/api/dynamic.doctree
Binary file not shown.
Binary file added .doctrees/api/generated/cbx.dynamics.CBO.doctree
Binary file not shown.
Binary file not shown.
Binary file added .doctrees/api/generated/cbx.dynamics.CBS.doctree
Binary file not shown.
Binary file not shown.
Binary file added .doctrees/api/generated/cbx.dynamics.PSO.doctree
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file added .doctrees/api/index.doctree
Binary file not shown.
Binary file added .doctrees/api/objectives.doctree
Binary file not shown.
Binary file added .doctrees/api/plotting.doctree
Binary file not shown.
Binary file added .doctrees/api/scheduler.doctree
Binary file not shown.
Binary file added .doctrees/api/utils.doctree
Binary file not shown.
Binary file added .doctrees/environment.pickle
Binary file not shown.
Binary file added .doctrees/examples/custom_noise.doctree
Binary file not shown.
Binary file added .doctrees/examples/low_level.doctree
Binary file not shown.
Binary file added .doctrees/examples/nns/mnist.doctree
Binary file not shown.
Binary file added .doctrees/examples/onedim_example.doctree
Binary file not shown.
Binary file added .doctrees/examples/polarcbo.doctree
Binary file not shown.
Binary file added .doctrees/examples/sampling.doctree
Binary file not shown.
Binary file added .doctrees/examples/simple_example.doctree
Binary file not shown.
Binary file added .doctrees/index.doctree
Binary file not shown.
110 changes: 110 additions & 0 deletions .doctrees/nbsphinx/examples/custom_noise.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
{
"cells": [
{
"cell_type": "markdown",
"id": "c70df536-cea0-4cdf-bd6a-601824a52768",
"metadata": {},
"source": [
"# Custom Noise CBX\n",
"\n",
"This notebook showcases, how to use a custom noise function."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "33394a7d-caa6-4f01-aa88-4dd69d5c400d",
"metadata": {},
"outputs": [],
"source": [
"from cbx.dynamics import CBO\n",
"import numpy as np"
]
},
{
"cell_type": "markdown",
"id": "ef05a88a-d000-4ea8-af21-714df66858fa",
"metadata": {},
"source": [
"## Define the custom noise function\n",
"\n",
"In this case we select that the noise should be zero"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "319270d9-01d4-49c0-8ac5-ab94bd6c327b",
"metadata": {},
"outputs": [],
"source": [
"def custom_noise(dyn):\n",
" return np.zeros(dyn.drift.shape)"
]
},
{
"cell_type": "markdown",
"id": "08dac699-4772-4409-8e25-2a398216a2f4",
"metadata": {},
"source": [
"## Define a loss function and test the method"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "aae8cac4-7a04-41a2-b9ee-a0c8c26e27da",
"metadata": {},
"outputs": [],
"source": [
"def f(x):\n",
" return np.linalg.norm(x, axis=-1)\n",
"\n",
"x0 = np.random.normal(0,1,(4,7,26))\n",
"dyn_cn = CBO(f, x=x0, noise=custom_noise, max_it=10, verbosity = 0)\n",
"x_cn = dyn_cn.optimize()"
]
},
{
"cell_type": "markdown",
"id": "762bcbf2-6861-4c21-8475-3e6b45e35fdb",
"metadata": {},
"source": [
"Using this noise is equivalent to specifying ``sigma=0`` with standard CBO. So let's test, if this is the case:"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "f1f11124-6a10-4e54-abef-4168cbfd6dbc",
"metadata": {},
"outputs": [],
"source": [
"dyn = CBO(f, x=x0, sigma=0, max_it=10, verbosity = 0)\n",
"x = dyn.optimize()\n",
"print('L-infinty Error between the custom noise solution and standard CBO with sigma=0: ' + str(np.abs(x-x_cn).max()))"
]
}
],
"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.12.3"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
143 changes: 143 additions & 0 deletions .doctrees/nbsphinx/examples/low_level.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
{
"cells": [
{
"cell_type": "markdown",
"id": "ce7c4dc3-e701-4113-b4f9-ad961e204e20",
"metadata": {},
"source": [
"# Visualiszing CBX dynamics"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "1ce71f14-a97a-475a-b027-324bc5864f09",
"metadata": {},
"outputs": [],
"source": [
"%load_ext autoreload\n",
"%autoreload 2\n",
"import numpy as np\n",
"import cbx as cbx\n",
"from cbx.dynamics import CBO, CBS\n",
"from cbx.objectives import Rastrigin\n",
"from cbx.utils.objective_handling import cbx_objective_fh\n",
"from cbx.scheduler import effective_sample_size, scheduler\n",
"from cbx.plotting import PlotDynamic, PlotDynamicHistory"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "dc027d54-35fc-44c8-bbc1-f6f8655a3863",
"metadata": {},
"outputs": [],
"source": [
"np.random.seed(420)\n",
"conf = {'alpha': 40.0,\n",
" 'dt': 0.1,\n",
" 'sigma': 1.,#8,#5.1,#8.0,\n",
" 'lamda': 1.0,\n",
" 'batch_args':{\n",
" 'batch_size':200,\n",
" 'batch_partial': False},\n",
" 'd': 2,\n",
" 'max_it': 500,\n",
" 'N': 50,\n",
" 'M': 3,\n",
" 'track_args': {'names':\n",
" ['update_norm', \n",
" 'energy','x', \n",
" 'consensus', \n",
" 'drift']}\n",
" }"
]
},
{
"cell_type": "markdown",
"id": "abd823be-8498-42c1-aad4-2b7fda0f5adf",
"metadata": {},
"source": [
"## Using the plotting functions"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "b33670f7-14a3-4176-b014-1545b249dbf8",
"metadata": {},
"outputs": [],
"source": [
"import matplotlib.pyplot as plt\n",
"from IPython import display\n",
"\n",
"fig, ax = plt.subplots(1,2, figsize=(15,5))\n",
"\n",
"f = Rastrigin()\n",
"x = cbx.utils.init_particles(shape=(conf['M'], conf['N'], conf['d']), x_min=-3., x_max = -1.) # Define the initial positions of the particles\n",
"\n",
"\n",
"dyn = CBO(f, x=x, noise='isotropic', f_dim='3D', **conf) # Define the CBO algorithm\n",
"sched = effective_sample_size(maximum=1e12)\n",
"plotter = PlotDynamic(dyn, ax=ax[0],\n",
" objective_args={'x_min':-3, 'x_max':3},\n",
" plot_consensus=True,\n",
" plot_drift=True)\n",
"plotter.init_plot()\n",
"while not dyn.terminate():\n",
" dyn.step()\n",
" sched.update(dyn)\n",
" # update energy plot\n",
" ax[1].clear()\n",
" ax[1].plot([e[0] for e in dyn.history['energy'][-30:]])\n",
" ax[1].set_title('Energy of the last 30 iterations') \n",
" ax[0].set_title('Iteration: ' + str(dyn.it)) \n",
" plotter.update(wait=0.2)\n",
" display.display(fig)\n",
" display.clear_output(wait=True)\n",
" "
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "68fc7a07-cb08-4177-9328-1354ecf0150d",
"metadata": {},
"outputs": [],
"source": [
"# import matplotlib.pyplot as plt\n",
"# plt.close('all')\n",
"# plotter = PlotDynamicHistory(\n",
"# dyn, dims=[0,1], \n",
"# objective_args={'x_min':-3, 'x_max':3, 'cmap':'viridis',\n",
"# 'num_pts':300},\n",
"# particle_args = {'s':50, 'c':'xkcd:sky', 'marker':'o'},\n",
"# drift_args = {'color':'pink', 'width':0.003},\n",
"# plot_consensus=True,\n",
"# plot_drift=True)\n",
"# plotter.run_plots(wait=0.5, freq=1,)"
]
}
],
"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.12.3"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
Loading

0 comments on commit 0d7cfd1

Please sign in to comment.