Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added spiking to feedback notebooks #49

Merged
merged 1 commit into from
Dec 19, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ Release History
- Added notebook examples featuring oscillators.
(`#46 <https://github.com/nengo/nengo-fpga/pull/46>`__)

- Added spiking to oscillator notebook examples.
(`#49 <https://github.com/nengo/nengo-fpga/pull/49>`__)

**Changed**

- Compatibility changes for Nengo 3.0.0.
Expand Down
71 changes: 65 additions & 6 deletions docs/examples/notebooks/03-integrator.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"## Step 8: Plot the Results:"
"## Step 8: Plot the Results"
]
},
{
Expand All @@ -211,17 +211,76 @@
"metadata": {},
"outputs": [],
"source": [
"plt.figure(figsize=(8, 5))\n",
"plt.plot(sim.trange(), sim.data[input_p], 'k--', label=\"Input\")\n",
"plt.plot(sim.trange(), sim.data[output_p], label=\"Integrator output\")\n",
"plt.legend();"
"def plot_results():\n",
" plt.figure(figsize=(8, 5))\n",
" plt.plot(sim.trange(), sim.data[input_p], 'k--', label=\"Input\")\n",
" plt.plot(sim.trange(), sim.data[output_p], label=\"Integrator output\")\n",
" plot_ideal()\n",
" plt.legend();\n",
"\n",
"\n",
"def plot_ideal():\n",
" # Obtain the input function data from the input_node\n",
" input_t = list(input_node.output.data.keys())\n",
" input_v = list(input_node.output.data.values())\n",
"\n",
" # Construct the ideal output (assumes 1D signal)\n",
" values = [[0]]\n",
" input_t += [sim.trange()[-1]]\n",
" for i, v in enumerate(input_v):\n",
" values += [values[-1] + v * (input_t[i + 1] - input_t[i])]\n",
"\n",
" # Make the plot\n",
" plt.plot(input_t, values, label=\"Ideal\")\n",
"\n",
"\n",
"plot_results()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The plot above shows the neurons effectively integrating the input signal. Because of the implementation in neurons, the integration is not perfect (i.e., there will be some drift). Run the simulation several times to get a sense of the kinds of drift you might expect."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Step 9: Spiking Neurons\n",
"\n",
"The plots above demonstrate the results of an integrator network implemented with non-spiking rectified linear neurons. The network can also be simulated using spiking neurons to illustrate the similarities and differences between a spiking and a non-spiking network.\n",
"\n",
"Below, we configure the FPGA neural ensemble to use spiking neurons, run the simulation, and plot the results."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"with model:\n",
" fpga_ens.ensemble.neuron_type = nengo.SpikingRectifiedLinear()\n",
"\n",
"with nengo_fpga.Simulator(model) as sim:\n",
" sim.run(6)\n",
"plot_results()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The plot above shows that while the output differs from the non-spiking simulation (because the network parameters are regenerated when a new simulation is created), the performance of the spiking integrator still conforms to the expected output of an integrator."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The plot above shows the neurons effectively integrating the input signal. Because of the implementation in neurons, the integration is not perfect (i.e., there will be some drift). Run the simulation several times to get a sense of the kinds of drift you might expect.\n",
"## Step 10: Experiment!\n",
"\n",
"Try playing around with the number of neurons in the FPGA ensemble as well as the synaptic time constant (`tau`) to see how it effects performance (e.g., observe how changing these numbers affect the drift)!"
]
Expand Down
75 changes: 65 additions & 10 deletions docs/examples/notebooks/04-oscillator.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"## Step 8: Plot the Results:"
"## Step 8: Plot the Results"
]
},
{
Expand All @@ -225,11 +225,15 @@
"metadata": {},
"outputs": [],
"source": [
"plt.figure(figsize=(8, 5))\n",
"plt.plot(sim.trange(), sim.data[input_p][:, 0], 'k--')\n",
"plt.plot(sim.trange(), sim.data[output_p])\n",
"plt.legend(['Kick', '$x_0$', '$x_1$'], loc='upper right')\n",
"plt.xlabel('Time (s)');"
"def plot_results():\n",
" plt.figure(figsize=(8, 5))\n",
" plt.plot(sim.trange(), sim.data[input_p][:, 0], 'k--')\n",
" plt.plot(sim.trange(), sim.data[output_p])\n",
" plt.legend(['Kick', '$x_0$', '$x_1$'], loc='upper right')\n",
" plt.xlabel('Time (s)');\n",
"\n",
"\n",
"plot_results()"
]
},
{
Expand All @@ -247,16 +251,67 @@
"metadata": {},
"outputs": [],
"source": [
"fig, ax, anim = make_anim_simple_osc(sim.data[output_p][:, 0],\n",
" sim.data[output_p][:, 1])\n",
"HTML(anim.to_html5_video())"
"def make_anim():\n",
" _, _, anim = make_anim_simple_osc(sim.data[output_p][:, 0],\n",
" sim.data[output_p][:, 1])\n",
" return anim\n",
"\n",
"\n",
"HTML(make_anim().to_html5_video())"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Step 9: Spiking Neurons\n",
"\n",
"The plots above demonstrate the results of a simple oscillator implemented in a network of non-spiking rectified linear neurons. The network can also be simulated using spiking neurons to illustrate the similarities and differences between a spiking and a non-spiking network.\n",
"\n",
"Below, we configure the FPGA neural ensemble to use spiking neurons, run the simulation, and plot the results."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"with model:\n",
" fpga_ens.ensemble.neuron_type = nengo.SpikingRectifiedLinear()\n",
"\n",
"with nengo_fpga.Simulator(model) as sim:\n",
" sim.run(5)\n",
"plot_results()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"HTML(make_anim().to_html5_video())"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The plots above show that with spiking neurons, the output of the network is, expectedly, more noisy (less precise) than the results of the non-spiking network. However, despite this, the oscillator network in its current configuration is stable even with the spikes adding additional noise into the system.\n",
"\n",
"It should be noted that the output of the spiking network might differ from the output of the non-spiking network because the network parameters are regenerated (randomized) when a new Nengo simulation is created."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Try playing around with the number of neurons in the FPGA ensemble as well as the synaptic time constant (`tau`) to see how it effects performance (e.g., observe how changing these numbers affect the stability of the oscillator)! Additionally, modify the oscillation frequency (try making it negative) to its impact on the output. You can also change the simulation time and see how long the oscillator is stable for!"
"## Step 10: Experiment!\n",
"\n",
"Try playing around with the number of neurons in the FPGA ensemble as well as the synaptic time constant (`tau`) to see how it effects performance (e.g., observe how changing these numbers affect the stability of the oscillator)! Additionally, modify the oscillation frequency (try making it negative) to its impact on the output. You can also change the simulation time and see how long the oscillator is stable for!\n",
"\n",
"Perform these experiments for both the non-spiking and spiking networks, and observe how the additional noise introduced by the spikes affect the performance of the network with relation to the various network and oscillator parameters."
]
}
],
Expand Down
81 changes: 68 additions & 13 deletions docs/examples/notebooks/05-controlled-oscillator.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"## Step 8: Plot the Results:"
"## Step 8: Plot the Results"
]
},
{
Expand All @@ -278,11 +278,15 @@
"metadata": {},
"outputs": [],
"source": [
"plt.figure(figsize=(8, 5))\n",
"plt.plot(sim.trange(), sim.data[freq_p], 'k--')\n",
"plt.plot(sim.trange(), sim.data[output_p])\n",
"plt.legend(['Frequency (Hz)', '$x_0$', '$x_1$', '$x_2$'], loc='upper right')\n",
"plt.xlabel('Time (s)');"
"def plot_results():\n",
" plt.figure(figsize=(8, 5))\n",
" plt.plot(sim.trange(), sim.data[freq_p], 'k--')\n",
" plt.plot(sim.trange(), sim.data[output_p])\n",
" plt.legend(['Frequency (Hz)', '$x_0$', '$x_1$', '$x_2$'], loc='upper right')\n",
" plt.xlabel('Time (s)');\n",
"\n",
"\n",
"plot_results()"
]
},
{
Expand All @@ -302,19 +306,70 @@
"metadata": {},
"outputs": [],
"source": [
"fig, ax, ax2, anim = make_anim_controlled_osc(sim.data[output_p][:, 0],\n",
" sim.data[output_p][:, 1],\n",
" sim.data[freq_p])\n",
"ax.set_title('Oscillator')\n",
"ax2.set_title('Speed (Hz)')\n",
"HTML(anim.to_html5_video())"
"def make_anim():\n",
" _, ax, ax2, anim = make_anim_controlled_osc(sim.data[output_p][:, 0],\n",
" sim.data[output_p][:, 1],\n",
" sim.data[freq_p])\n",
" ax.set_title('Oscillator')\n",
" ax2.set_title('Speed (Hz)')\n",
" return anim\n",
"\n",
"\n",
"HTML(make_anim().to_html5_video())"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Step 9: Spiking Neurons\n",
"\n",
"The plots above demonstrate the results of a controlled oscillator implemented in a network of non-spiking rectified linear neurons. The network can also be simulated using spiking neurons to illustrate the similarities and differences between a spiking and a non-spiking network.\n",
"\n",
"Below, we configure the FPGA neural ensemble to use spiking neurons, run the simulation, and plot the results."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"with model:\n",
" fpga_ens.ensemble.neuron_type = nengo.SpikingRectifiedLinear()\n",
"\n",
"with nengo_fpga.Simulator(model) as sim:\n",
" sim.run(10)\n",
"plot_results()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"HTML(make_anim().to_html5_video())"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The plots above show that with spiking neurons, the output of the network is, expectedly, more noisy (less precise) than the results of the non-spiking network. However, despite this, the oscillator network in its current configuration is stable even with the spikes adding additional noise into the system.\n",
"\n",
"It should be noted that the output of the spiking network differs from the output of the non-spiking network because the network parameters are regenerated (randomized) when a new Nengo simulation is created."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Try playing around with the number of neurons in the FPGA ensemble as well as the synaptic time constant (`tau`) to see how it effects performance (e.g., observe how changing these numbers affect the stability of the oscillator)! Additionally, try modifying the oscillation frequency input and make the appropriate changes to the scaling factor to see if they have the desired effect. You can also change the simulation time and see how long the oscillator is stable for!"
"## Step 10: Experiment!\n",
"\n",
"Try playing around with the number of neurons in the FPGA ensemble as well as the synaptic time constant (`tau`) to see how it effects performance (e.g., observe how changing these numbers affect the stability of the oscillator)! Additionally, try modifying the oscillation frequency input and make the appropriate changes to the scaling factor to see if they have the desired effect. You can also change the simulation time and see how long the oscillator is stable for!\n",
"\n",
"Perform these experiments for both the non-spiking and spiking networks, and observe how the additional noise introduced by the spikes affect the performance of the network with relation to the various network and oscillator parameters."
]
}
],
Expand Down
Loading