{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# *(Tian, et al., 2020)* E/I Net for fast response\n", "\n", "[![Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/brainpy/examples/blob/main/ei_nets/Tian_2020_EI_net_for_fast_response.ipynb)\n", "[![Open in Kaggle](https://kaggle.com/static/images/open-in-kaggle.svg)](https://kaggle.com/kernels/welcome?src=https://github.com/brainpy/examples/blob/main/ei_nets/Tian_2020_EI_net_for_fast_response.ipynb)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Implementation of the paper: \n", "\n", "- *Tian, Gengshuo, et al. \"Excitation-Inhibition Balanced Neural Networks for Fast Signal Detection.\" Frontiers in Computational Neuroscience 14 (2020): 79.*\n", "\n", "Author: [Chaoming Wang](https://github.com/chaoming0625)" ] }, { "cell_type": "code", "execution_count": 31, "metadata": { "ExecuteTime": { "end_time": "2023-08-27T07:21:03.140094300Z", "start_time": "2023-08-27T07:21:03.100966200Z" }, "pycharm": { "is_executing": true } }, "outputs": [], "source": [ "import brainpy as bp\n", "import brainpy.math as bm\n", "import numpy as np\n", "\n", "bp.math.set_platform('cpu')" ] }, { "cell_type": "code", "execution_count": 32, "metadata": { "ExecuteTime": { "end_time": "2023-08-27T07:21:03.170723200Z", "start_time": "2023-08-27T07:21:03.112097100Z" }, "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "'2.4.4.post1'" ] }, "execution_count": 32, "metadata": {}, "output_type": "execute_result" } ], "source": [ "bp.__version__" ] }, { "cell_type": "code", "execution_count": 33, "metadata": { "ExecuteTime": { "end_time": "2023-08-27T07:21:03.172217100Z", "start_time": "2023-08-27T07:21:03.127486800Z" } }, "outputs": [], "source": [ "# set parameters\n", "\n", "num = 10000\n", "num_inh = int(num * 0.2)\n", "num_exc = num - num_inh\n", "prob = 0.25\n", "\n", "tau_E = 15.\n", "tau_I = 10.\n", "V_reset = 0.\n", "V_threshold = 15.\n", "f_E = 3.\n", "f_I = 2.\n", "mu_f = 0.1\n", "\n", "tau_Es = 6.\n", "tau_Is = 5.\n", "JEE = 0.25\n", "JEI = -1.\n", "JIE = 0.4\n", "JII = -1." ] }, { "cell_type": "markdown", "metadata": { "collapsed": false }, "source": [ "## Old version " ] }, { "cell_type": "code", "execution_count": 34, "metadata": { "ExecuteTime": { "end_time": "2023-08-27T07:21:03.173224900Z", "start_time": "2023-08-27T07:21:03.146032700Z" }, "collapsed": false }, "outputs": [], "source": [ "class LIF(bp.dyn.NeuDyn):\n", " def __init__(self, size, tau, **kwargs):\n", " super().__init__(size, **kwargs)\n", "\n", " # parameters\n", " self.tau = tau\n", "\n", " # variables\n", " self.V = bp.math.Variable(bp.math.zeros(size))\n", " self.spike = bp.math.Variable(bp.math.zeros(size, dtype=bool))\n", " self.input = bp.math.Variable(bp.math.zeros(size))\n", "\n", " # integral\n", " self.integral = bp.odeint(lambda V, t, Isyn: (-V + Isyn) / self.tau)\n", "\n", " def update(self):\n", " tdi = bp.share.get_shargs()\n", " V = self.integral(self.V, tdi.t, self.input, tdi.dt)\n", " self.spike.value = V >= V_threshold\n", " self.V.value = bm.where(self.spike, V_reset, V)\n", " self.input[:] = 0." ] }, { "cell_type": "code", "execution_count": 35, "metadata": { "ExecuteTime": { "end_time": "2023-08-27T07:21:03.173224900Z", "start_time": "2023-08-27T07:21:03.161304300Z" }, "collapsed": false }, "outputs": [], "source": [ "class EINet(bp.DynSysGroup):\n", " def __init__(self):\n", " # neurons\n", " E = LIF(num_exc, tau=tau_E)\n", " I = LIF(num_inh, tau=tau_I)\n", " E.V[:] = bm.random.random(num_exc) * (V_threshold - V_reset) + V_reset\n", " I.V[:] = bm.random.random(num_inh) * (V_threshold - V_reset) + V_reset\n", "\n", " # synapses\n", " E2I = bp.synapses.Exponential(pre=E, post=I, conn=bp.conn.FixedProb(prob), tau=tau_Es, g_max=JIE)\n", " E2E = bp.synapses.Exponential(pre=E, post=E, conn=bp.conn.FixedProb(prob), tau=tau_Es, g_max=JEE)\n", " I2I = bp.synapses.Exponential(pre=I, post=I, conn=bp.conn.FixedProb(prob), tau=tau_Is, g_max=JII)\n", " I2E = bp.synapses.Exponential(pre=I, post=E, conn=bp.conn.FixedProb(prob), tau=tau_Is, g_max=JEI)\n", "\n", " super(EINet, self).__init__(E2E, E2I, I2E, I2I, E=E, I=I)" ] }, { "cell_type": "code", "execution_count": 36, "metadata": { "ExecuteTime": { "end_time": "2023-08-27T07:21:08.552766500Z", "start_time": "2023-08-27T07:21:03.173224900Z" }, "collapsed": false }, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "ef8ffe25d6e540b288e5f3e327464f23", "version_major": 2, "version_minor": 0 }, "text/plain": [ " 0%| | 0/1000 [00:00" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "# visualization\n", "fig, gs = bp.visualize.get_figure(5, 1, 1.5, 10)\n", "\n", "fig.add_subplot(gs[:3, 0])\n", "bp.visualize.raster_plot(runner.mon.ts, runner.mon['E.spike'], xlim=(0, 100))\n", "\n", "fig.add_subplot(gs[3:, 0])\n", "bp.visualize.raster_plot(runner.mon.ts, runner.mon['I.spike'], xlim=(0, 100), show=True)" ] }, { "cell_type": "markdown", "metadata": { "collapsed": false }, "source": [ "## New version (brainpy>=2.4.0)" ] }, { "cell_type": "code", "execution_count": 38, "metadata": { "ExecuteTime": { "end_time": "2023-08-27T07:21:08.918081400Z", "start_time": "2023-08-27T07:21:08.818820200Z" }, "collapsed": false }, "outputs": [], "source": [ "class LIF(bp.dyn.NeuDyn):\n", " def __init__(self, size, tau, **kwargs):\n", " super().__init__(size, **kwargs)\n", "\n", " # parameters\n", " self.tau = tau\n", "\n", " # variables\n", " self.V = bp.math.Variable(bp.math.zeros(size))\n", " self.spike = bp.math.Variable(bp.math.zeros(size, dtype=bool))\n", "\n", " # integral\n", " self.integral = bp.odeint(lambda V, t, Isyn: (-V + Isyn) / self.tau)\n", "\n", " def update(self, x=None):\n", " x = 0. if x is None else x\n", " x = self.sum_inputs(self.V, init=x)\n", " V = self.integral(self.V, bp.share['t'], x, bp.share['dt'])\n", " self.spike.value = V >= V_threshold\n", " self.V.value = bm.where(self.spike, V_reset, V)\n", " return self.spike.value\n", " \n", " def return_info(self):\n", " return self.spike" ] }, { "cell_type": "code", "execution_count": 39, "metadata": { "ExecuteTime": { "end_time": "2023-08-27T07:21:08.918081400Z", "start_time": "2023-08-27T07:21:08.829700600Z" }, "collapsed": false }, "outputs": [], "source": [ "class ExponCUBA(bp.Projection):\n", " def __init__(self, pre, post, prob, g_max, tau):\n", " super().__init__()\n", " self.proj = bp.dyn.ProjAlignPostMg2(\n", " pre=pre,\n", " delay=None, \n", " comm=bp.dnn.EventCSRLinear(bp.conn.FixedProb(prob, pre=pre.num, post=post.num), g_max),\n", " syn=bp.dyn.Expon.desc(post.num, tau=tau),\n", " out=bp.dyn.CUBA.desc(),\n", " post=post,\n", " )" ] }, { "cell_type": "code", "execution_count": 40, "metadata": { "ExecuteTime": { "end_time": "2023-08-27T07:21:08.918081400Z", "start_time": "2023-08-27T07:21:08.845977100Z" } }, "outputs": [], "source": [ "class EINet(bp.DynSysGroup):\n", " def __init__(self):\n", " super().__init__()\n", " # neurons\n", " self.E = LIF(num_exc, tau=tau_E)\n", " self.I = LIF(num_inh, tau=tau_I)\n", " self.E.V[:] = bm.random.random(num_exc) * (V_threshold - V_reset) + V_reset\n", " self.I.V[:] = bm.random.random(num_inh) * (V_threshold - V_reset) + V_reset\n", "\n", " # synapses\n", " self.E2I = ExponCUBA(self.E, self.I, prob, tau=tau_Es, g_max=JIE)\n", " self.E2E = ExponCUBA(self.E, self.E, prob, tau=tau_Es, g_max=JEE)\n", " self.I2I = ExponCUBA(self.I, self.I, prob, tau=tau_Is, g_max=JII)\n", " self.I2E = ExponCUBA(self.I, self.E, prob, tau=tau_Is, g_max=JEI)\n", " \n", " def update(self, e_inp, i_inp):\n", " self.E2E()\n", " self.E2I()\n", " self.I2E()\n", " self.I2I()\n", " self.E(e_inp)\n", " self.I(i_inp)\n", " \n", " # monitor\n", " return self.E.spike, self.I.spike" ] }, { "cell_type": "code", "execution_count": 41, "metadata": { "ExecuteTime": { "end_time": "2023-08-27T07:21:11.184288100Z", "start_time": "2023-08-27T07:21:08.860501600Z" } }, "outputs": [], "source": [ "net = EINet()" ] }, { "cell_type": "code", "execution_count": 42, "metadata": { "ExecuteTime": { "end_time": "2023-08-27T07:21:12.056652600Z", "start_time": "2023-08-27T07:21:11.184288100Z" } }, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "3b7d6674150346408408a71705e7f473", "version_major": 2, "version_minor": 0 }, "text/plain": [ " 0%| | 0/1000 [00:00" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "ts = indices * bm.get_dt()\n", "\n", "# visualization\n", "fig, gs = bp.visualize.get_figure(5, 1, 1.5, 10)\n", "\n", "fig.add_subplot(gs[:3, 0])\n", "bp.visualize.raster_plot(ts, e_sps, xlim=(0, 100))\n", "\n", "fig.add_subplot(gs[3:, 0])\n", "bp.visualize.raster_plot(ts, i_sps, xlim=(0, 100), show=True)" ] } ], "metadata": { "hide_input": false, "jupytext": { "encoding": "# -*- coding: utf-8 -*-", "formats": "ipynb,py:percent" }, "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.9.12" }, "latex_envs": { "LaTeX_envs_menu_present": true, "autoclose": false, "autocomplete": true, "bibliofile": "biblio.bib", "cite_by": "apalike", "current_citInitial": 1, "eqLabelWithNumbers": true, "eqNumInitial": 1, "hotkeys": { "equation": "Ctrl-E", "itemize": "Ctrl-I" }, "labels_anchors": false, "latex_user_defs": false, "report_style_numbering": false, "user_envs_cfg": false }, "toc": { "base_numbering": 1, "nav_menu": {}, "number_sections": false, "sideBar": true, "skip_h1_title": false, "title_cell": "Table of Contents", "title_sidebar": "Contents", "toc_cell": false, "toc_position": { "height": "calc(100% - 180px)", "left": "10px", "top": "150px", "width": "243.07px" }, "toc_section_display": true, "toc_window_display": true }, "varInspector": { "cols": { "lenName": 16, "lenType": 16, "lenVar": 40 }, "kernels_config": { "python": { "delete_cmd_postfix": "", "delete_cmd_prefix": "del ", "library": "var_list.py", "varRefreshCmd": "print(var_dic_list())" }, "r": { "delete_cmd_postfix": ") ", "delete_cmd_prefix": "rm(", "library": "var_list.r", "varRefreshCmd": "cat(var_dic_list()) " } }, "types_to_exclude": [ "module", "function", "builtin_function_or_method", "instance", "_Feature" ], "window_display": false } }, "nbformat": 4, "nbformat_minor": 4 }