-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
NengoBones
committed
Nov 15, 2023
1 parent
1620e0c
commit f5124c1
Showing
262 changed files
with
60,167 additions
and
0 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
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: e50ace2b05ef2b4cc5db2904c86d8b23 | ||
tags: 645f666f9bcd5a90fca523b33c5a78b7 |
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.
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
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,160 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"# Implementing a custom module\n", | ||
"\n", | ||
"This example demonstrates how custom SPA modules can be created that can take advantage\n", | ||
"of all the features of the SPA syntax. We will adapt the `InputGatedMemory` from\n", | ||
"`nengo`." | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 1, | ||
"metadata": { | ||
"execution": { | ||
"iopub.execute_input": "2023-11-15T20:15:28.378489Z", | ||
"iopub.status.busy": "2023-11-15T20:15:28.378054Z", | ||
"iopub.status.idle": "2023-11-15T20:15:28.814483Z", | ||
"shell.execute_reply": "2023-11-15T20:15:28.813940Z" | ||
} | ||
}, | ||
"outputs": [], | ||
"source": [ | ||
"import nengo\n", | ||
"import nengo_spa as spa" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"Implementing a SPA module requires a few steps:\n", | ||
"\n", | ||
"1. Implement a class inheriting from `spa.Network`.\n", | ||
"2. Use `VocabularyOrDimParam` to declare class variables for storing vocublary\n", | ||
"parameters. This will allow the usage of integer dimensions instead of vocabularies\n", | ||
"without any further additions.\n", | ||
"3. Declare inputs and outputs with their respective vocabularies.\n", | ||
"\n", | ||
"Not that parameters in SPA modules should usually be defined as readonly because\n", | ||
"changing them will usually not update the network accordingly." | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 2, | ||
"metadata": { | ||
"execution": { | ||
"iopub.execute_input": "2023-11-15T20:15:28.817439Z", | ||
"iopub.status.busy": "2023-11-15T20:15:28.816988Z", | ||
"iopub.status.idle": "2023-11-15T20:15:28.822749Z", | ||
"shell.execute_reply": "2023-11-15T20:15:28.822243Z" | ||
} | ||
}, | ||
"outputs": [], | ||
"source": [ | ||
"class GatedMemory(spa.Network):\n", | ||
" # The vocabulary parameter.\n", | ||
" vocab = spa.vocabulary.VocabularyOrDimParam(\"vocab\", default=None, readonly=True)\n", | ||
" # The number of neurons per dimensions.\n", | ||
" neurons_per_dimension = nengo.params.IntParam(\n", | ||
" \"neurons_per_dimension\", default=200, low=1, readonly=True\n", | ||
" )\n", | ||
"\n", | ||
" # Arguments assigned to parameters should be assigned\n", | ||
" # nengo.params.Default as default value. This makes sure they work\n", | ||
" # properly with nengo.Config. It is a good idea to pass on the keyword\n", | ||
" # arguments **kwargs to the spa.Network constructor to allow the user to\n", | ||
" # set the network label etc.\n", | ||
" def __init__(\n", | ||
" self,\n", | ||
" vocab=nengo.params.Default,\n", | ||
" neurons_per_dimension=nengo.params.Default,\n", | ||
" **kwargs,\n", | ||
" ):\n", | ||
" super(GatedMemory, self).__init__(**kwargs)\n", | ||
"\n", | ||
" # Assign parameter values\n", | ||
" # If vocab is an integer dimension, the appropriate Vocabulary\n", | ||
" # instance will assigned to self.vocab.\n", | ||
" self.vocab = vocab\n", | ||
" self.neurons_per_dimension = neurons_per_dimension\n", | ||
"\n", | ||
" # Construct the network\n", | ||
" with self:\n", | ||
" self.mem = nengo.networks.InputGatedMemory(\n", | ||
" self.neurons_per_dimension, self.vocab.dimensions\n", | ||
" )\n", | ||
"\n", | ||
" # Assign inputs to root object for easier referencing\n", | ||
" self.input = self.mem.input\n", | ||
" self.input_gate = self.mem.gate\n", | ||
" self.input_reset = self.mem.reset\n", | ||
" self.output = self.mem.output\n", | ||
"\n", | ||
" # Declare inputs and outputs\n", | ||
" # Use None as vocabulary for scalar inputs/outputs\n", | ||
" self.declare_input(self.input, self.vocab)\n", | ||
" self.declare_input(self.input_gate, None)\n", | ||
" self.declare_input(self.input_reset, None)\n", | ||
" self.declare_output(self.output, self.vocab)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"We can then use our new module as we would any other module." | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 3, | ||
"metadata": { | ||
"execution": { | ||
"iopub.execute_input": "2023-11-15T20:15:28.825047Z", | ||
"iopub.status.busy": "2023-11-15T20:15:28.824703Z", | ||
"iopub.status.idle": "2023-11-15T20:15:28.909594Z", | ||
"shell.execute_reply": "2023-11-15T20:15:28.909061Z" | ||
} | ||
}, | ||
"outputs": [], | ||
"source": [ | ||
"dimensions = 32\n", | ||
"\n", | ||
"with spa.Network() as model:\n", | ||
" # The module can be configured\n", | ||
" model.config[GatedMemory].neurons_per_dimension = 150\n", | ||
"\n", | ||
" spa_in = spa.Transcode(\"OKAY\", output_vocab=dimensions)\n", | ||
" gate_in = nengo.Node(lambda t: 1 if t < 0.1 else 0)\n", | ||
"\n", | ||
" g_mem = GatedMemory(dimensions)\n", | ||
"\n", | ||
" # It can be in routing rules\n", | ||
" spa_in >> g_mem\n", | ||
" gate_in >> g_mem.input_gate" | ||
] | ||
} | ||
], | ||
"metadata": { | ||
"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.18" | ||
} | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 1 | ||
} |
Oops, something went wrong.