From b23fb4c4dc2026dfee3b943922e8811582dca3a6 Mon Sep 17 00:00:00 2001 From: chaoming Date: Sat, 14 May 2022 14:15:36 +0800 Subject: [PATCH 1/2] improve oo-to-function transformation speed --- brainpy/math/controls.py | 16 ++++++++-------- brainpy/math/jit.py | 2 +- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/brainpy/math/controls.py b/brainpy/math/controls.py index 5f86b0d04..6046878c1 100644 --- a/brainpy/math/controls.py +++ b/brainpy/math/controls.py @@ -169,9 +169,9 @@ def call(xs=None, length=None): turn_off_global_jit() except UnexpectedTracerError as e: turn_off_global_jit() - for v, d in zip(dyn_vars, init_values): v.value = d + for v, d in zip(dyn_vars, init_values): v._value = d raise errors.JaxTracerError(variables=dyn_vars) from e - for v, d in zip(dyn_vars, dyn_values): v.value = d + for v, d in zip(dyn_vars, dyn_values): v._value = d return tree_unflatten(tree, out_values), results else: @@ -189,7 +189,7 @@ def call(xs): turn_off_global_jit() for v, d in zip(dyn_vars, init_values): v._value = d raise e - for v, d in zip(dyn_vars, dyn_values): v.value = d + for v, d in zip(dyn_vars, dyn_values): v._value = d return tree_unflatten(tree, out_values) return call @@ -271,7 +271,7 @@ def call(x=None): turn_off_global_jit() for v, d in zip(dyn_vars, dyn_init): v._value = d raise e - for v, d in zip(dyn_vars, dyn_values): v.value = d + for v, d in zip(dyn_vars, dyn_values): v._value = d return call @@ -359,7 +359,7 @@ def call(pred, x=None): turn_off_global_jit() for v, d in zip(dyn_vars, old_values): v._value = d raise e - for v, d in zip(dyn_vars, dyn_values): v.value = d + for v, d in zip(dyn_vars, dyn_values): v._value = d return res else: @@ -477,7 +477,7 @@ def _false_fun(op): turn_off_global_jit() for v, d in zip(dyn_vars, old_values): v._value = d raise e - for v, d in zip(dyn_vars, dyn_values): v.value = d + for v, d in zip(dyn_vars, dyn_values): v._value = d else: turn_on_global_jit() res = lax.cond(pred, true_fun, false_fun, operands) @@ -663,7 +663,7 @@ def fun2scan(dyn_vals, x): turn_off_global_jit() for v, d in zip(dyn_vars, init_vals): v._value = d raise e - for v, d in zip(dyn_vars, dyn_vals): v.value = d + for v, d in zip(dyn_vars, dyn_vals): v._value = d return out_vals @@ -729,4 +729,4 @@ def _cond_fun(op): turn_off_global_jit() for v, d in zip(dyn_vars, dyn_init): v._value = d raise e - for v, d in zip(dyn_vars, dyn_values): v.value = d + for v, d in zip(dyn_vars, dyn_values): v._value = d diff --git a/brainpy/math/jit.py b/brainpy/math/jit.py index 5bde413e6..3242fda07 100644 --- a/brainpy/math/jit.py +++ b/brainpy/math/jit.py @@ -56,7 +56,7 @@ def call(*args, **kwargs): turn_off_global_jit() for key, v in vars.items(): v._value = variable_data[key] raise e - vars.assign(changes) + for key, v in vars.items(): v._value = changes[key] return out return change_func_name(name=f_name, f=call) if f_name else call From 44eac4a3f5fecdff8115dec06ceade38bd089436 Mon Sep 17 00:00:00 2001 From: chaoming Date: Sun, 15 May 2022 13:34:38 +0800 Subject: [PATCH 2/2] update docs and examples --- brainpy/dyn/base.py | 10 +- docs/apis/dyn.rst | 1 + docs/auto_generater.py | 12 +- docs/index.rst | 2 +- .../fde_numerical_solvers.ipynb | 4 +- .../initializing_connection_weights.ipynb | 554 ------------------ .../Wang_2002_decision_making_spiking.py | 1 + 7 files changed, 21 insertions(+), 563 deletions(-) delete mode 100644 docs/tutorial_toolbox/initializing_connection_weights.ipynb diff --git a/brainpy/dyn/base.py b/brainpy/dyn/base.py index d48c773fb..ff94899c8 100644 --- a/brainpy/dyn/base.py +++ b/brainpy/dyn/base.py @@ -54,13 +54,13 @@ class DynamicalSystem(Base): """Global delay variables. Useful when the same target variable is used in multiple mappings.""" - global_delay_vars: Dict[str, bm.LengthDelay] = dict() + global_delay_vars: Dict[str, bm.LengthDelay] = Collector() def __init__(self, name=None): super(DynamicalSystem, self).__init__(name=name) # local delay variables - self.local_delay_vars: Dict[str, bm.LengthDelay] = dict() + self.local_delay_vars: Dict[str, bm.LengthDelay] = Collector() def __repr__(self): return f'{self.__class__.__name__}(name={self.name})' @@ -334,15 +334,17 @@ def reset(self): @classmethod def has(cls, **children_cls): - """ + """The aggressive operation to gather master and children classes. Parameters ---------- children_cls + The children classes. Returns ------- - + wrapper: ContainerWrapper + A wrapper which has master and its children classes. """ return ContainerWrapper(master=cls, **children_cls) diff --git a/docs/apis/dyn.rst b/docs/apis/dyn.rst index 2d3447c6e..922cfdcbe 100644 --- a/docs/apis/dyn.rst +++ b/docs/apis/dyn.rst @@ -13,4 +13,5 @@ auto/dyn/neurons auto/dyn/synapses auto/dyn/rates + auto/dyn/others auto/dyn/runners \ No newline at end of file diff --git a/docs/auto_generater.py b/docs/auto_generater.py index 4e7d2337f..c1d6090e1 100644 --- a/docs/auto_generater.py +++ b/docs/auto_generater.py @@ -255,7 +255,6 @@ def generate_dyn_docs(path='apis/auto/dyn/'): module_and_name = [ ('biological_models', 'Biological Models'), ('fractional_models', 'Fractional-order Models'), - ('input_models', 'Input Models'), ('reduced_models', 'Reduced Models'), ] write_submodules(module_name='brainpy.dyn.neurons', @@ -278,7 +277,6 @@ def generate_dyn_docs(path='apis/auto/dyn/'): module_and_name = [ ('populations', 'Population Models'), ('couplings', 'Coupling Models'), - ('noises', 'Noise Models'), ] write_submodules(module_name='brainpy.dyn.rates', filename=os.path.join(path, 'rates.rst'), @@ -286,6 +284,16 @@ def generate_dyn_docs(path='apis/auto/dyn/'): submodule_names=[a[0] for a in module_and_name], section_names=[a[1] for a in module_and_name]) + module_and_name = [ + ('noises', 'Noise Models'), + ('inputs', 'Input Models'), + ] + write_submodules(module_name='brainpy.dyn.others', + filename=os.path.join(path, 'others.rst'), + header='Helper Models', + submodule_names=[a[0] for a in module_and_name], + section_names=[a[1] for a in module_and_name]) + write_module(module_name='brainpy.dyn.runners', filename=os.path.join(path, 'runners.rst'), header='Runners') diff --git a/docs/index.rst b/docs/index.rst index b3f3f19fd..0973c3f8e 100644 --- a/docs/index.rst +++ b/docs/index.rst @@ -59,8 +59,8 @@ The code of BrainPy is open-sourced at GitHub: tutorial_toolbox/ode_numerical_solvers tutorial_toolbox/sde_numerical_solvers - tutorial_toolbox/dde_numerical_solvers tutorial_toolbox/fde_numerical_solvers + tutorial_toolbox/dde_numerical_solvers tutorial_toolbox/joint_equations tutorial_toolbox/synaptic_connections tutorial_toolbox/synaptic_weights diff --git a/docs/tutorial_toolbox/fde_numerical_solvers.ipynb b/docs/tutorial_toolbox/fde_numerical_solvers.ipynb index dc2492958..1f6bdcf09 100644 --- a/docs/tutorial_toolbox/fde_numerical_solvers.ipynb +++ b/docs/tutorial_toolbox/fde_numerical_solvers.ipynb @@ -46,7 +46,7 @@ "source": [ "Factional differential equations have several definitions. It can be defined in a variety of different ways that do often do not all lead to the same result even for smooth functions. In neuroscience, we usually use the following two definitions:\n", "\n", - "- Riemann–Liouville fractional derivative\n", + "- Grünwald-Letnikov derivative\n", "- Caputo fractional derivative\n", "\n", "See [Fractional calculus - Wikipedia](https://en.wikipedia.org/wiki/Fractional_calculus) for more details." @@ -421,7 +421,7 @@ { "cell_type": "markdown", "source": [ - "## Methods for Riemann–Liouville FDEs" + "## Methods for Grünwald-Letnikov FDEs" ], "metadata": { "collapsed": false, diff --git a/docs/tutorial_toolbox/initializing_connection_weights.ipynb b/docs/tutorial_toolbox/initializing_connection_weights.ipynb deleted file mode 100644 index 9152fb35e..000000000 --- a/docs/tutorial_toolbox/initializing_connection_weights.ipynb +++ /dev/null @@ -1,554 +0,0 @@ -{ - "cells": [ - { - "cell_type": "markdown", - "id": "d67d4fa4", - "metadata": {}, - "source": [ - "# Connection Weight Initialization" - ] - }, - { - "cell_type": "markdown", - "id": "c69f432f", - "metadata": {}, - "source": [ - "@[Xiaoyu Chen](mailto:c-xy17@tsinghua.org.cn)" - ] - }, - { - "cell_type": "markdown", - "id": "d579e1e5", - "metadata": {}, - "source": [ - "After a neural network is constructed, the connection weights of different layers should be initialized. The ``brainpy.training.initialize`` module provides some built-in initializers. Besides, users can customize the initializers of their own. Here we will briefly talk about how to use these built-in initializers and how to create customized initializers." - ] - }, - { - "cell_type": "markdown", - "id": "2f48218a", - "metadata": {}, - "source": [ - "## General Structure of Initializers" - ] - }, - { - "cell_type": "markdown", - "id": "804f757f", - "metadata": {}, - "source": [ - "### Base Class: bp.init.Initializer" - ] - }, - { - "cell_type": "markdown", - "id": "456657ed", - "metadata": {}, - "source": [ - "Initializers initialize the coonection weights between two groups of neurons (though they can refer to the same group). The base class of initializers are [`brainpy.training.initialize.Initializer`](../apis/auto/training/initialize.rst), which can be accessed by the shortcut `bp.init`. All initializers, built-in or costumized, should inherit the `Initializer` class. \n", - "\n", - "Weight initialization is implemented in the `__call__` function, so that it can be realized automatically when the initializer is called. The `__call__` function has a `shape` parameter that has different meanings in the following two superclasses and returns a matrix containing initialized weights." - ] - }, - { - "cell_type": "code", - "execution_count": 2, - "id": "bde31457", - "metadata": {}, - "outputs": [], - "source": [ - "import brainpy as bp\n", - "# bp.training.initialize can be accessed by bp.init\n", - "\n", - "bp.math.set_platform('cpu')" - ] - }, - { - "cell_type": "markdown", - "id": "2edfba91", - "metadata": {}, - "source": [ - "### Superclass 1: bp.init.InterLayerInitializer" - ] - }, - { - "cell_type": "markdown", - "id": "00908305", - "metadata": {}, - "source": [ - "The [`InterLayerInitializer`](../apis/auto/training/initialize.rst) is an abstract subclass of `Initializer`. Subclasses of `InterLayerInitializer` initialize the weights between two fully connected layers. The `shape` parameter of the `__call__` function should be a 2-element tuple $(m, n)$, which refers to the number of presynaptic neurons $m$ and of postsynaptic neurons $n$. The output of the `__call__` function is a `bp.math.ndarray` with the shape of $(m, n)$, where the value at $(i, j)$ is the initialized weight of the presynaptic neuron $i$ to postsynpatic neuron $j$." - ] - }, - { - "cell_type": "markdown", - "id": "bd8fb450", - "metadata": {}, - "source": [ - "### Superclass 2: bp.init.IntraLayerInitializer" - ] - }, - { - "cell_type": "markdown", - "id": "08c79681", - "metadata": {}, - "source": [ - "The [`IntraLayerInitializer`](../apis/auto/training/initialize.rst) is also an abstract subclass of `Initializer`. Subclasses of `InterLayerInitializer` initialize the weights within a layer. The `shape` parameter of the `__call__` function refers to the the structure of the neural population $(n_1, n_2, ..., n_d)$. The `__call__` function returns a 2-D `bp.math.ndarray` with the shape of $(\\prod_{k=1}^d n_k, \\prod_{k=1}^d n_k)$. In the 2-D array, the value at $(i, j)$ is the initialized weight of neuron $i$ to neuron $j$ of the flattened neural sequence." - ] - }, - { - "cell_type": "markdown", - "id": "be9a3ca9", - "metadata": {}, - "source": [ - "## Built-In Regular Initializers" - ] - }, - { - "cell_type": "markdown", - "id": "8d0f9f82", - "metadata": {}, - "source": [ - "Regular initializers all belong to `InterLayerInitializer` and initialize the connection weights between two layers with a regular pattern. There are `ZeroInit`, `OneInit`, and `Identity` initializers in built-in regular initializers. Here we show how to use the `OneInit` initializer. The remaining two classes are used in a similar way. See [Weight Initialization](../apis/auto/training/initialize.rst) for more details." - ] - }, - { - "cell_type": "markdown", - "id": "6e528f29", - "metadata": {}, - "source": [ - "### bp.init.OneInit" - ] - }, - { - "cell_type": "markdown", - "id": "c7419de2", - "metadata": {}, - "source": [ - "This initializer initializes all the weights with the same given value. In the following example, 5 presynaptic neurons are fully connected to 6 postsynaptic neurons with the same weight value:" - ] - }, - { - "cell_type": "code", - "execution_count": 3, - "id": "62387bba", - "metadata": {}, - "outputs": [], - "source": [ - "import numpy as np\n", - "import matplotlib.pyplot as plt\n", - "\n", - "# visualization\n", - "def mat_visualize(matrix, cmap=plt.cm.get_cmap('coolwarm')):\n", - " im = plt.matshow(matrix, cmap=cmap)\n", - " plt.colorbar(mappable=im, shrink=0.8, aspect=15)\n", - " plt.show()" - ] - }, - { - "cell_type": "code", - "execution_count": 4, - "id": "a815be5f", - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "JaxArray(DeviceArray([[2.5, 2.5, 2.5, 2.5, 2.5, 2.5],\n", - " [2.5, 2.5, 2.5, 2.5, 2.5, 2.5],\n", - " [2.5, 2.5, 2.5, 2.5, 2.5, 2.5],\n", - " [2.5, 2.5, 2.5, 2.5, 2.5, 2.5],\n", - " [2.5, 2.5, 2.5, 2.5, 2.5, 2.5]], dtype=float32))\n" - ] - } - ], - "source": [ - "shape = (5, 6)\n", - "one_init = bp.init.OneInit(value=2.5)\n", - "weights = one_init(shape)\n", - "print(weights)" - ] - }, - { - "cell_type": "markdown", - "id": "824db6d9", - "metadata": {}, - "source": [ - "## Built-In Random Initializers" - ] - }, - { - "cell_type": "markdown", - "id": "c57a12b1", - "metadata": {}, - "source": [ - "Random initializers all belong to `InterLayerInitializer` and initialize the connection weights between two layers with a random distribution. There are `Normal`, `Uniform`, `Orthogonal` and other initializers in built-in regular initializers. Here we show how to use the `Normal` and `Uniform` initializer. See [Weight Initialization](../apis/auto/training/initialize.rst) for more details." - ] - }, - { - "cell_type": "markdown", - "id": "ba256cfa", - "metadata": {}, - "source": [ - "### bp.init.Normal" - ] - }, - { - "cell_type": "markdown", - "id": "518cede6", - "metadata": {}, - "source": [ - "This initializer initializes the weights with a normal distribution. The variance of the distribution changes according to the `scale` parameter. In the following example, 10 presynaptic neurons are fully connected to 20 postsynaptic neurons with the same weight value:" - ] - }, - { - "cell_type": "code", - "execution_count": 5, - "id": "99f74e71", - "metadata": {}, - "outputs": [ - { - "data": { - "image/png": "iVBORw0KGgoAAAANSUhEUgAAAbgAAADVCAYAAAAl1+WDAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjQuMywgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy/MnkTPAAAACXBIWXMAAAsTAAALEwEAmpwYAAAVFklEQVR4nO3de5CddX3H8c/n7OZ+IVfIVQgSQaSIcQ0KSkGgRkoNWh3RimjV1GkRbdUWyyjOOM441VasMo5bpWC9F0UoRm7aiBZBAqRyh3ALmwSSEHK/7p5v/9iT6Uo2e559nt85e/bZ92vmTHJ2z/mcL082+fJ7nuf3+zkiBABA2VSGugAAABqBBgcAKCUaHACglGhwAIBSosEBAEqJBgcAKKWmNzjbS2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9ETHg5ttZtssBAKBfTdzw9IsRcWJEnCTpBkmfqfcGRnAAgFwiQtUm3WQSEdv6PJ2g3ntcBkSDAwDkNsiR2QzbffcT7YyIzMsX2f68pPdK2irpjHqvp8EBAHKJiMFOE9gUER2H+qbtWyXN6udbl0bEdRFxqaRLbX9K0kWqs40bDQ4AkFvKpboi4qyML/2epJ+JBgcAaIgmXoOzvTAiHqs9fYukh+u9hwYHAMglJFWbN9H7C7aPVe80gacl1d3ChAYHAMinibsJRMSfD/Y9NDgAQE7NO0WZBw0OAJBPi69F6Ubsxmp7o3rPkQ5khqRNyT8cfXGMG49j3Hgc4+KOjIji26+/iO0b1fvnk9WmiFiSuo5DaUiDy/TB9sqB5kOgOI5x43GMG49jjLxYixIAUEo0OABAKQ1lg8u8/hhy4xg3Hse48TjGyGXIrsEBANBInKIEAJQSDQ4AUEo0OABAKdHgAAClRIMDAJTS/wFfTf7g4jkMigAAAABJRU5ErkJggg==\n", - "text/plain": [ - "
" - ] - }, - "metadata": { - "needs_background": "light" - }, - "output_type": "display_data" - } - ], - "source": [ - "shape = (10, 20)\n", - "normal_init = bp.init.Normal(scale=1.0)\n", - "weights = normal_init(shape)\n", - "mat_visualize(weights)" - ] - }, - { - "cell_type": "markdown", - "id": "7ce49cea", - "metadata": {}, - "source": [ - "### bp.init.Uniform" - ] - }, - { - "cell_type": "markdown", - "id": "36003d3b", - "metadata": {}, - "source": [ - "This initializer initializes the weights with a uniform distribution. The variance of the distribution changes according to the `scale` parameter. In the following example, 10 presynaptic neurons are fully connected to 20 postsynaptic neurons with the same weight value:" - ] - }, - { - "cell_type": "code", - "execution_count": 6, - "id": "694f6017", - "metadata": {}, - "outputs": [ - { - "data": { - "image/png": "iVBORw0KGgoAAAANSUhEUgAAAboAAADVCAYAAAAhIjW+AAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjQuMywgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy/MnkTPAAAACXBIWXMAAAsTA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s5cUoEdFs+1xJCyXVS5obEctsn93++hxJX5O0i6SrbUtSc5FRIo0OAFC61DdejYgFkhZ0eG5OwddnSjqzO5mZGp3t89uDQ9Ljks6IiFe680YAgHyJ9NMLKqLoZ3S2x0j6V0mTIuJtahtOTq90YQCA3i9aI/OjWrKeuuwnaZDt1yQNVidXwQAA+paIUEtLa7XLKKroiC4i1kr6jqTVktZLej4i7q50YQCA3q8WRnRZTl2OVNsSLBMk7SlpiO1PdLLfTNuLbS9+viXN6hYAgN7r9c/oUi7qXAlZ5tEdKempiNgYEa+pbf7C+zruFBENETEpIiYNr+diTgDIv+Tz6CoiS0daLek9tgdLelnSEZIWd/1XAAC5F91eGaUqija6iHjI9q2SHpbULOkRtd0WAQDQh4Wk1hq4GCXTOcaIuFDShRWuBQBQS0JVvcgkKz5MAwCUqDamF9DoAAAlSX2H8Uqh0QEAShNSS3MfHdG1vtqqrateLTvnn6/on6Aa6Se/SnMX7Wd3Hp8k5xd/Lj/nI/vslqAS6Rt37p4kZ7fJNybJOea7702Ss+8J70ySc/859yXJueLLhybJ2dD6lyQ5u++0b5KcVT9bWHbGddeen6ASadkpTyTJmfKN05LkfOK+NHcq/6YHlp3RqvoElXSmutMGsmJEBwAoSYQUrX10RAcA6Bu4GAUAkF9R3TUss6LRAQBKEpJaWlqqXUZRNDoAQGmYMA4AyLNQ5GcJMAAAthNSK1ddAgDyjFOXAIDcigi1cjEKACDPGNEBAHIrIpheAADIN5YAAwDkF5/RAQDyLCS18hkdACC3uHsBACDfOHUJAMizGlnr0pW4O6ztjZKeLrLbaEmbkr85CnGMK49jXHkc4/KNj4hdU4favktt35+sNkXE1NR1FFORRpfpje3FETGpKm/eR3CMK49jXHkcY5SrrtoFAABQSTQ6AECuVbPRNVTxvfsKjnHlcYwrj2OMslTtMzoAAHoCpy4BALlGowMA5BqNDgCQazQ6AECu0egAALn2f92hYgqdiP1qAAAAAElFTkSuQmCC\n", - "text/plain": [ - "
" - ] - }, - "metadata": { - "needs_background": "light" - }, - "output_type": "display_data" - } - ], - "source": [ - "shape = (10, 20)\n", - "uniform_init = bp.init.Uniform(min_val=0., max_val=1.)\n", - "weights = uniform_init(shape)\n", - "mat_visualize(weights)" - ] - }, - { - "cell_type": "markdown", - "id": "8d63ac24", - "metadata": {}, - "source": [ - "## Built-In Decay Initializers" - ] - }, - { - "cell_type": "markdown", - "id": "aae2fd22", - "metadata": {}, - "source": [ - "Decay initializers all belong to `IntraLayerInitializer` and initialize the connection weights within a layer with a decay function according to the neural distance. There are `GaussianDecay` and `DOGDecay` initializers in built-in decay initializers. Below are examples of how to use them." - ] - }, - { - "cell_type": "markdown", - "id": "b26c0a47", - "metadata": {}, - "source": [ - "### brainpy.training.initialize.GaussianDecay" - ] - }, - { - "cell_type": "markdown", - "id": "17488f64", - "metadata": {}, - "source": [ - "This initializer creates a Gaussian connectivity pattern within a population of neurons, where the weights decay with a gaussian function.\n", - "Specifically, for any pair of neurons $ (i, j) $, the weight is computed as\n", - "\n", - "$$\n", - "w(i, j) = w_{max} \\cdot \\exp(-\\frac{\\sum_{k=1}^n |v_k^i - v_k^j|^2 }{2\\sigma^2})\n", - "$$\n", - "\n", - "where $ v_k^i $ is the $ i $-th neuron's encoded value (position) at dimension $ k $.\n", - "\n", - "The example below is a neural population with the size of $ 5 \\times 5 $. Note that this shape is the structure of the target neural population, not the size of presynaptic and postsynaptic neurons." - ] - }, - { - "cell_type": "code", - "execution_count": 7, - "id": "50c68a98", - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "shape of weights: (25, 25)\n" - ] - } - ], - "source": [ - "size = (5, 5)\n", - "gaussian_init = bp.init.GaussianDecay(sigma=2., max_w=10., include_self=True)\n", - "weights = gaussian_init(size)\n", - "print('shape of weights: {}'.format(weights.shape))" - ] - }, - { - "cell_type": "markdown", - "id": "6e23ccde", - "metadata": {}, - "source": [ - "Self-connections are created if `include_self=True`. The connection weights of each neuron with others are stored in each row of `weights`. For instance, the connection weights of neuron(0, 0) to other neurons are stored in `weights[0]`. After reshaping, the weights are:" - ] - }, - { - "cell_type": "code", - "execution_count": 8, - "id": "5ead905c", - "metadata": {}, - "outputs": [ - { - "data": { - "image/png": "iVBORw0KGgoAAAANSUhEUgAAAPkAAADVCAYAAACVIVbdAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjQuMywgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy/MnkTPAAAACXBIWXMAAAsTAAALEwEAmpwYAAAOIElEQVR4nO3db4wd1X3G8e9zr/+s7V1jE7sp8pqYRBFqhBSoNg61paoyVDJgEaQmrZFAaYXkN2lqKiQELyrUd31RIXiRNyugUIGoKCAVoTStFbAQUupkDW6EWapGaUqcOLVdoBhUBxx+fXFn7d10vXfu3TN3zsw+H2nEXu/12QfY3z1nzsw5o4jAzNqrU3cAM6uWi9ys5VzkZi3nIjdrORe5Wcu5yM1aLssil7RX0r9J+pGk+2rO8pikU5LeqDNHkWW7pJclzUo6LulgzXnGJH1f0r8Wef6yzjxzJHUlvS7pxbqz5CC7IpfUBb4F3AR8Abhd0hdqjPQ4sLfGnz/feeCeiPgt4HrgGzX/t/klsCcivghcC+yVdH2NeeYcBGbrDpGL7Ioc2An8KCJ+HBEfAX8HfKWuMBHxCvBOXT9/vog4GRGvFV+fpfeLvK3GPBERHxQvVxdHrXdXSZoEbgEeqTNHTnIs8m3AT+e9PkGNv8i5krQDuA44UnOOrqRjwCngUETUmgd4CLgX+KTmHNnIsci1yJ/53tt5JI0DzwF3R8T7dWaJiF9FxLXAJLBT0jV1ZZG0DzgVEUfrypCjHIv8BLB93utJ4Oc1ZcmOpNX0CvypiHi+7jxzIuI94DD1zl/sBm6V9BN6p3l7JD1ZY54s5FjkPwA+L+kqSWuA/cALNWfKgiQBjwKzEfFgBnm2StpUfL0OuBF4q648EXF/RExGxA56vzcvRcQddeXJRXZFHhHngT8F/onexNIzEXG8rjySnga+B1wt6YSku+rKQq+nupNeD3WsOG6uMc8Vw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xHx5qANucjNUkk4ux4RJ4GTxddnJc0C2wAXuVltBhuub5E0M+/1dERML9qstIPeng5DbaDqIjdLYfA73s5ExFT/ZjUOPAfcHRHvDxPNRW6WSuKbYYqHmTwHPBURzw/bjovcLIm0E2+SBDwKzEbEg8tpK8+r92ZNM3czTLotmXcDdwJ7JB0rjpuHieae3CwJoYQ3w0TEq/Q+OpbNRW6Wim9rNWsx7wxjtgJ4qalZy3kjR7MW83DdbAVo+nC9eODhDPCziNhXXSSzJlIrZtcPArPAxoqymDVbpj15qY8eSZPALcAj1cYxa6j0d7wlU/anPQTcS2+HikVJOiBpRtLM6XfeTZHNrFk0wDFCfYtc0j7gVEQcXep9ETEdEVMRMbX18s3JApo1R55VXuacfDdwa3Fz/BiwUdKTEXFHtdHMmiTf55P3TRUR90fEZETsAPYDL7nAzRbT3J7czMpowSU0IuIwcLiSJGZNJmV7Cc09uVkqbejJzezS5J7crOVc5GZt1o57183sUoR7crPWc09u1nJ5duQucrM0fE5u1m4+JzdbCVzkZi2W7yo0F7lZMnn25IqI9I1Kp4H/XGYzW4AzCeKk4jxLyylPqiyfiYitZd4o6TvFzy3rTETsHS7WYCop8hQkzZR5SPuoOM/ScsqTU5Yc5HkSYWbJuMjNWi7nIp+uO8CvcZ6l5ZQnpyy1y/ac3MzSyLknN7MEXORmLeciN2s5F7lZy7nIzVru/wBGrghgUGOb2AAAAABJRU5ErkJggg==\n", - "text/plain": [ - "
" - ] - }, - "metadata": { - "needs_background": "light" - }, - "output_type": "display_data" - } - ], - "source": [ - "mat_visualize(weights[0].reshape(size), cmap=plt.cm.get_cmap('Reds'))" - ] - }, - { - "cell_type": "markdown", - "id": "f7a25e57", - "metadata": {}, - "source": [ - "### brainpy.training.initialize.DOGDecay" - ] - }, - { - "cell_type": "markdown", - "id": "7b56e0dd", - "metadata": {}, - "source": [ - "This initializer creates a Difference-Of-Gaussian (DOG) connectivity pattern within a population of neurons. Specifically, for the given pair of neurons $ (i, j) $, the weight between them is computed as\n", - "\n", - "$$\n", - "w(i, j) = w_{max}^+ \\cdot \\exp(-\\frac{\\sum_{k=1}^n |v_k^i - v_k^j|^2}{2\\sigma_+^2}) - w_{max}^- \\cdot \\exp(-\\frac{\\sum_{k=1}^n |v_k^i - v_k^j|^2}{2\\sigma_-^2})\n", - "$$\n", - "\n", - "where $ v_k^i $ is the $ i $-th neuron's encoded value (position) at dimension $ k $.\n", - "\n", - "\n", - "\n", - "The example below is a neural population with the size of $ 10 \\times 10 $:" - ] - }, - { - "cell_type": "code", - "execution_count": 9, - "id": "9a4701d5", - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "shape of weights: (100, 100)\n" - ] - } - ], - "source": [ - "size = (10, 10)\n", - "gaussian_init = bp.init.DOGDecay(sigmas=(1., 3.), max_ws=(10., 5.), min_w=0.1, include_self=True)\n", - "weights = gaussian_init(size)\n", - "print('shape of weights: {}'.format(weights.shape))" - ] - }, - { - "cell_type": "markdown", - "id": "18203c26", - "metadata": {}, - "source": [ - "Weights smaller than `min_w` will not be created. `min_w` $ = 0.005 \\times min( $ `max_ws` $ ) $ if it is not assigned with a value.\n", - "The organization of `weights` is similar to that in the `GaussianDecay` initializer. For instance, the connection weights of neuron (3, 4) to other neurons after reshaping are shown as below:" - ] - }, - { - "cell_type": "code", - "execution_count": 10, - "id": "e29818d4", - "metadata": {}, - "outputs": [ - { - "data": { - "image/png": "iVBORw0KGgoAAAANSUhEUgAAAPsAAADVCAYAAACR1IbgAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjQuMywgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy/MnkTPAAAACXBIWXMAAAsTAAALEwEAmpwYAAAPE0lEQVR4nO3dX4xc5X3G8e+zu9iLjS1DbQVhQ03UKG2EGhGtEhqiVIqpRIOFb6oGKqI2ubAqNYmDkGhoLnLdNkqJVBRp5SQ3IIhEqBpViD8t4aJSY7H8URtwqBBJwfwpNggM2GDv7tOLmaW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mTrbnbc/ZntuxZXNfyUdMBKn6NpgHgcts/z7w38AtVf6oSrE/B1whaZMkAXuAw+tOM2JSTU1V3wZg+wHbi92HvwB2Vfm7NU/Q2T4k6W7gMWAReByYX2+iEROpvrnxXwV+UmXHSmfjbX8H+M4gGUVMvP6659slLax6PG/7vUZU0r8CF57l775t+5+7+3ybTgN8R5UnzAy6iCLU71n2Y7bnPuyXtq/q+WzSnwN7gT22XeUJU+wRpYxouqykq4G/Bv7Q9omqf5dijyhhtMtS/SOwEXiwc86cX9j+y7X+aDjFvrwMJwuMtRdaqYaTlb/8epsp9HI1aaWaxcW196ni9KlmxTlVIE613vH/G9F1MLZ/Zz1/l5Y9ophmX/WWYo8oIvdnj2iRtOwR7ZBlqSJaoMyc96FKsUeUkpY9oh2Ulj2iJVLsEW3Q99z4kUuxR5Qg0rJHtEZa9oiWaHbDnmKPKCPH7BHtkGP2iDZJsUe0QK56i2iRZrfsqrhWXX9BpaPA/6yx23bgWPEnX58m5QLJp5dR5vLbtndU2VHSfXRyq+qY7avXl9b6DKXYKz2xtNBrdc1RalIukHx6aVIu46bZBxkRUUyKPaIl6iz2Jt1Cqkm5QPLppUm5jJXajtkjYrTSjY9oiRR7REuk2CNaIsUe0RIp9oiW+D/CL6QdCAv7eAAAAABJRU5ErkJggg==\n", - "text/plain": [ - "
" - ] - }, - "metadata": { - "needs_background": "light" - }, - "output_type": "display_data" - } - ], - "source": [ - "mat_visualize(weights[34].reshape(size), cmap=plt.cm.get_cmap('Reds'))" - ] - }, - { - "cell_type": "markdown", - "id": "77c73b63", - "metadata": {}, - "source": [ - "## Customize your initializers" - ] - }, - { - "cell_type": "markdown", - "id": "0a8852bf", - "metadata": {}, - "source": [ - "BrainPy also allows users to customize the weight initializers of their own. When customizing a initializer, users should follow the instructions below:\n", - "- Your initializer should inherit `brainpy.training.initialize.Initializer`.\n", - "- Override the `__call__` funtion, to which the `shape` parameter should be given.\n", - "\n", - "Here is an example of creating an inter-layer initializer that initialize the weights as follows:\n", - "\n", - "$$\n", - "w(i, j) = max(w_{max} - \\sigma |v_i - v_j|, 0)\n", - "$$" - ] - }, - { - "cell_type": "code", - "execution_count": 11, - "id": "c0d03e7f", - "metadata": {}, - "outputs": [], - "source": [ - "class LinearDecay(bp.init.InterLayerInitializer):\n", - " def __init__(self, max_w, sigma=1.):\n", - " self.max_w = max_w\n", - " self.sigma = sigma\n", - " \n", - " def __call__(self, shape, dtype=None):\n", - " mat = bp.math.zeros(shape, dtype=dtype)\n", - " n_pre, n_post = shape\n", - " seq = np.arange(n_pre)\n", - " current_w = self.max_w\n", - " \n", - " for i in range(max(n_pre, n_post)):\n", - " if current_w <= 0:\n", - " break\n", - " seq_plus = ((seq + i) >= 0) & ((seq + i) < n_post)\n", - " seq_minus = ((seq - i) >= 0) & ((seq - i) < n_post)\n", - " mat[seq[seq_plus], (seq + i)[seq_plus]] = current_w\n", - " mat[seq[seq_minus], (seq - i)[seq_minus]] = current_w\n", - " current_w -= self.sigma\n", - " \n", - " return mat" - ] - }, - { - "cell_type": "code", - "execution_count": 12, - "id": "0c327190", - "metadata": {}, - "outputs": [ - { - "data": { - "image/png": "iVBORw0KGgoAAAANSUhEUgAAAVEAAADXCAYAAACj6GIzAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjQuMywgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy/MnkTPAAAACXBIWXMAAAsTAAALEwEAmpwYAAAQ6UlEQVR4nO3dcaxkZXnH8e/v3sUuLLtC2VWRpQVbQ2tIC2aDKEYtiNkigSa1CaYQ29rwT1U0NhZiUv9rTGqsJjW2N4g1kWLMCikxihKVGBMlLLAqsFgJWlzAskuroi3C6tM/Zi69rPfOnJk5M2dm+X6Sk70z9933PNw7PPu+57zvc1JVSJLGs9R1AJK0yEyikjQBk6gkTcAkKkkTMIlK0gRMopI0gZkn0SS7k3wnyQNJrp71+Y+I5dQkX0myP8m9Sa7qMp5+TMtJ7k7y2TmI5YQke5Lc3/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MrzJzMxTLGA8g8xTLGA8g8xTLI1V1eEkbwO+ACwD11XVvRu1n8qNJUl6rnA6L0kTMIlK0gRMopI0AZOoJE3AJCpJEzCJStIETKKSNIH/A3HMI67vl6/4AAAAAElFTkSuQmCC\n", - "text/plain": [ - "
" - ] - }, - "metadata": { - "needs_background": "light" - }, - "output_type": "display_data" - } - ], - "source": [ - "shape = (10, 15)\n", - "lin_init = LinearDecay(max_w=5, sigma=1.)\n", - "weights = lin_init(shape)\n", - "mat_visualize(weights, cmap=plt.cm.get_cmap('Reds'))" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "cd5d5480", - "metadata": {}, - "outputs": [], - "source": [] - } - ], - "metadata": { - "kernelspec": { - "display_name": "Python 3", - "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" - } - }, - "nbformat": 4, - "nbformat_minor": 5 -} \ No newline at end of file diff --git a/examples/simulation/Wang_2002_decision_making_spiking.py b/examples/simulation/Wang_2002_decision_making_spiking.py index 1d2e60f68..ecc0cde81 100644 --- a/examples/simulation/Wang_2002_decision_making_spiking.py +++ b/examples/simulation/Wang_2002_decision_making_spiking.py @@ -3,6 +3,7 @@ import brainpy as bp import brainpy.math as bm +bp.check.turn_off() bm.set_platform('cpu') import matplotlib.pyplot as plt